Skip to content

Commit fa115e7

Browse files
committed
Added readme files.
1 parent e8d0924 commit fa115e7

File tree

14 files changed

+703
-0
lines changed

14 files changed

+703
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
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.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
5\. Longest Palindromic Substring
2+
3+
Medium
4+
5+
Given a string `s`, return _the longest palindromic substring_ in `s`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "babad"
10+
11+
**Output:** "bab" **Note:** "aba" is also a valid answer.
12+
13+
**Example 2:**
14+
15+
**Input:** s = "cbbd"
16+
17+
**Output:** "bb"
18+
19+
**Example 3:**
20+
21+
**Input:** s = "a"
22+
23+
**Output:** "a"
24+
25+
**Example 4:**
26+
27+
**Input:** s = "ac"
28+
29+
**Output:** "a"
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 1000`
34+
* `s` consist of only digits and English letters.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
6\. Zigzag Conversion
2+
3+
Medium
4+
5+
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)
6+
7+
P A H N A P L S I I G Y I R
8+
9+
And then read line by line: `"PAHNAPLSIIGYIR"`
10+
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+
**Example 1:**
16+
17+
**Input:** s = "PAYPALISHIRING", numRows = 3
18+
19+
**Output:** "PAHNAPLSIIGYIR"
20+
21+
**Example 2:**
22+
23+
**Input:** s = "PAYPALISHIRING", numRows = 4
24+
25+
**Output:** "PINALSIGYAHRPI"
26+
27+
**Explanation:** P I N A L S I G Y A H R P I
28+
29+
**Example 3:**
30+
31+
**Input:** s = "A", numRows = 1
32+
33+
**Output:** "A"
34+
35+
**Constraints:**
36+
37+
* `1 <= s.length <= 1000`
38+
* `s` consists of English letters (lower-case and upper-case), `','` and `'.'`.
39+
* `1 <= numRows <= 1000`
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
7\. Reverse Integer
2+
3+
Medium
4+
5+
Given a signed 32-bit integer `x`, return `x` _with its digits reversed_. If reversing `x` causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return `0`.
6+
7+
**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).**
8+
9+
**Example 1:**
10+
11+
**Input:** x = 123
12+
13+
**Output:** 321
14+
15+
**Example 2:**
16+
17+
**Input:** x = -123
18+
19+
**Output:** -321
20+
21+
**Example 3:**
22+
23+
**Input:** x = 120
24+
25+
**Output:** 21
26+
27+
**Example 4:**
28+
29+
**Input:** x = 0
30+
31+
**Output:** 0
32+
33+
**Constraints:**
34+
35+
* <code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
8\. String to Integer (atoi)
2+
3+
Medium
4+
5+
Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer (similar to C/C++'s `atoi` function).
6+
7+
The algorithm for `myAtoi(string s)` is as follows:
8+
9+
1. Read in and ignore any leading whitespace.
10+
2. Check if the next character (if not already at the end of the string) is `'-'` or `'+'`. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
11+
3. Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
12+
4. Convert these digits into an integer (i.e. `"123" -> 123`, `"0032" -> 32`). If no digits were read, then the integer is `0`. Change the sign as necessary (from step 2).
13+
5. If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then clamp the integer so that it remains in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be clamped to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be clamped to <code>2<sup>31</sup> - 1</code>.
14+
6. Return the integer as the final result.
15+
16+
**Note:**
17+
18+
* Only the space character `' '` is considered a whitespace character.
19+
* **Do not ignore** any characters other than the leading whitespace or the rest of the string after the digits.
20+
21+
**Example 1:**
22+
23+
**Input:** s = "42"
24+
25+
**Output:** 42
26+
27+
**Explanation:** The underlined characters are what is read in, the caret is the current reader position.
28+
29+
Step 1: "42" (no characters read because there is no leading whitespace)
30+
^
31+
Step 2: "42" (no characters read because there is neither a '-' nor '+')
32+
^
33+
Step 3: "42" ("42" is read in)
34+
^
35+
36+
The parsed integer is 42. Since 42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 42.
37+
38+
**Example 2:**
39+
40+
**Input:** s = " -42"
41+
42+
**Output:** -42
43+
44+
**Explanation:**
45+
46+
Step 1: " -42" (leading whitespace is read and ignored)
47+
^
48+
Step 2: " -42" ('-' is read, so the result should be negative)
49+
^
50+
Step 3: " -42" ("42" is read in)
51+
^
52+
The parsed integer is -42.
53+
54+
Since -42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is -42.
55+
56+
**Example 3:**
57+
58+
**Input:** s = "4193 with words"
59+
60+
**Output:** 4193
61+
62+
**Explanation:**
63+
64+
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
65+
^
66+
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
67+
^
68+
Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit)
69+
^
70+
The parsed integer is 4193.
71+
72+
Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193.
73+
74+
**Example 4:**
75+
76+
**Input:** s = "words and 987"
77+
78+
**Output:** 0
79+
80+
**Explanation:**
81+
82+
Step 1: "words and 987" (no characters read because there is no leading whitespace)
83+
^
84+
Step 2: "words and 987" (no characters read because there is neither a '-' nor '+')
85+
^
86+
Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w')
87+
^
88+
The parsed integer is 0 because no digits were read.
89+
90+
Since 0 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 0.
91+
92+
**Example 5:**
93+
94+
**Input:** s = "-91283472332"
95+
96+
**Output:** -2147483648
97+
98+
**Explanation:**
99+
100+
Step 1: "-91283472332" (no characters read because there is no leading whitespace)
101+
^
102+
Step 2: "-91283472332" ('-' is read, so the result should be negative)
103+
^
104+
Step 3: "-91283472332" ("91283472332" is read in)
105+
^
106+
The parsed integer is -91283472332.
107+
108+
Since -91283472332 is less than the lower bound of the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is clamped to -2<sup>31</sup> = -2147483648.
109+
110+
**Constraints:**
111+
112+
* `0 <= s.length <= 200`
113+
* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`.

0 commit comments

Comments
 (0)