Skip to content

Commit 4ee7d56

Browse files
committed
update
1 parent 9e9d3c4 commit 4ee7d56

File tree

6 files changed

+190
-0
lines changed

6 files changed

+190
-0
lines changed

code/164.最大间距.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# @lc app=leetcode.cn id=164 lang=python3
3+
#
4+
# [164] 最大间距
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
10+
def maximumGap(self, nums: List[int]) -> int:
11+
if len(nums) < 2:
12+
return 0
13+
14+
buf = [0] * len(nums)
15+
16+
sign = 1
17+
max_num = max(nums)
18+
19+
while max_num >= sign:
20+
bucket = [0] * 10
21+
for num in nums:
22+
bucket[num // sign % 10] += 1
23+
24+
for i in range(1, 10):
25+
bucket[i] += bucket[i - 1]
26+
27+
for i in range(len(nums) - 1, -1, -1):
28+
num = nums[i]
29+
n = num // sign % 10
30+
buf[bucket[n] - 1] = num
31+
bucket[n] -= 1
32+
sign *= 10
33+
34+
if max_num >= sign:
35+
for i, num in enumerate(buf):
36+
nums[i] = num
37+
38+
res = 0
39+
for i in range(1, len(buf)):
40+
b = buf[i] - buf[i - 1]
41+
if b > res:
42+
res = b
43+
44+
return res
45+
46+
# @lc code=end
47+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# @lc app=leetcode.cn id=241 lang=python3
3+
#
4+
# [241] 为运算表达式设计优先级
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
10+
def diffWaysToCompute_(self, numbers, symbols):
11+
if len(symbols) == 0:
12+
return numbers
13+
res = []
14+
for i in range(1, len(numbers)):
15+
symbol = symbols[i - 1]
16+
if symbol == '+':
17+
number = numbers[i] + numbers[i - 1]
18+
elif symbol == '-':
19+
number = numbers[i - 1] - numbers[i]
20+
else:
21+
number = numbers[i] * numbers[i - 1]
22+
23+
res.extend(self.diffWaysToCompute_([*numbers[:i-1], number, *numbers[i+1:]], [*symbols[:i - 1], *symbols[i:]]))
24+
return res
25+
26+
27+
def diffWaysToCompute(self, expression: str) -> List[int]:
28+
numbers = []
29+
symbols = []
30+
number = ''
31+
for exp in expression:
32+
if '0' <= exp <= '9':
33+
number += exp
34+
else:
35+
numbers.append(int(number))
36+
number = ''
37+
symbols.append(exp)
38+
numbers.append(int(number))
39+
return self.diffWaysToCompute_(numbers, symbols)
40+
41+
# @lc code=end

code/682.棒球比赛.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# @lc app=leetcode.cn id=682 lang=python3
3+
#
4+
# [682] 棒球比赛
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def calPoints(self, operations: List[str]) -> int:
10+
sum_ = 0
11+
scores = []
12+
13+
for operation in operations:
14+
if operation == '+':
15+
n = scores[-1] + scores[-2]
16+
sum_ += n
17+
scores.append(n)
18+
elif operation == 'D':
19+
n = scores[-1] * 2
20+
sum_ += n
21+
scores.append(n)
22+
elif operation == 'C':
23+
score = scores.pop()
24+
sum_ -= score
25+
else:
26+
n = int(operation)
27+
scores.append(n)
28+
sum_ += n
29+
return sum_
30+
# @lc code=end
31+

code/693.交替位二进制数.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# @lc app=leetcode.cn id=693 lang=python3
3+
#
4+
# [693] 交替位二进制数
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def hasAlternatingBits(self, n: int) -> bool:
10+
start = n % 2
11+
n //= 2
12+
while n > 0:
13+
back = n % 2
14+
n //= 2
15+
if start == back:
16+
return False
17+
start = back
18+
19+
return True
20+
# @lc code=end
21+

code/696.计数二进制子串.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# @lc app=leetcode.cn id=696 lang=python3
3+
#
4+
# [696] 计数二进制子串
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def countBinarySubstrings(self, s: str) -> int:
10+
sign = -1
11+
sum_ = 0
12+
for i in range(1, len(s)):
13+
if s[i] != s[i- 1]:
14+
sum_ += 1
15+
sign = i - 2
16+
elif sign > -1:
17+
if s[i] != s[sign]:
18+
sum_ += 1
19+
sign -= 1
20+
else:
21+
sign = -1
22+
23+
return sum_
24+
# @lc code=end
25+

code/697.数组的度.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# @lc app=leetcode.cn id=697 lang=python3
3+
#
4+
# [697] 数组的度
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def findShortestSubArray(self, nums: List[int]) -> int:
10+
max_key = -1
11+
res = {}
12+
for i, num in enumerate(nums):
13+
msg = res.get(num, [0, i, i])
14+
msg[0] += 1
15+
msg[2] = i
16+
res[num] = msg
17+
18+
max_msg = res.get(max_key)
19+
if max_key == -1 or max_msg[0] < msg[0] or(max_msg[0] == msg[0] and max_msg[2] - max_msg[1] > msg[2] - msg[1]):
20+
max_key = num
21+
22+
msg = res.get(max_key)
23+
return msg[2] - msg[1] + 1
24+
# @lc code=end
25+

0 commit comments

Comments
 (0)