Skip to content

Commit 903c9c7

Browse files
committed
update basic list
1 parent 1473e07 commit 903c9c7

12 files changed

+299
-138
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4444
| [] | [blind75.md](./list/blind75.md) | 77/77 | - |
4545
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4646
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
47-
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 9/128 | 3 vips |
47+
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
4848
| [🔲] | [9c-top.md](./list/9c-top.md) | 5/48 | - |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/56 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
@@ -845,13 +845,14 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
845845

846846
## String
847847

848-
| Link | Problem(1) | Solution | Tag | Time | Space | Ref |
848+
| Link | Problem(2) | Solution | Tag | Time | Space | Ref |
849849
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
850+
| [Leetcode-796](https://leetcode.com/problems/rotate-string/) | Rotate String | [c++](./leetcode/796.rotate-string.cpp), [python3](./leetcode/796.rotate-string.py) | String | O\(N\) | O\(N\) | - |
850851
| [Lintcode-1790](https://www.lintcode.com/problem/rotate-string-ii/) | Rotate String II | [c++](./lintcode/1790.rotate-string-ii.cpp), [python3](./lintcode/1790.rotate-string-ii.py) | String | O\(N\) | O\(N\) | - |
851852

852853
## Other
853854

854-
| Link | Problem(131) | Solution | Tag | Time | Space | Ref |
855+
| Link | Problem(130) | Solution | Tag | Time | Space | Ref |
855856
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
856857
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
857858
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -952,7 +953,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
952953
| [Leetcode-657](https://leetcode.com/problems/robot-return-to-origin/) | Robot Return To Origin | [python3](./leetcode/657.robot-return-to-origin.py) | Other | \- | \- | - |
953954
| [Leetcode-13](https://leetcode.com/problems/roman-to-integer/) | Roman To Integer | [c++](./leetcode/13.roman-to-integer.cpp), [python3](./leetcode/13.roman-to-integer.py) | Other | \- | \- | - |
954955
| [Leetcode-61](https://leetcode.com/problems/rotate-list/) | Rotate List | [python3](./leetcode/61.rotate-list.py) | Other | \- | \- | - |
955-
| [Leetcode-796](https://leetcode.com/problems/rotate-string/) | Rotate String | [c++](./leetcode/796.rotate-string.cpp), [python3](./leetcode/796.rotate-string.py) | Other | \- | \- | - |
956956
| [Leetcode-702](https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/) | Search In A Sorted Array Of Unknown Size | [c++](./leetcode/702.search-in-a-sorted-array-of-unknown-size.cpp), [python3](./leetcode/702.search-in-a-sorted-array-of-unknown-size.py) | Other | \- | \- | - |
957957
| [Leetcode-444](https://leetcode.com/problems/sequence-reconstruction/) | Sequence Reconstruction | [c++](./leetcode/444.sequence-reconstruction.cpp), [python3](./leetcode/444.sequence-reconstruction.py) | Other | \- | \- | - |
958958
| [Leetcode-773](https://leetcode.com/problems/sliding-puzzle/) | Sliding Puzzle | [python3](./leetcode/773.sliding-puzzle.py) | Other | \- | \- | - |

leetcode/33.search-in-rotated-sorted-array.cpp

+44-12
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,60 @@
3333
class Solution {
3434
public:
3535
int search(vector<int>& nums, int target) {
36-
int start = 0;
37-
int end = nums.size() - 1;
36+
int left = 0;
37+
int right = nums.size() - 1;
3838

39-
while (start < end) {
40-
int mid = start + (end - start) / 2;
39+
while (left < right) {
40+
int mid = left + (right - left) / 2;
4141

42-
if (nums[mid] >= nums[start]) {
43-
if (target >= nums[start] && target <= nums[mid]) {
44-
end = mid;
42+
if (nums[mid] >= nums[left]) {
43+
if (target >= nums[left] && target <= nums[mid]) {
44+
right = mid;
4545
} else {
46-
start = mid + 1;
46+
left = mid + 1;
4747
}
4848
} else {
49-
if (target <= nums[mid] || target >= nums[start]) {
50-
end = mid;
49+
if (target <= nums[mid] || target >= nums[left]) {
50+
right = mid;
5151
} else {
52-
start = mid + 1;
52+
left = mid + 1;
5353
}
5454

5555
}
5656
}
5757

58-
return (nums[start] == target) ? start : -1;
58+
return (nums[left] == target) ? left : -1;
59+
}
60+
};
61+
62+
class Solution {
63+
public:
64+
int search(vector<int>& nums, int target) {
65+
int n = nums.size();
66+
int left = 0;
67+
int right = n - 1;
68+
69+
while (left < right) {
70+
int mid = left + (right - left) / 2;
71+
if (nums[mid] == target) {
72+
return mid;
73+
}
74+
75+
if (target > nums[right]) {
76+
if (nums[mid] > nums[right] && nums[mid] < target) {
77+
left = mid + 1;
78+
} else {
79+
right = mid;
80+
}
81+
} else {
82+
if (nums[mid] <= nums[right] && nums[mid] >= target) {
83+
right = mid;
84+
} else {
85+
left = mid + 1;
86+
}
87+
}
88+
}
89+
90+
return nums[left] == target ? left : -1;
5991
}
6092
};

leetcode/33.search-in-rotated-sorted-array.py

+36-12
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,46 @@
3232

3333
class Solution:
3434
def search(self, nums: List[int], target: int) -> int:
35-
start = 0
36-
end = len(nums) - 1
35+
left = 0
36+
right = len(nums) - 1
3737

38-
while start < end:
38+
while left < right:
3939

40-
mid = start + (end - start) // 2
40+
mid = left + (right - left) // 2
4141

42-
if nums[mid] >= nums[end]:
43-
if nums[end] <= target <= nums[mid]:
44-
end = mid
42+
if nums[mid] > nums[right]:
43+
if nums[right] < target <= nums[mid]:
44+
right = mid
4545
else:
46-
start = mid + 1
46+
left = mid + 1
4747
else:
48-
if nums[mid] < target < nums[end]:
49-
start = mid + 1
48+
if nums[mid] < target <= nums[right]:
49+
left = mid + 1
5050
else:
51-
end = mid
51+
right = mid
5252

53-
return start if nums[start] == target else -1
53+
return left if nums[left] == target else -1
54+
55+
class Solution:
56+
def search(self, nums: List[int], target: int) -> int:
57+
n = len(nums)
58+
left = 0
59+
right = n - 1
60+
61+
while left < right:
62+
mid = left + (right - left) // 2
63+
if nums[mid] == target:
64+
return mid
65+
66+
if target > nums[right]:
67+
if nums[mid] > nums[right] and nums[mid] < target:
68+
left = mid + 1
69+
else:
70+
right = mid
71+
else:
72+
if nums[mid] <= nums[right] and nums[mid] >= target:
73+
right = mid
74+
else:
75+
left = mid + 1
76+
77+
return left if nums[left] == target else -1

leetcode/796.rotate-string.cpp

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1+
// Tag: String, String Matching
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
8+
// A shift on s consists of moving the leftmost character of s to the rightmost position.
9+
//
10+
// For example, if s = "abcde", then it will be "bcdea" after one shift.
11+
//
12+
//  
13+
// Example 1:
14+
// Input: s = "abcde", goal = "cdeab"
15+
// Output: true
16+
// Example 2:
17+
// Input: s = "abcde", goal = "abced"
18+
// Output: false
19+
//
20+
//  
21+
// Constraints:
22+
//
23+
// 1 <= s.length, goal.length <= 100
24+
// s and goal consist of lowercase English letters.
25+
//
26+
//
27+
128
class Solution {
229
public:
3-
bool rotateString(string A, string B) {
4-
return A.size() == B.size() && (A + A).find(B) != string::npos;
30+
bool rotateString(string s, string goal) {
31+
532
}
6-
};
33+
};

leetcode/796.rotate-string.py

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
class Solution(object):
2-
def rotateString(self, A, B):
3-
"""
4-
:type A: str
5-
:type B: str
6-
:rtype: bool
7-
"""
8-
9-
return len(A) == len(B) and (B in A + A)
1+
# Tag: String, String Matching
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
8+
# A shift on s consists of moving the leftmost character of s to the rightmost position.
9+
#
10+
# For example, if s = "abcde", then it will be "bcdea" after one shift.
11+
#
12+
#  
13+
# Example 1:
14+
# Input: s = "abcde", goal = "cdeab"
15+
# Output: true
16+
# Example 2:
17+
# Input: s = "abcde", goal = "abced"
18+
# Output: false
19+
#
20+
#  
21+
# Constraints:
22+
#
23+
# 1 <= s.length, goal.length <= 100
24+
# s and goal consist of lowercase English letters.
25+
#
26+
#
27+
28+
class Solution:
29+
def rotateString(self, s: str, goal: str) -> bool:
30+
return len(s) == len(goal) and s in goal + goal

leetcode/81.search-in-rotated-sorted-array-ii.cpp

+52-14
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,71 @@ class Solution {
3232
public:
3333
bool search(vector<int>& nums, int target) {
3434

35-
int start = 0;
36-
int end = nums.size() - 1;
35+
int left = 0;
36+
int right = nums.size() - 1;
3737

38-
while (start < end) {
38+
while (left < right) {
3939

40-
if (nums[start] == nums[end]) {
41-
end --;
40+
if (nums[left] == nums[right]) {
41+
right --;
4242
continue;
4343
}
4444

45-
int mid = start + (end - start) / 2;
46-
if (nums[mid] >= nums[start]) {
47-
if (target >= nums[start] && target <= nums[mid]) {
48-
end = mid;
45+
int mid = left + (right - left) / 2;
46+
if (nums[mid] >= nums[left]) {
47+
if (target >= nums[left] && target <= nums[mid]) {
48+
right = mid;
4949
} else {
50-
start = mid + 1;
50+
left = mid + 1;
5151
}
5252

5353
} else {
54-
if (target > nums[end] || target <= nums[mid]) {
55-
end = mid;
54+
if (target > nums[right] || target <= nums[mid]) {
55+
right = mid;
5656
} else {
57-
start = mid + 1;
57+
left = mid + 1;
5858
}
5959
}
6060
}
6161

62-
return nums[start] == target;
62+
return nums[left] == target;
63+
}
64+
};
65+
66+
67+
class Solution {
68+
public:
69+
bool search(vector<int>& nums, int target) {
70+
int n = nums.size();
71+
int left = 0;
72+
int right = n - 1;
73+
74+
while (left < right) {
75+
int mid = left + (right - left) / 2;
76+
if (nums[mid] == target) {
77+
return true;
78+
}
79+
80+
if (nums[left] == nums[right]) {
81+
left += 1;
82+
continue;
83+
}
84+
85+
if (target > nums[right]) {
86+
if (nums[mid] > nums[right] && nums[mid] < target) {
87+
left = mid + 1;
88+
} else {
89+
right = mid;
90+
}
91+
} else {
92+
if (nums[mid] <= nums[right] && nums[mid] >= target) {
93+
right = mid;
94+
} else {
95+
left = mid + 1;
96+
}
97+
}
98+
}
99+
100+
return nums[left] == target;
63101
}
64102
};

0 commit comments

Comments
 (0)