Skip to content

Commit 4ab5b32

Browse files
committed
first few solutions
1 parent a10d4d1 commit 4ab5b32

35 files changed

+1496
-0
lines changed

1.two-sum.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode id=1 lang=rust
3+
*
4+
* [1] Two Sum
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
10+
impl Solution {
11+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
12+
let size = nums.len();
13+
let mut indices = HashMap::new();
14+
15+
for i in 0..size {
16+
let val = nums.get(i).unwrap();
17+
if (indices.contains_key(&(target - val))) {
18+
let j = indices.get(&(target - val)).unwrap();
19+
return vec![*j as i32, i as i32];
20+
}
21+
indices.insert(val, i);
22+
}
23+
24+
return vec![0, 0];
25+
}
26+
}
27+
// @lc code=end
28+

100.same-tree.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode id=100 lang=rust
3+
*
4+
* [100] Same Tree
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
30+
31+
}
32+
}
33+
// @lc code=end
34+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @lc app=leetcode id=109 lang=rust
3+
*
4+
* [109] Convert Sorted List to Binary Search Tree
5+
*/
6+
7+
// @lc code=start
8+
// Definition for singly-linked list.
9+
// #[derive(PartialEq, Eq, Clone, Debug)]
10+
// pub struct ListNode {
11+
// pub val: i32,
12+
// pub next: Option<Box<ListNode>>
13+
// }
14+
//
15+
// impl ListNode {
16+
// #[inline]
17+
// fn new(val: i32) -> Self {
18+
// ListNode {
19+
// next: None,
20+
// val
21+
// }
22+
// }
23+
// }
24+
// Definition for a binary tree node.
25+
// #[derive(Debug, PartialEq, Eq)]
26+
// pub struct TreeNode {
27+
// pub val: i32,
28+
// pub left: Option<Rc<RefCell<TreeNode>>>,
29+
// pub right: Option<Rc<RefCell<TreeNode>>>,
30+
// }
31+
//
32+
// impl TreeNode {
33+
// #[inline]
34+
// pub fn new(val: i32) -> Self {
35+
// TreeNode {
36+
// val,
37+
// left: None,
38+
// right: None
39+
// }
40+
// }
41+
// }
42+
use std::rc::Rc;
43+
use std::cell::RefCell;
44+
impl Solution {
45+
pub fn sorted_list_to_bst(head: Option<Box<ListNode>>) -> Option<Rc<RefCell<TreeNode>>> {
46+
47+
}
48+
}
49+
// @lc code=end
50+

11.container-with-most-water.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode id=11 lang=rust
3+
*
4+
* [11] Container With Most Water
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn max_area(height: Vec<i32>) -> i32 {
10+
let mut left = 0;
11+
let mut right = height.len() - 1;
12+
let mut ans = 0;
13+
14+
while left < right {
15+
if height[left] < height[right] {
16+
let area = (right - left) as i32 * height[left];
17+
if (ans < area) {
18+
ans = area;
19+
}
20+
left += 1;
21+
}
22+
else {
23+
let area = (right - left) as i32 * height[right];
24+
if (ans < area) {
25+
ans = area;
26+
}
27+
right -= 1;
28+
}
29+
}
30+
31+
ans
32+
}
33+
}
34+
// @lc code=end
35+

136.single-number.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @lc app=leetcode id=136 lang=rust
3+
*
4+
* [136] Single Number
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn single_number(nums: Vec<i32>) -> i32 {
10+
let mut ans = 0;
11+
12+
for num in nums {
13+
ans ^= num;
14+
}
15+
16+
ans
17+
}
18+
}
19+
// @lc code=end
20+

141.linked-list-cycle.rs

Whitespace-only changes.

143.reorder-list.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* @lc app=leetcode id=143 lang=rust
3+
*
4+
* [143] Reorder List
5+
*/
6+
7+
// @lc code=start
8+
// Definition for singly-linked list.
9+
// #[derive(PartialEq, Eq, Clone, Debug)]
10+
// pub struct ListNode {
11+
// pub val: i32,
12+
// pub next: Option<Box<ListNode>>
13+
// }
14+
//
15+
// impl ListNode {
16+
// #[inline]
17+
// fn new(val: i32) -> Self {
18+
// ListNode {
19+
// next: None,
20+
// val
21+
// }
22+
// }
23+
// }
24+
use std::mem;
25+
26+
impl Solution {
27+
pub fn reorder_list(mut head: &mut Option<Box<ListNode>>) {
28+
29+
let mut href = head.as_ref();
30+
let mut N = 0;
31+
32+
while let Some(node) = href.take() {
33+
href = node.next.as_ref();
34+
N += 1;
35+
}
36+
37+
let mut hmut = head.as_mut();
38+
let mut cnt = 1;
39+
40+
while cnt < (N + 1) / 2 {
41+
if let Some(node) = hmut.take() {
42+
hmut = node.next.as_mut();
43+
cnt += 1;
44+
} else {
45+
break;
46+
}
47+
}
48+
49+
match hmut.take() {
50+
None => (),
51+
Some(node) => {
52+
let tail = Self::reverse(node.next.take());
53+
if let Some(node) = head {
54+
node.next = Self::merge_two_lists(tail, node.next.take());
55+
}
56+
}
57+
}
58+
59+
}
60+
61+
pub fn merge_two_lists(mut l1: Option<Box<ListNode>>, mut l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
62+
63+
match (l1.as_mut(), l2.as_mut()) {
64+
(None, None) => None,
65+
(Some(node1), None) => l1,
66+
(None, Some(node2)) => l2,
67+
(Some(node1), Some(node2)) => {
68+
node1.next = Self::merge_two_lists(l2, node1.next.take());
69+
l1
70+
}
71+
}
72+
}
73+
74+
pub fn reverse(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
75+
76+
let mut prev = None;
77+
let mut curr = head;
78+
79+
while let Some(mut curr_inner) = curr.take() {
80+
curr = curr_inner.next.take();
81+
curr_inner.next = prev.take();
82+
prev = Some(curr_inner);
83+
}
84+
85+
prev
86+
}
87+
}
88+
// @lc code=end
89+

15.3-sum.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @lc app=leetcode id=15 lang=rust
3+
*
4+
* [15] 3Sum
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
10+
nums.sort();
11+
12+
let n = nums.len();
13+
let mut ans: Vec<Vec<i32>> = vec![];
14+
if (n < 3) {
15+
return ans;
16+
}
17+
18+
let mut i = 0;
19+
let mut j = 1;
20+
let mut k = n-1;
21+
22+
while i <= n - 2 {
23+
j = i + 1;
24+
k = n - 1;
25+
26+
while j < k {
27+
if nums[i] + nums[j] + nums[k] < 0 {
28+
j += 1;
29+
} else if nums[i] + nums[j] + nums[k] > 0 {
30+
k -= 1;
31+
} else {
32+
ans.push(vec![nums[i], nums[j], nums[k]]);
33+
while j < k && nums[j] == nums[j+1] {
34+
j += 1;
35+
}
36+
j += 1;
37+
while j < k && nums[k-1] == nums[k] {
38+
k -= 1;
39+
}
40+
k -= 1;
41+
}
42+
43+
}
44+
45+
while i <= n - 2 && nums[i] == nums[i+1] {
46+
i += 1;
47+
}
48+
i += 1;
49+
}
50+
51+
ans
52+
}
53+
}
54+
// @lc code=end
55+

16.3-sum-closest.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @lc app=leetcode id=16 lang=rust
3+
*
4+
* [16] 3Sum Closest
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn three_sum_closest(mut nums: Vec<i32>, target: i32) -> i32 {
10+
let N = nums.len();
11+
12+
nums.sort();
13+
14+
let mut ans = 0;
15+
for i in 0..3 {
16+
ans += nums[i];
17+
}
18+
19+
let mut i = 0;
20+
let mut j = 1;
21+
let mut k = N - 1;
22+
23+
while i < N - 2 {
24+
j = i + 1;
25+
k = N - 1;
26+
27+
while j < k {
28+
let sum = nums[i] + nums[j] + nums[k];
29+
if ((sum - target).abs() < (ans - target).abs()) {
30+
ans = sum;
31+
}
32+
if (sum < target) {
33+
j += 1;
34+
} else if (sum > target) {
35+
k -= 1;
36+
} else {
37+
return sum;
38+
}
39+
}
40+
41+
while i < N - 2 && nums[i] == nums[i+1] {
42+
i += 1;
43+
}
44+
45+
i += 1;
46+
}
47+
48+
ans
49+
}
50+
}
51+
// @lc code=end
52+

0 commit comments

Comments
 (0)