src
class Solution:
def countEven(self, num: int) -> int:
def cnt(x):
sum1 = 0
while x != 0:
sum1 += x % 10
x //= 10
return sum1
ans = 0
for i in range(1, num + 1):
if cnt(i) % 2 == 0:
ans += 1
return ans
src
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
value = []
p = head
tempsum = 0
while p != None:
tempsum += p.val
if p.val == 0:
if tempsum > 0:
value.append(tempsum)
tempsum = 0
p = p.next
print(value)
p = head
n = len(value)
for i in range(n):
if i == 0:
p.val = value[i]
else:
q = ListNode(value[i])
p.next = q
p = q
if n == 1:
p.next = None
return head
思路:
用dict反向模拟,比较恶心,要根据答案改bug
用pre记录前一个
class Solution:
def repeatLimitedString(self, s: str, repeatLimit: int) -> str:
cnt = defaultdict(int)
for c in s:
cnt[ord(c) - ord('a')] += 1
#print(cnt)
cnt = sorted(cnt.items(), key = lambda x: -x[0])
cnt = [list(i) for i in cnt]
#print(cnt)
ans = ""
now = 0
pre = -1
while now < len(cnt):
if pre != -1 and cnt[pre][1] > 0 and now != pre:
if cnt[now][1] > 0:
ans += chr(ord('a') + cnt[now][0])
cnt[now][1] -= 1
now = pre
continue
#print(ans)
#print(cnt)
else:
now += 1
continue
if cnt[now][1] <= repeatLimit:
ans += chr(ord('a') + cnt[now][0]) * cnt[now][1]
cnt[now][1] = 0
now += 1
#print(ans)
#print(cnt)
else:
ans += chr(ord('a') + cnt[now][0]) * repeatLimit
cnt[now][1] -= repeatLimit
pre = now
now += 1
#print(ans)
#print(cnt)
return ans
思路:
找出gcd,然后判断它们乘积就好
src:
class Solution:
def coutPairs(self, nums: List[int], k: int) -> int:
def gcd(x, y):
if y != 0:
return gcd(y, x % y)
return x
gcdDict = defaultdict(int)
for num in nums:
gcdDict[gcd(num, k)] += 1
ans = 0
#print(gcdDict)
keys = list(gcdDict.keys())
#print(keys)
n = len(keys)
for i in range(n):
for j in range(i, n):
if i != j and (keys[i] * keys[j]) % k == 0:
ans += gcdDict[keys[i]] * gcdDict[keys[j]]
elif i == j and (keys[i] * keys[j]) % k == 0:
ans += gcdDict[keys[i]] * (gcdDict[keys[i]] - 1) // 2
return ans
src:
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n = len(nums)
ans = 0
for i in range(n):
for j in range(i + 1, n):
if nums[i] == nums[j] and (i * j) % k == 0:
ans += 1
return ans
src:
class Solution:
def sumOfThree(self, num: int) -> List[int]:
ans = []
if num % 3 == 0:
mid = num // 3
ans.append(mid - 1)
ans.append(mid)
ans.append(mid + 1)
return ans
思路:
处以2后,按12345这样求和得到1,3,6,10看看总和夹在哪个位置
然后填数字的时候就从12345填入,最后补充剩余的一个即可
src:
class Solution:
def maximumEvenSplit(self, finalSum: int) -> List[int]:
if finalSum % 2 == 1:
return []
sum1 = finalSum // 2
ans = 0
k = 0
flag = False
left = 0
right = 0xffffffff
while left < right:
mid = (left + right) // 2 + 1
cnt = mid * (mid + 1) // 2
if cnt == sum1:
k = mid
flag = True
break
elif cnt > sum1:
right = mid - 1
else:
left = mid
if flag:
ans = k
else:
ans = left
res = []
temp = 0
for i in range(1, ans):
res.append(2 * i)
temp += 2 * i
res.append(finalSum - temp)
return res
总结:
有进步!第二次周赛没写完最后一个hard着实有点可惜!