Posts

Showing posts from June, 2020

Python list

a =[0,1,2,3,4,5,6,7,8,9] b = a [i:j] means copy a [i] to a[j-1] to generate a new List b b = a[1:3] so the content of b is [1,2] When i defaults, the default is 0, that is, a[:3] is equivalent to a [0:3] When j defaults, it defaults to len(a), i.e. a [1:] is equivalent to a [1:10] When i and j are default, a [:] is equivalent to a complete copy of a b = a [i:j:s], i,j is the same as above, but s means step, default is 1. a[i:j:1] is equivalent to a [i:j] When s <0, i defaults to -1. j defaults to -len(a)-1 a[::-1] is the same thing as a [-1:-len(a)-1:-1], which is the same thing as going from the last element to the first element. The last element is -1 and the second last is -1+s = -2, so the first element 0 is -10. nums[1:][::-1] means reverse the list nums[1:],  so we can get [9, 8, 7, 6, 5, 4, 3, 2, 1]  get the element reversely through 'for' loop:  for j in xrange(len(nums)-1, 0, -1):

Leetcode write up for 1-30 problems

1, two sum: 从一个list中找到两个数的和达到target。如7+2=9.  尝试: 为了降低时间复杂度,使用target减当前的数,再去里面找有没有差值。 一个循环搞定,但是要记得我们取的是index,所以要key是值,value是index。 class Solution(object):     def twoSum(self, nums, target):         L = len(nums)         tmp = {}         for i in range(L):             mid = target  - nums[i]             if mid in tmp:                 return [i,tmp[mid]]             if mid not in tmp:                 tmp[nums[i]] = i 2. Add Two Numbers 先让root, curr都为一个空指针,curr会移动并且更新,root也会更新, 但是始终还在第一个点。 cur...