when you do this:
num = num[:pre]+tmp
actually you did not modify the array which num referrer, you just assign a new array referrer to num, that means you did not modify the old array the question given to you.
try this
num[pre+1:]=tmp
when you do this:
num = num[:pre]+tmp
actually you did not modify the array which num referrer, you just assign a new array referrer to num, that means you did not modify the old array the question given to you.
try this
num[pre+1:]=tmp
improved code
if not digits:
return []
result = list(key[digits[0]])
for i in digits[1:]:
temp = []
for x in result:
for k in key[i]:
temp.append(x+k)
result=temp
return result
assume you call the function like below:
a = [1,2,3,4,5,6]
Solution().rotate(a, 3)
print a
In the rotate function, variable nums is not a list ,
it's a reference of list a
nums[:] = [1,2,3] operating will edit the list as a references (they share same memory area)
nums= [1,2,3], you just give the reference of [1,2,3] to nums, after this nums doesn't reference the old list (a) any more
class Solution:
# @param num, a list of integers
# @return an integer
def majorityElement(self, num):
temp=[]
for i in num:
if not temp or temp[-1]==i:
temp.append(i)
else:
temp.pop()
return temp[0]
#solution:
###two different number make a pair, after making the total list do the match, the majority number must have at least 1 element that can not match. the rest element(s) that not matched is the majority number.
###here i use a stack to solve the problem, it takes 356 ms, i don't think it's fast enough