Skip to content

Commit 063898f

Browse files
committed
init all problems
1 parent ec2ac92 commit 063898f

File tree

2,108 files changed

+128601
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,108 files changed

+128601
-0
lines changed

src/problem/p0001_two_sum.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* [1] Two Sum
3+
*
4+
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
5+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
* You can return the answer in any order.
7+
*
8+
* <strong class="example">Example 1:
9+
*
10+
* Input: nums = [2,7,11,15], target = 9
11+
* Output: [0,1]
12+
* Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
13+
*
14+
* <strong class="example">Example 2:
15+
*
16+
* Input: nums = [3,2,4], target = 6
17+
* Output: [1,2]
18+
*
19+
* <strong class="example">Example 3:
20+
*
21+
* Input: nums = [3,3], target = 6
22+
* Output: [0,1]
23+
*
24+
*
25+
* Constraints:
26+
*
27+
* 2 <= nums.length <= 10^4
28+
* -10^9 <= nums[i] <= 10^9
29+
* -10^9 <= target <= 10^9
30+
* Only one valid answer exists.
31+
*
32+
*
33+
* Follow-up: Can you come up with an algorithm that is less than O(n^2)<font face="monospace"> </font>time complexity?
34+
*/
35+
pub struct Solution {}
36+
37+
// problem: https://leetcode.com/problems/two-sum/
38+
// discuss: https://leetcode.com/problems/two-sum/discuss/?currentPage=1&orderBy=most_votes&query=
39+
40+
// submission codes start here
41+
42+
impl Solution {
43+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
44+
vec![]
45+
}
46+
}
47+
48+
// submission codes end
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
fn test_1() {
56+
}
57+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
Some(Box::new(ListNode::new(0)))
58+
}
59+
}
60+
61+
// submission codes end
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
fn test_2() {
69+
}
70+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* [3] Longest Substring Without Repeating Characters
3+
*
4+
* Given a string s, find the length of the longest <span data-keyword="substring-nonempty">substring</span> without repeating characters.
5+
*
6+
* <strong class="example">Example 1:
7+
*
8+
* Input: s = "abcabcbb"
9+
* Output: 3
10+
* Explanation: The answer is "abc", with the length of 3.
11+
*
12+
* <strong class="example">Example 2:
13+
*
14+
* Input: s = "bbbbb"
15+
* Output: 1
16+
* Explanation: The answer is "b", with the length of 1.
17+
*
18+
* <strong class="example">Example 3:
19+
*
20+
* Input: s = "pwwkew"
21+
* Output: 3
22+
* Explanation: The answer is "wke", with the length of 3.
23+
* Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
24+
*
25+
*
26+
* Constraints:
27+
*
28+
* 0 <= s.length <= 5 * 10^4
29+
* s consists of English letters, digits, symbols and spaces.
30+
*
31+
*/
32+
pub struct Solution {}
33+
34+
// problem: https://leetcode.com/problems/longest-substring-without-repeating-characters/
35+
// discuss: https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/?currentPage=1&orderBy=most_votes&query=
36+
37+
// submission codes start here
38+
39+
impl Solution {
40+
pub fn length_of_longest_substring(s: String) -> i32 {
41+
0
42+
}
43+
}
44+
45+
// submission codes end
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
51+
#[test]
52+
fn test_3() {
53+
}
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
0f64
40+
}
41+
}
42+
43+
// submission codes end
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
#[test]
50+
fn test_4() {
51+
}
52+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* [5] Longest Palindromic Substring
3+
*
4+
* Given a string s, return the longest <span data-keyword="palindromic-string">palindromic</span> <span data-keyword="substring-nonempty">substring</span> in s.
5+
*
6+
* <strong class="example">Example 1:
7+
*
8+
* Input: s = "babad"
9+
* Output: "bab"
10+
* Explanation: "aba" is also a valid answer.
11+
*
12+
* <strong class="example">Example 2:
13+
*
14+
* Input: s = "cbbd"
15+
* Output: "bb"
16+
*
17+
*
18+
* Constraints:
19+
*
20+
* 1 <= s.length <= 1000
21+
* s consist of only digits and English letters.
22+
*
23+
*/
24+
pub struct Solution {}
25+
26+
// problem: https://leetcode.com/problems/longest-palindromic-substring/
27+
// discuss: https://leetcode.com/problems/longest-palindromic-substring/discuss/?currentPage=1&orderBy=most_votes&query=
28+
29+
// submission codes start here
30+
31+
impl Solution {
32+
pub fn longest_palindrome(s: String) -> String {
33+
String::new()
34+
}
35+
}
36+
37+
// submission codes end
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
43+
#[test]
44+
fn test_5() {
45+
}
46+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* [6] Zigzag Conversion
3+
*
4+
* The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
5+
*
6+
* P A H N
7+
* A P L S I I G
8+
* Y I R
9+
*
10+
* And then read line by line: "PAHNAPLSIIGYIR"
11+
* Write the code that will take a string and make this conversion given a number of rows:
12+
*
13+
* string convert(string s, int numRows);
14+
*
15+
*
16+
* <strong class="example">Example 1:
17+
*
18+
* Input: s = "PAYPALISHIRING", numRows = 3
19+
* Output: "PAHNAPLSIIGYIR"
20+
*
21+
* <strong class="example">Example 2:
22+
*
23+
* Input: s = "PAYPALISHIRING", numRows = 4
24+
* Output: "PINALSIGYAHRPI"
25+
* Explanation:
26+
* P I N
27+
* A L S I G
28+
* Y A H R
29+
* P I
30+
*
31+
* <strong class="example">Example 3:
32+
*
33+
* Input: s = "A", numRows = 1
34+
* Output: "A"
35+
*
36+
*
37+
* Constraints:
38+
*
39+
* 1 <= s.length <= 1000
40+
* s consists of English letters (lower-case and upper-case), ',' and '.'.
41+
* 1 <= numRows <= 1000
42+
*
43+
*/
44+
pub struct Solution {}
45+
46+
// problem: https://leetcode.com/problems/zigzag-conversion/
47+
// discuss: https://leetcode.com/problems/zigzag-conversion/discuss/?currentPage=1&orderBy=most_votes&query=
48+
49+
// submission codes start here
50+
51+
impl Solution {
52+
pub fn convert(s: String, num_rows: i32) -> String {
53+
String::new()
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_6() {
65+
}
66+
}

0 commit comments

Comments
 (0)