8/24/2020
1480. Running Sum of 1d Array
- 我的做法
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
sums = []
sum = 0
for num in nums:
sum += num
sums.append(sum)
return sums
- 大神做法
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
return accumulate(nums)
我是用了一个简单的loop,大神直接用了itertools的函数。
1119. Remove Vowels from a String
- 我
class Solution:
def removeVowels(self, S: str) -> str:
return S.replace("a","").replace("e","").replace("i","").replace("o","").replace("u","")
- 大神
class Solution:
def removeVowels(self, S: str) -> str:
return "".join(c for c in S if c not in "aeiou")
- 小伙伴
class Solution:
def removeVowels(self, S: str) -> str:
import re
return re.sub("a|e|i|o|u","",S)
检索替换字符串
Python字符串操作之字符串搜索与替换
python使用re进行字符串查找和替换
1431. Kids With the Greatest Number of Candies
class Solution:
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
max_num = max(candies)
return [candy + extraCandies >= max_num for candy in candies]
1470. Shuffle the Array
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
result = []
for i in range(n):
result.append(nums[i])
result.append(nums[i+n])
return result
1512. Number of Good Pairs
- 我
class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
n = 0
m = 0
for num in set(nums):
n = nums.count(num) * (nums.count(num) - 1)//2
m += n
return m
- 大神
def numIdenticalPairs(self, A):
return sum(k * (k - 1) // 2 for k in collections.Counter(A).values())
查询List中元素出现次数
python 统计list中各个元素出现的次数
- 利用Python字典统计
- 利用Python的collection包下Counter的类统计
- 利用Python的pandas包下的value_counts的类统计
- 利用Python的str.count(“a”)
8/25/2020
1108. Defanging an IP Address
class Solution:
def defangIPaddr(self, address: str) -> str:
return address.replace(".","[.]")
771. Jewels and Stones
- 我
class Solution:
def numJewelsInStones(self, J: str, S: str) -> int:
m=0
for i in list(J):
n=S.count(i)
m+=n
return m
- 大神
class Solution(object):
def numJewelsInStones(self, J, S):
return sum(s in set(J) for s in S)
1342. Number of Steps to Reduce a Number to Zero
class Solution:
def numberOfSteps (self, num: int) -> int:
i = 0
while num>0:
if num%2 == 0:
num = num/2
else:
num = num-1
i += 1
return i
1528. Shuffle String
这道题我没写出来,菜鸟哇。
class Solution:
def restoreString(self, s: str, indices: List[int]) -> str:
ans = list(s)
for i, j in zip(indices, list(s)):
ans[i] = j
return "".join(ans)
1365. How Many Numbers Are Smaller Than the Current Number
class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
return [sorted(nums).index(a) for a in nums]
8/26/2020
1281. Subtract the Product and Sum of Digits of an Integer
class Solution:
def subtractProductAndSum(self, n: int) -> int:
p = 1
s = 0
for i in [int(item) for item in str(n)]:
p = p*i
s = s+i
result = p - s
return result
int怎么转list:
- map(int, str(num))
- [int(x) for x in str(num)]
1313. Decompress Run-Length Encoded List
class Solution:
def decompressRLElist(self, nums: List[int]) -> List[int]:
result= []
# linear scan with step size = +2
for i in range( 0, len(nums), 2):
result.extend( [nums[i+1]] * nums[i])
return result
1165. Single-Row Keyboard
class Solution:
def calculateTime(self, keyboard: str, word: str) -> int:
idx = []
result = 0
last = 0
for w in word:
idx += [keyboard.index(w)]
for x in idx:
result += abs(x-last)
last = x
return result
1486. XOR Operation in an Array
class Solution:
def xorOperation(self, n: int, start: int) -> int:
result = start
for i in range(1,n):
num = start + 2*i
result = result ^ num
return result
1389. Create Target Array in the Given Order
class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
ans = []
for i,j in zip(index, nums):
ans.insert(i, j)
return ans