We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6a8cdad commit 7409b53Copy full SHA for 7409b53
aaa_macro/Cargo.toml
@@ -0,0 +1,8 @@
1
+[package]
2
+name = "aaa_macro"
3
+version = "0.1.0"
4
+edition = "2018"
5
+
6
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8
+[dependencies]
aaa_macro/src/lib.rs
@@ -0,0 +1,36 @@
+#[derive(PartialEq, Eq, Clone, Debug)]
+pub struct ListNode {
+ pub val: i32,
+ pub next: Option<Box<ListNode>>
+}
+impl ListNode {
9
+ pub fn new(val: i32) -> ListNode {
10
+ ListNode {
11
+ val,
12
+ next: None
13
+ }
14
15
16
17
+#[macro_export]
18
+macro_rules! linklist {
19
+ () => {
20
+ None
21
+ };
22
+ ($($e:expr), *) => {
23
+ {
24
+ let mut head = Box::new(ListNode::new(0));
25
+ let mut ref_head = &mut head;
26
27
+ $(
28
+ ref_head.next = Some(Box::new(ListNode::new($e)));
29
+ ref_head = ref_head.next.as_mut().unwrap();
30
+ )*
31
32
+ let _ = ref_head; // 避免 `unused_assignments`
33
+ head.next
34
35
36
double-pointer/Cargo.toml
@@ -6,3 +6,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+aaa_macro = {path="../aaa_macro"}
double-pointer/src/lib.rs
@@ -1,5 +1,8 @@
+use aaa_macro::*;
mod q977;
mod q283;
mod q167;
mod q344;
-mod q557;
+mod q557;
+mod q876;
double-pointer/src/q557.rs
@@ -1,29 +1,30 @@
-struct Solution ();
+struct Solution();
impl Solution {
pub fn reverse_words(s: String) -> String {
-
let mut v = s.into_bytes();
- let (mut left, mut right) = (0, 0);
+ let (mut l, mut r) = (0, 0);
- while right < v.len() {
- while right < v.len() && v[right] != ' ' as u8 {
- right += 1
+ while r < v.len() {
+ while r < v.len() && v[r] != ' ' as u8 {
+ r += 1;
}
- let next = right + 1;
+ let new_pos = r + 1;
- while left < right {
+ while l < r {
unsafe {
- std::ptr::swap(&mut v[right -1], &mut v[left]);
+ std::ptr::swap(&mut v[l], &mut v[r-1]);
+ l += 1;
+ r -= 1;
- left += 1;
- right -= 1;
- left = next;
- right = next;
- }
+ l = new_pos;
+ r = new_pos;
String::from_utf8(v).unwrap()
@@ -32,14 +33,10 @@ impl Solution {
mod tests {
use super::*;
- #[test] fn test_1() {
37
38
+ #[test]
+ fn test_1() {
39
assert_eq!(
40
Solution::reverse_words(String::from("Let's take LeetCode contest")),
41
String::from("s'teL ekat edoCteeL tsetnoc"))
42
43
44
45
double-pointer/src/q876.py
@@ -0,0 +1,39 @@
+# Definition for singly-linked list.
+import pytest
+class ListNode:
+ def __init__(self, val=0, next=None):
+ self.val = val
+ self.next = next
+class Solution:
+ def middleNode(self, head: ListNode) -> ListNode:
+ if head.next is None:
+ return head
+ fp = head
+ np = head
+ while True:
+ np = np.next
+ if np is None:
+ return fp
+ return fp.next
+ fp = fp.next
+def test_mid():
+ head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5, None)))))
+ assert Solution().middleNode(head).val == 3
+ head = ListNode(0, head)
+if __name__ == '__main__':
+ pytest.main(["./q876.py", "-s", "-v"])
double-pointer/src/q876.rs
+use super::*;
+struct Solution ();
+impl Solution {
+ pub fn middle_node(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
+ let mut fast = head.as_ref().unwrap();
+ let mut slow = head.as_ref();
+ if fast.next.is_none() { return head; }
+ while !fast.next.is_none() && !fast.next.as_ref().unwrap().next.is_none() {
+ fast = fast.next.as_ref().unwrap().next.as_ref().unwrap();
+ if fast.next.is_none() {
+ return slow.unwrap().next.clone();
+ slow = slow.unwrap().next.as_ref();
+ slow.unwrap().next.clone()
+#[cfg(test)]
+mod tests {
+ use super::*;
+ #[test] fn test_1() {
+ assert_eq!(Solution::middle_node(linklist!(1,2,3,4,5)).unwrap().val, 3);
+ assert_eq!(Solution::middle_node(linklist!(1,2,3,4,5, 6)).unwrap().val, 4);
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments