Skip to content

Commit bf7b9bd

Browse files
committed
update
1 parent 7bb83fc commit bf7b9bd

37 files changed

+448
-12
lines changed

Diff for: 0001-two-sum/0001-two-sum.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#利用哈希表,空间换时间
1+
# 利用哈希表,空间换时间
22
class Solution:
33
def twoSum(self, nums: List[int], target: int) -> List[int]:
4-
n=len(nums)
5-
mapper={}
4+
n = len(nums)
5+
mapper = {}
66
for i in range(n):
7-
if (target-nums[i] in mapper):
8-
return [mapper[target-nums[i]],i]
7+
if (target - nums[i] in mapper):
8+
return [mapper[target - nums[i]], i]
99
else:
10-
mapper[nums[i]]=i
11-
return []
10+
mapper[nums[i]] = i
11+
return []

Diff for: 0005-longest-palindromic-substring.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 以每个字母或相邻两个字母为中心计算回文字符串长度,再比较
2+
class Solution:
3+
def longestPalindrome(self, s: str) -> str:
4+
n = len(s)
5+
if n == 0:
6+
return ""
7+
res = s[0]
8+
9+
def func(s: str, i: int, j: int):
10+
while (i >= 0 and j < len(s) and s[i] == s[j]):
11+
i -= 1
12+
j += 1
13+
return s[i + 1:j]
14+
15+
for i in range(n - 1):
16+
s1 = func(s, i, i)
17+
s2 = func(s, i, i + 1)
18+
if (max(len(s1), len(s2)) > len(res)):
19+
if (len(s1) > len(s2)):
20+
res = s1
21+
else:
22+
res = s2
23+
return res

Diff for: 0006-zigzag-conversion.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 压缩矩阵排列,并以2n-2为周期
2+
class Solution:
3+
def convert(self, s: str, numRows: int) -> str:
4+
l = len(s)
5+
if numRows == 1 or numRows >= l:
6+
return s
7+
res = [['' for i in range(2 * (l // (2 * numRows - 2) + 1))] for j in range(numRows)]
8+
9+
for i in range(l):
10+
a = 2 * (i // (2 * numRows - 2))
11+
b = i % (2 * numRows - 2)
12+
if (b >= numRows):
13+
a += 1
14+
b = (2 * numRows - 2 - b)
15+
res[b][a] = s[i]
16+
return ''.join(ch for row in res for ch in row if ch)
File renamed without changes.

Diff for: 0009-palindrome-number/0009-palindrome-number.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 反转数之后比较
2+
class Solution:
3+
def isPalindrome(self, x: int) -> bool:
4+
if x < 0:
5+
return False
6+
if x == 0:
7+
return True
8+
x1 = x
9+
num = 0
10+
while x > 0:
11+
num = num * 10 + (x % 10)
12+
x = x // 10
13+
return x1 == num

Diff for: 0015-3sum.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#排序之后双指针
2+
class Solution:
3+
def threeSum(self, nums: List[int]) -> List[List[int]]:
4+
nums.sort()
5+
res = []
6+
n = len(nums)
7+
8+
for i in range(n - 2):
9+
if (i > 0 and nums[i] == nums[i - 1]): #去重
10+
continue
11+
left = i + 1
12+
right = n - 1
13+
while (left < right):
14+
if (nums[i] + nums[left] + nums[right] < 0):
15+
left += 1
16+
elif (nums[i] + nums[left] + nums[right] > 0):
17+
right -= 1
18+
else:
19+
res.append([nums[i], nums[left], nums[right]])
20+
while (left < right and nums[left] == nums[left + 1]): #去重
21+
left += 1
22+
while (left < right and nums[right] == nums[right - 1]):
23+
right -= 1
24+
left += 1
25+
right -= 1
26+
return (res)

Diff for: 0016-3sum-closest.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#固定一个数,利用双指针计算。比较绝对值
2+
class Solution:
3+
def threeSumClosest(self, nums: List[int], target: int) -> int:
4+
nums.sort()
5+
res = nums[0] + nums[1] + nums[2]
6+
n = len(nums)
7+
if (n < 3):
8+
return
9+
for i in range(n - 2):
10+
if (i > 0 and nums[i] == nums[i - 1]):
11+
continue
12+
left = i + 1
13+
right = n - 1
14+
while (left < right):
15+
s = nums[i] + nums[left] + nums[right]
16+
if (s == target):
17+
return s
18+
if (abs(s - target) < abs(res - target)):
19+
res = s
20+
if (s < target):
21+
left += 1
22+
elif (s > target):
23+
right -= 1
24+
return res

Diff for: 0018-4sum.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#通过递归将问题始终分解成两数和
2+
class Solution:
3+
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
4+
nums.sort()
5+
res = []
6+
self.findnums(target, nums, 4, [], res)
7+
return res
8+
9+
def findnums(self, target: int, nums: List[int], N: int, temp: List[int], res: List[List[int]]):
10+
if N < 2 or len(nums) < N:
11+
return
12+
if N == 2:
13+
left = 0
14+
right = len(nums) - 1
15+
while left < right:
16+
if nums[left] + nums[right] < target:
17+
left += 1
18+
elif nums[left] + nums[right] > target:
19+
right -= 1
20+
else:
21+
res.append(temp + [nums[left], nums[right]])
22+
left += 1
23+
right -= 1
24+
while left < right and nums[left] == nums[left - 1]:
25+
left += 1
26+
while left < right and nums[right] == nums[right + 1]:
27+
right -= 1
28+
else:
29+
for i in range(len(nums)):
30+
if i == 0 or i > 0 and nums[i - 1] != nums[i]:
31+
self.findnums(target - nums[i], nums[i + 1:], N - 1, temp + [nums[i]], res)
32+
return

Diff for: 0020-valid-parentheses.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//?
22
typedef struct stack {
33
char* top;
44
char* base;

Diff for: 0038-count-and-say.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 遍历生成
2+
class Solution:
3+
def countAndSay(self, n: int) -> str:
4+
ret = "1"
5+
for i in range(n - 1):
6+
cur = ""
7+
pos = 0
8+
start = 0
9+
10+
while pos < len(ret):
11+
while pos < len(ret) and ret[pos] == ret[start]:
12+
pos += 1
13+
cur += str(pos - start) + ret[start]
14+
start = pos
15+
ret = cur
16+
return ret

Diff for: 0053-maximum-subarray.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 动态规划,计算nums[list,i-1]的和,与nums[i]比较。如果小于则不加,如果大于则加
2+
class Solution:
3+
def maxSubArray(self, nums: List[int]) -> int:
4+
n = len(nums)
5+
max_temp = max_sum = nums[0]
6+
for i in range(1, n):
7+
max_sum = max(max_sum + nums[i], nums[i])
8+
max_temp = max(max_sum, max_temp)
9+
return max_temp

Diff for: 0166-fraction-to-recurring-decimal.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 取符号、取整数、取小数
2+
class Solution:
3+
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
4+
if numerator == 0 or denominator == 0:
5+
return "0"
6+
nums = []
7+
temp = []
8+
if (numerator < 0) != (denominator < 0): # 取符号
9+
nums.append('-')
10+
temp.append('-')
11+
12+
num = abs(numerator)
13+
den = abs(denominator)
14+
a = num // den # 取整数
15+
b = num % den
16+
nums.append(str(a))
17+
if b != 0:
18+
nums.append('.')
19+
while b != 0: # 取小数
20+
if b not in temp:
21+
temp.append(b)
22+
nums.append(str(b * 10 // den))
23+
else:
24+
nums.insert(temp.index(b) + 2, '(')
25+
nums.append(')')
26+
break
27+
b = b * 10 % den
28+
29+
return ''.join(nums)

Diff for: 0179-largest-number.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 通过字符串求和比较大小排序
2+
class Solution:
3+
def largestNumber(self, nums: List[int]) -> str:
4+
s = [str(i) for i in nums]
5+
6+
def com(a, b):
7+
if (a + b) > (b + a):
8+
return 1
9+
if (a + b) < (b + a):
10+
return -1
11+
return 0
12+
13+
s.sort(reverse=True, key=functools.cmp_to_key(com))
14+
max_num = str(int("".join(s))) # int()处理第一位为0的情况
15+
return max_num

Diff for: 0217-contains-duplicate.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# set() 函数创建一个无序不重复元素集
2+
class Solution:
3+
def containsDuplicate(self, nums: List[int]) -> bool:
4+
return not len(set(nums)) == len(nums)
5+
6+
7+
# 排序后相邻比较
8+
class Solution:
9+
def containsDuplicate(self, nums: List[int]) -> bool:
10+
nums.sort()
11+
for i in range(len(nums) - 1):
12+
if nums[i] == nums[i + 1]:
13+
return True
14+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 双指针
2+
class Solution:
3+
def isPalindrome(self, head: ListNode) -> bool:
4+
fast = right = head
5+
left = None
6+
while (fast and fast.next):
7+
fast = fast.next.next
8+
temp = right.next
9+
right.next = left
10+
left = right
11+
right = temp
12+
if fast:
13+
right = right.next
14+
while left:
15+
if (left.val != right.val):
16+
return False
17+
left = left.next
18+
right = right.next
19+
return True

Diff for: 0283-move-zeroes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//移动快慢指针,剩下的补0(类似027
1+
//移动快慢指针,剩下的补0(类似27
22
void moveZeroes(int* nums, int numsSize) {
33
int slow = 0;
44
for(int fast = 0; fast < numsSize; fast++) {

Diff for: 0368-largest-divisible-subset.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 遍历
2+
class Solution:
3+
def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
4+
s = {-1: set()}
5+
nums.sort()
6+
for i in nums:
7+
temp = []
8+
for j in s:
9+
if (i % j) == 0:
10+
s[j].add(i)
11+
temp.append(s[j])
12+
s[j].remove(i)
13+
s[i] = max(temp, key=len) | {i}
14+
return list(max(s.values(), key=len))

Diff for: 0454-4sum-ii.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#两两求和
2+
class Solution:
3+
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
4+
mapper = {}
5+
res = 0
6+
for i in nums1:
7+
for j in nums2:
8+
mapper[i + j] = mapper.get(i + j, 0) + 1
9+
for i in nums3:
10+
for j in nums4:
11+
res += mapper.get(-1 * (i + j), 0)
12+
return res

Diff for: 0509-fibonacci-number.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//动态规划
21
//自底向上
32
int fib(int N) {
43
if(N <= 1) {

Diff for: 0516-longest-palindromic-subsequence.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 动态规划,设n行n列的数组,dp[i][j]表示s[i:j+1]中的最大回文长度
2+
# 如果i==j,则为1
3+
# 如果s[i]==[j],则在原基础上加2
4+
# 如果扩展时两边不相等,无法扩展,则取dp[i+1][j]和dp[i][j-1]中最大值
5+
class Solution:
6+
def longestPalindromeSubseq(self, s: str) -> int:
7+
n = len(s)
8+
dp = [[0] * n for i in range(n)]
9+
for i in reversed(range(n)):
10+
for j in range(i, n):
11+
if i == j:
12+
dp[i][j] = 1
13+
elif s[i] == s[j]:
14+
dp[i][j] = dp[i + 1][j - 1] + 2
15+
else:
16+
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
17+
return dp[0][n - 1]

Diff for: 0680-valid-palindrome-ii.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#利用双指针
2+
class Solution:
3+
def validPalindrome(self, s: str) -> bool:
4+
n = len(s)
5+
6+
def func(s: str, l: int, r: int):
7+
while (l < r):
8+
if s[l] != s[r]:
9+
return False
10+
l += 1
11+
r -= 1
12+
return True
13+
14+
l = 0
15+
r = n - 1
16+
while (l < r):
17+
if s[l] != s[r]:
18+
return func(s, l + 1, r) or func(s, l, r - 1)
19+
l += 1
20+
r -= 1
21+
return True

Diff for: 0779-k-th-symbol-in-grammar.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 利用对称,前半部分与上一行相同,后半部分与上一行相反
2+
class Solution:
3+
def kthGrammar(self, n: int, k: int) -> int:
4+
if n == 1:
5+
return 0
6+
if k <= (1 << (n - 2)):
7+
return self.kthGrammar(n - 1, k)
8+
else:
9+
return self.kthGrammar(n - 1, k - (1 << (n - 2))) ^ 1
10+
11+
12+
# 每个2i位置都与上行i相同,每个2i+1位置都与上行i反转,每一次k除2判断奇偶可以知道是否取反,多次k除2判断奇偶性可以等价于二进制中1的数量
13+
class Solution:
14+
def kthGrammar(self, n: int, k: int) -> int:
15+
return (k - 1).bit_count() & 1
16+
17+
18+
# 递归
19+
class Solution:
20+
def kthGrammar(self, n: int, k: int) -> int:
21+
if n == 1:
22+
return 0
23+
return (k & 1) ^ 1 ^ self.kthGrammar(n - 1, (k + 1) // 2)

Diff for: 0901-online-stock-span.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 利用单调栈,通过比较大小更新跨度值
2+
class StockSpanner:
3+
4+
def __init__(self):
5+
self.stack = [(-1, inf)]
6+
self.index = -1
7+
8+
def next(self, price: int) -> int:
9+
self.index += 1
10+
while price >= self.stack[-1][1]:
11+
self.stack.pop()
12+
self.stack.append((self.index, price))
13+
return self.index - self.stack[-2][0]

0 commit comments

Comments
 (0)