Skip to content

Commit 4e3577c

Browse files
committed
feat(python): solutions
1 parent a512478 commit 4e3577c

14 files changed

+577
-62
lines changed

JavaScript/0034. Find First and Last Position of Element in Sorted Array.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const searchRange3 = (nums, target) => {
5555

5656
while (l < r) {
5757
const m = Math.ceil((l + r) / 2); // note using Math.ceil
58-
5958
if (nums[m] > target) r = m - 1;
6059
else l = m;
6160
}
@@ -86,7 +85,6 @@ const searchRange = (nums, target) => {
8685

8786
while (l < r) {
8887
const m = Math.ceil((l + r) / 2); // note using Math.ceil
89-
9088
if (nums[m] > target) r = m - 1;
9189
else l = m;
9290
}

JavaScript/0038. Count and Say.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,26 @@
2828
* @return {string}
2929
*/
3030
const countAndSay = (n) => {
31-
let res = '1';
32-
for (let i = 1; i < n; i++) {
33-
res = say(res);
31+
let s = '1';
32+
for (let i = 0; i < n - 1; i++) {
33+
s = say(s);
3434
}
35-
return res;
35+
return s;
3636
};
3737

3838
const say = (s) => {
3939
let res = '';
4040
let count = 0;
41-
let num = s[0];
41+
let n = s[0];
4242

4343
for (let i = 0; i < s.length; i++) {
44-
if (s[i] === num) {
44+
if (s[i] === n) {
4545
count++;
4646
} else {
4747
res += count + s[i - 1];
4848
count = 1;
49-
num = s[i];
49+
n = s[i];
5050
}
5151
}
52-
return res + count + num;
52+
return res + count + n;
5353
};

JavaScript/0041. First Missing Positive.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
// i++
4848
// i = 3
4949
// i++
50+
// i = 4
5051
const firstMissingPositive = (nums) => {
5152
const swap = (i, j) => [nums[i], nums[j]] = [nums[j], nums[i]];
5253

JavaScript/0069. Sqrt(x).js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ const mySqrt = (x) => {
5757
const m = ~~((l + r) / 2);
5858

5959
if (m * m === x) return m;
60-
61-
if (m * m < x) l = m + 1;
60+
else if (m * m < x) l = m + 1;
6261
else r = m - 1;
6362
}
6463
return r;

Python/0005. Longest Palindromic Substring.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ class Solution:
4949
def longestPalindrome(self, s: str) -> str:
5050
res = ""
5151
for i in range(len(s)):
52-
s1 = self.expend_from_center(s, i, i)
52+
s1 = Solution.expend_from_center(s, i, i)
5353
if len(s1) > len(res):
5454
res = s1
55-
s2 = self.expend_from_center(s, i, i + 1)
55+
s2 = Solution.expend_from_center(s, i, i + 1)
5656
if len(s2) > len(res):
5757
res = s2
5858
return res
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
2+
# The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.
3+
# Return the quotient after dividing dividend by divisor.
4+
#
5+
# Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For this problem, if the quotient is strictly greater than 2^31 - 1, then return 2^31 - 1, and if the quotient is strictly less than -2^31, then return -2^31.
6+
#
7+
# Example 1:
8+
#
9+
# Input: dividend = 10, divisor = 3
10+
# Output: 3
11+
# Explanation: 10/3 = 3.33333.. which is truncated to 3.
12+
#
13+
# Example 2:
14+
#
15+
# Input: dividend = 7, divisor = -3
16+
# Output: -2
17+
# Explanation: 7/-3 = -2.33333.. which is truncated to -2.
18+
#
19+
# Constraints:
20+
#
21+
# -2^31 <= dividend, divisor <= 2^31 - 1
22+
# divisor != 0
23+
24+
25+
class Solution:
26+
def divide(self, dividend: int, divisor: int) -> int:
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# There is an integer array nums sorted in ascending order (with distinct values).
2+
# Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
3+
# Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
4+
# You must write an algorithm with O(log n) runtime complexity.
5+
#
6+
# Example 1:
7+
#
8+
# Input: nums = [4,5,6,7,0,1,2], target = 0
9+
# Output: 4
10+
#
11+
# Example 2:
12+
#
13+
# Input: nums = [4,5,6,7,0,1,2], target = 3
14+
# Output: -1
15+
#
16+
# Example 3:
17+
#
18+
# Input: nums = [1], target = 0
19+
# Output: -1
20+
#
21+
# Constraints:
22+
#
23+
# 1 <= nums.length <= 5000
24+
# -10^4 <= nums[i] <= 10^4
25+
# All values of nums are unique.
26+
# nums is an ascending array that is possibly rotated.
27+
# -10^4 <= target <= 10^4
28+
29+
30+
# 1) Binary search
31+
# https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/273622/Javascript-Simple-O(log-N)-Binary-Search-Solution
32+
#
33+
# Time O(log n)
34+
# Space O(1)
35+
#
36+
# e.g. [1, 2, 3, 4, 5, 6, 7]
37+
#
38+
# When you divide the rotated array into two halves, using mid index, at least one of them should remain sorted ALWAYS.
39+
#
40+
# [3, 4, 5] [6, 7, 1, 2] the left side remains sorted
41+
# [6, 7, 1] [2, 3, 4, 5] the right side remains sorted
42+
# [1, 2, 3] [4, 5, 6, 7] Both sides remain sorted.
43+
#
44+
# If you know one side is sorted, the rest of logic becomes very simple.
45+
# If one side is sorted, check if the target is in the boundary, otherwise it's on the other side.
46+
#
47+
# IF smallest <= target <= biggest
48+
# then target is here
49+
# ELSE
50+
# then target is on the other side
51+
class Solution:
52+
def search(self, nums: List[int], target: int) -> int:
53+
l = 0
54+
r = len(nums) - 1
55+
while l <= r:
56+
m = (l + r) // 2
57+
if nums[m] == target:
58+
return m
59+
60+
# When dividing the rotated array into two halves, one must be sorted
61+
# Check if the left side is sorted
62+
if nums[l] <= nums[m]:
63+
if nums[l] <= target <= nums[m]:
64+
r = m - 1 # target is in the left
65+
else:
66+
l = m + 1 # target is in the right
67+
else:
68+
if nums[m] <= target <= nums[r]:
69+
l = m + 1 # target is in the right
70+
else:
71+
r = m - 1 # target is in the left
72+
return -1
73+
74+
75+
# 2) Binary search, similar to 1)
76+
class Solution:
77+
def search(self, nums: List[int], target: int) -> int:
78+
l = 0
79+
r = len(nums) - 1
80+
while l + 1 < r:
81+
m = (l + r) // 2
82+
if nums[m] == target:
83+
return m
84+
if nums[l] < nums[m]: # e.g. 4, 5, 6, 7
85+
if nums[l] <= target <= nums[m]:
86+
r = m
87+
else:
88+
l = m
89+
else: # e.g. 7, 0, 1, 2
90+
if nums[m] <= target <= nums[r]:
91+
l = m
92+
else:
93+
r = m
94+
if nums[l] == target:
95+
return l
96+
if nums[r] == target:
97+
return r
98+
return -1
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
2+
# If target is not found in the array, return [-1, -1].
3+
# You must write an algorithm with O(log n) runtime complexity.
4+
#
5+
# Example 1:
6+
#
7+
# Input: nums = [5,7,7,8,8,10], target = 8
8+
# Output: [3,4]
9+
#
10+
# Example 2:
11+
#
12+
# Input: nums = [5,7,7,8,8,10], target = 6
13+
# Output: [-1,-1]
14+
#
15+
# Example 3:
16+
#
17+
# Input: nums = [], target = 0
18+
# Output: [-1,-1]
19+
#
20+
# Constraints:
21+
#
22+
# 0 <= nums.length <= 10^5
23+
# -10^9 <= nums[i] <= 10^9
24+
# nums is a non-decreasing array.
25+
# -10^9 <= target <= 10^9
26+
27+
28+
# 1) Brute force / Linear scan
29+
# Time O(n)
30+
# Space O(1)
31+
32+
33+
# 2) Binary search
34+
# Time O(log n)
35+
# Space O(1)
36+
class Solution:
37+
def searchRange(self, nums: List[int], target: int) -> List[int]:
38+
l = Solution.lower_bound(nums, target)
39+
r = Solution.upper_bound(nums, target) - 1
40+
if l <= r:
41+
return [l, r]
42+
return [-1, -1]
43+
44+
@staticmethod
45+
def lower_bound(nums: List[int], target: int) -> int:
46+
l = 0
47+
r = len(nums)
48+
while l < r:
49+
m = (l + r) // 2
50+
if nums[m] < target:
51+
l = m + 1
52+
else:
53+
r = m
54+
return l
55+
56+
@staticmethod
57+
def upper_bound(nums: List[int], target: int) -> int:
58+
l = 0
59+
r = len(nums)
60+
while l < r:
61+
m = (l + r) // 2
62+
if nums[m] <= target:
63+
l = m + 1
64+
else:
65+
r = m
66+
return l
67+
68+
69+
# 3) Binary search, similar to 2)
70+
class Solution:
71+
def searchRange(self, nums: List[int], target: int) -> List[int]:
72+
l = Solution.lower_bound(nums, target)
73+
r = Solution.lower_bound(nums, target + 1) - 1
74+
if l <= r:
75+
return [l, r]
76+
return [-1, -1]
77+
78+
@staticmethod
79+
def lower_bound(nums: List[int], target: int) -> int:
80+
l = 0
81+
r = len(nums)
82+
while l < r:
83+
m = (l + r) // 2
84+
if nums[m] < target:
85+
l = m + 1
86+
else:
87+
r = m
88+
return l

0 commit comments

Comments
 (0)