好难过,这是我刷算法第20天了。被张昊狂批了一番,“你不是都刷了七十多道题了吗?怎么连地址、引用都不懂?”刷这么多道题,还这种水平。╮(╯▽╰)╭我是不是每次刷题太匆忙了,做不到挖透这道题,顺带连这个类型的都会。我的学习方法该怎么提高呀?
- Find Pivot Index
找数组中第一个能让左边的数相加等于右边的数相加的数
class Solution(object):
def pivotIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) < 3:
return -1
sum1 = 0
sum2 = 0
for i in range (len(nums)):
sum1 += nums[i]
for i in range (-1,len(nums)-1,1):
if sum2 == sum1 - sum2 -nums[i+1]:
return i+1
sum2 += nums[i+1]
return -1
- 1-bit and 2-bit Characters
如果字符数组中可以出现10 11 0这三个字符,判断最后是否是0这个字符
class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
i = 0
while i < len(bits):
if i == len(bits)-1:
return True
if bits[i] == 1:
i += 2
else:
i += 1
return False
网友评论