Python 专栏收录该内容
1 篇文章 0 订阅

# 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)


## 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)


## 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())


python 统计list中各个元素出现的次数

1. 利用Python字典统计
2. 利用Python的collection包下Counter的类统计
3. 利用Python的pandas包下的value_counts的类统计
4. 利用Python的str.count(“a”)

# 8/25/2020

## 1108. Defanging an IP Address

class Solution:


## 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

• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

11-30 179

11-30 153
04-05 1093
03-10 2300
04-02 2511
09-16 692

Yating260

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。