Skip to content

Commit bc55636

Browse files
author
joker53-1
committed
first
1 parent 69fb9b8 commit bc55636

14 files changed

+1098
-0
lines changed

src/problem/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod p0001_two_sum;
2+
mod p0002_add_two_numbers;
3+
mod p0003_longest_substring_without_repeating_characters;
4+
mod p0004_median_of_two_sorted_arrays;
5+
mod p0005_longest_palindromic_substring;
6+
mod p0006_zigzag_conversion;
7+
mod p0007_reverse_integer;
8+
mod p0008_string_to_integer_atoi;
9+
mod p0009_palindrome_number;
10+
mod p0094_binary_tree_inorder_traversal;
11+
mod p0095_unique_binary_search_trees_ii;
12+
mod p0096_unique_binary_search_trees;
13+
mod p0098_validate_binary_search_tree;

src/problem/p0001_two_sum.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use std::collections::HashMap;
2+
3+
/**
4+
* [1] Two Sum
5+
*
6+
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
7+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
8+
* You can return the answer in any order.
9+
*
10+
* <strong class="example">Example 1:
11+
*
12+
* Input: nums = [2,7,11,15], target = 9
13+
* Output: [0,1]
14+
* Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
15+
*
16+
* <strong class="example">Example 2:
17+
*
18+
* Input: nums = [3,2,4], target = 6
19+
* Output: [1,2]
20+
*
21+
* <strong class="example">Example 3:
22+
*
23+
* Input: nums = [3,3], target = 6
24+
* Output: [0,1]
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 2 <= nums.length <= 10^4
30+
* -10^9 <= nums[i] <= 10^9
31+
* -10^9 <= target <= 10^9
32+
* Only one valid answer exists.
33+
*
34+
*
35+
* Follow-up: Can you come up with an algorithm that is less than O(n^2)<font face="monospace"> </font>time complexity?
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/two-sum/
40+
// discuss: https://leetcode.com/problems/two-sum/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
46+
let mut map = HashMap::new();
47+
nums.iter().enumerate().for_each(|(i, &v)| {
48+
map.insert(target-v, i);
49+
});
50+
for (idx, x) in nums.iter().enumerate() {
51+
if let Some(&i) = map.get(&(x)) {
52+
if i != idx {
53+
return vec![idx as i32, i as i32];
54+
}
55+
}
56+
}
57+
vec![]
58+
}
59+
}
60+
61+
// submission codes end
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
fn test_1() {
69+
}
70+
}

src/problem/p0002_add_two_numbers.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* [2] Add Two Numbers
3+
*
4+
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
5+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6+
*
7+
* <strong class="example">Example 1:
8+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" />
9+
* Input: l1 = [2,4,3], l2 = [5,6,4]
10+
* Output: [7,0,8]
11+
* Explanation: 342 + 465 = 807.
12+
*
13+
* <strong class="example">Example 2:
14+
*
15+
* Input: l1 = [0], l2 = [0]
16+
* Output: [0]
17+
*
18+
* <strong class="example">Example 3:
19+
*
20+
* Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
21+
* Output: [8,9,9,9,0,0,0,1]
22+
*
23+
*
24+
* Constraints:
25+
*
26+
* The number of nodes in each linked list is in the range [1, 100].
27+
* 0 <= Node.val <= 9
28+
* It is guaranteed that the list represents a number that does not have leading zeros.
29+
*
30+
*/
31+
pub struct Solution {}
32+
use crate::util::linked_list::{ListNode, to_list};
33+
34+
// problem: https://leetcode.com/problems/add-two-numbers/
35+
// discuss: https://leetcode.com/problems/add-two-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
36+
37+
// submission codes start here
38+
39+
// Definition for singly-linked list.
40+
// #[derive(PartialEq, Eq, Clone, Debug)]
41+
// pub struct ListNode {
42+
// pub val: i32,
43+
// pub next: Option<Box<ListNode>>
44+
// }
45+
//
46+
// impl ListNode {
47+
// #[inline]
48+
// fn new(val: i32) -> Self {
49+
// ListNode {
50+
// next: None,
51+
// val
52+
// }
53+
// }
54+
// }
55+
impl Solution {
56+
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
57+
let (mut l1, mut l2) = (l1, l2);
58+
let mut head = ListNode::new(0);
59+
let mut tail = &mut head;
60+
let mut carry = 0;
61+
while l1.is_some() || l2.is_some() {
62+
if let Some(node) = l1{
63+
carry += node.val;
64+
l1 = node.next;
65+
}
66+
if let Some(node) = l2{
67+
carry += node.val;
68+
l2 = node.next;
69+
}
70+
tail.next = Some(Box::new(ListNode::new(carry % 10)));
71+
72+
carry = carry / 10;
73+
tail = tail.next.as_mut()?;
74+
}
75+
if carry > 0 {
76+
tail.next = Some(Box::new(ListNode::new(carry)));
77+
}
78+
head.next
79+
}
80+
}
81+
82+
// submission codes end
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
#[test]
89+
fn test_2() {
90+
}
91+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::cmp::max;
2+
use std::collections::{HashMap, HashSet};
3+
4+
/**
5+
* [3] Longest Substring Without Repeating Characters
6+
*
7+
* Given a string s, find the length of the longest <span data-keyword="substring-nonempty">substring</span> without duplicate characters.
8+
*
9+
* <strong class="example">Example 1:
10+
*
11+
* Input: s = "abcabcbb"
12+
* Output: 3
13+
* Explanation: The answer is "abc", with the length of 3.
14+
*
15+
* <strong class="example">Example 2:
16+
*
17+
* Input: s = "bbbbb"
18+
* Output: 1
19+
* Explanation: The answer is "b", with the length of 1.
20+
*
21+
* <strong class="example">Example 3:
22+
*
23+
* Input: s = "pwwkew"
24+
* Output: 3
25+
* Explanation: The answer is "wke", with the length of 3.
26+
* Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* 0 <= s.length <= 5 * 10^4
32+
* s consists of English letters, digits, symbols and spaces.
33+
*
34+
*/
35+
pub struct Solution {}
36+
37+
// problem: https://leetcode.com/problems/longest-substring-without-repeating-characters/
38+
// discuss: https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/?currentPage=1&orderBy=most_votes&query=
39+
40+
// submission codes start here
41+
42+
impl Solution {
43+
pub fn length_of_longest_substring(s: String) -> i32 {
44+
let chars = s.chars().collect::<Vec<char>>();
45+
let mut map = HashMap::new();
46+
let mut ans = 0;
47+
let mut left = 0;
48+
for i in 0..chars.len() {
49+
if map.contains_key(&chars[i]) {
50+
left = max(map[&chars[i]] + 1, left);
51+
}
52+
map.insert(chars[i], i);
53+
ans = max(ans, i - left + 1);
54+
}
55+
ans as i32
56+
}
57+
}
58+
59+
// submission codes end
60+
61+
#[cfg(test)]
62+
mod tests {
63+
use super::*;
64+
65+
#[test]
66+
fn test_3() {
67+
println!("{}", Solution::length_of_longest_substring("abba".to_string()));
68+
}
69+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* [4] Median of Two Sorted Arrays
3+
*
4+
* Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
5+
* The overall run time complexity should be O(log (m+n)).
6+
*
7+
* <strong class="example">Example 1:
8+
*
9+
* Input: nums1 = [1,3], nums2 = [2]
10+
* Output: 2.00000
11+
* Explanation: merged array = [1,2,3] and median is 2.
12+
*
13+
* <strong class="example">Example 2:
14+
*
15+
* Input: nums1 = [1,2], nums2 = [3,4]
16+
* Output: 2.50000
17+
* Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
18+
*
19+
*
20+
* Constraints:
21+
*
22+
* nums1.length == m
23+
* nums2.length == n
24+
* 0 <= m <= 1000
25+
* 0 <= n <= 1000
26+
* 1 <= m + n <= 2000
27+
* -10^6 <= nums1[i], nums2[i] <= 10^6
28+
*
29+
*/
30+
pub struct Solution {}
31+
32+
// problem: https://leetcode.com/problems/median-of-two-sorted-arrays/
33+
// discuss: https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/?currentPage=1&orderBy=most_votes&query=
34+
35+
// submission codes start here
36+
37+
impl Solution {
38+
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
39+
let size = nums1.len() + nums2.len();
40+
if size == 1 { return if nums1.len() == 1 { nums1[0] as f64 } else { nums2[0] as f64 } }
41+
let mid = size / 2;
42+
let (mut n1, mut n2, mut i) = (0, 0, 0);
43+
let mut pre = -1;
44+
let mut cur = -1;
45+
while i <= mid && n1 < nums1.len() && n2 < nums2.len() {
46+
pre = cur;
47+
if nums1[n1] < nums2[n2] {
48+
cur = nums1[n1];
49+
n1 += 1
50+
} else {
51+
cur = nums2[n2];
52+
n2 += 1
53+
}
54+
i += 1;
55+
}
56+
while i <= mid && n1 < nums1.len() {
57+
pre = cur;
58+
cur = nums1[n1];
59+
n1 += 1;
60+
i += 1;
61+
}
62+
while i <= mid && n2 < nums2.len() {
63+
pre = cur;
64+
cur = nums2[n2];
65+
n2 += 1;
66+
i += 1;
67+
}
68+
if size % 2 == 0 { (pre + cur) as f64/2.0 } else { cur as f64 }
69+
}
70+
}
71+
72+
// submission codes end
73+
74+
#[cfg(test)]
75+
mod tests {
76+
use super::*;
77+
78+
#[test]
79+
fn test_4() {}
80+
}

0 commit comments

Comments
 (0)