自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

技术奇的默默积累

不断进步,多多指教!

  • 博客(116)
  • 收藏
  • 关注

原创 Leetcode204.count primes

def countPrimes(n): if n<=1: return 0 nums = [None]*n nums[0] = False nums[1] = False for i in range(n): if nums[i] ==None: nums[i] = True for j in range(i+i,n,i): nums[j] = Fa.

2020-06-22 08:05:45 303

原创 Leetcode189.rotate array

def rotate(nums, k): k = k % len(nums) nums[:k], nums[k:] = nums[len(nums) - k:], nums[:len(nums) - k]

2020-06-22 08:05:11 311

原创 Leetcode203.remove linked list elements

class ListNode: def __init__(self, x): self.val = x self.next = Noneclass Solution: def removeElement(self, head, val): dummy = ListNode(float('-inf')) dummy.next = head previous, current = dummy, dummy.n.

2020-06-22 08:04:13 302

原创 leetcode17.letter combination of a phone number

def letterCombinations(digits): if len(digits) == 0: return [] digit_map = { 0: '0', 1: '1', 2: 'abc', 3: 'def', 4: 'ghi', 5: 'jkl', 6: 'mno', 7: 'pqrs', 8: 'tuv', .

2020-06-22 08:03:00 251

原创 Leetcode171.excel sheet column number

def titleToNumber(s): res = 0 for i in range(len(s)): res *= 26 res += ord(s[i]) - ord('A') + 1 return res

2020-06-22 08:02:15 138

原创 Leetcode172.

def trailingZeroes(n): res = 0 while n != 0: res += n // 5 n = n // 5 return resn = 120print(trailingZeroes(n))这个还不是特别懂

2020-06-22 08:01:33 89

原创 Leetcode223.rectangle area

# 左下角和右上角的点def computeArea(A, B, C, D, E, F, G, H): overlap = max(min(C, G) - max(A, E), 0) * max(min(D, H) - max(B, F), 0) return (A - C) * (B - D) + (E - G) * (F - H) - overlap

2020-06-22 08:00:11 124

原创 Leetcode168.excel sheet column title

def convertToTitle(n): res, num = '', n while num: res += chr((num - 1) % 26 + ord('A')) num = (num - 1) // 26 return res[::-1]#chr() num=> char#ord() char=>numn = 28print(convertToTitle(n))

2020-06-22 07:59:34 128

原创 Leetcode169.majority element

def majorityElement(nums): # from collections import Counter # lookup = Counter(nums) # for i in lookup: # if lookup[i]>len(nums)//2: # return i index, cnt = 0, 1 for i in range(1, len(nums)): if nums[ind.

2020-06-21 23:44:27 165

原创 Leetcode167.two sum

def twoSum(numbers, target): start = 0 end = len(numbers) - 1 res = 0 while start != end: res = numbers[start] + numbers[end] if res > target: end -= 1 elif res < target: start += 1 .

2020-06-21 23:43:44 97

原创 leetcode160.intersection of two linked lists

class ListNode(object): def __init__(self,x): self.val = x self.next = Noneclass Solution(object): def getIntersectionNode(self,headA,headB): p1 = headA p2 = headB while p1 !=p2: if not p1: .

2020-06-21 23:42:33 143

原创 leetcode141.linked list cycle

class ListNode(object): def __init__(self,x): self.val = x self.next = Noneclass Solution(object): def hasCycle(self,head): fast,slow = head,head while fast and fast.next: fast,slow = fast.next.next,slo.

2020-06-21 23:41:45 173

原创 leetcode11.container with most water

def maxArea(height): # area = 0 # res = 0 # # for i in range(len(height)): # for j in range(i+1,len(height)): # area = abs(j-i)*min(height[i],height[j]) # res = max(res,area) l = 0 r = len(height)-1.

2020-06-21 23:41:07 146

原创 leetcode213.power of two

def isPowerOfTwo(n): # return n > 0 and n&(n -1)==0 if n < 1: return False while n%2==0: n = n // 2 return n==1

2020-06-21 23:39:43 88

原创 L226.Invert binary tree

def invertTree(self, root: TreeNode) -> TreeNode: if not root: return root.right, root.left = root.left, root.right self.invertTree(root.left) self.invertTree(root.right) return root

2020-06-21 23:39:09 93

原创 leetcode242.valid anagram

def isAnagram(s,t): # from collections import Counter # s = Counter(s) # t = Counter(t) # return s==t #lookup = collections.defaultdict(int)#初始化字典值为0 lookup = {} for i in s: if i not in lookup: lookup[i] =1.

2020-06-21 23:37:58 169

原创 leetcode243.shortest word distance

def shortestDistance(words, word1, word2): dist = float('inf') i, d1, d2 = 0, None, None while i<len(words): if words[i]==word1: d1=i elif words[i]==word2: d2=i if d1 is not None and d2 is not.

2020-06-21 23:37:08 115

原创 Leetcode12.integer to roman

def intToRoman(num): values = [1000,900,500,400,100,90,50,40,10,9,5,4,1] numerals = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'] res = '' for i in range(len(values)): while num>=values[i]: num-=values[.

2020-06-21 23:35:28 164

原创 Leetcode246.Strobogrammatic number

def isStrobogrammatic(num): lookup = {'0':'0','1':'1','6':'9','9':'6','8':'8'} n =len(num) for i in range(int((n+1)/2)): if num[i] not in lookup or num[i]!= lookup[num[n-1-i]]: return False return True

2020-06-18 21:25:56 139

原创 Leetcode258.add digits

def addDigits(num): # return (num - 1) % 9 + 1 if num>0 else 0 while len(str(num))>1: num = num//10 +num%10 return nums = 38print(addDigits(s))

2020-06-18 21:25:11 109

原创 leetcode.three sum

def threeSum(nums): n = len(nums) res = [] nums.sort() for i in range(n - 2): if nums[i] + nums[i + 1] + nums[i + 2] > 0: break if nums[i] + nums[n - 2] + nums[n - 1] < 0: continue if i.

2020-06-18 21:24:17 131

原创 Leetcode263.ugly number

def isUgly(num): if num==0: return False for i in [2,3,5]: while num%i ==0: num/=i return num==1

2020-06-18 21:23:12 165

原创 leetcode16.three sum closest

def threeSumClosest(nums, target): # 这个题只要找不到最终的结果的话,三个数一定是连在一起的,所以不会出现后面两个前面一个并且隔开的情况 nums = sorted(nums) res = nums[0] + nums[1] + nums[len(nums) - 1] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: .

2020-06-18 21:21:41 147

原创 Leetcode283.Move zeros

def moveZeroes(nums): if not nums: return 0 j = 0 for i in range(len(nums)): if nums[i]: nums[j],nums[i] = nums[i],nums[j] j+=1

2020-06-18 21:18:47 182

原创 leetcode290.word pattern

def wordPattern(pattern, str): # res = str.split() # # return list(map(pattern.index, pattern)) == list(map(res.index, res)) # # words = str.split(' ') if len(pattern) != len(words): return False hashmap = {} m.

2020-06-18 21:18:11 147

原创 Leetcode326.power of three

def isPowerOfThree(n): if n == 1: return True elif n <= 2: return False while n >= 3: if n == 3: return True if n % 3 == 0: n /= 3 else: return False

2020-06-18 21:17:20 111

原创 Leetcode303.range sum query

class NumArray:#空间换时间 def __init__(self, nums): self.res = [0] for num in nums: self.res.append(self.res[-1] + num) def sumRange(self, i, j): return self.res[j + 1] - self.res[i]

2020-06-18 21:16:42 83

原创 Leetcode345.reverse vowels of a string

def reverseVowels(s): vowels = 'aeiou' string = list(s) left, right = 0, len(s) - 1 while left < right: if string[left].lower() not in vowels: left += 1 elif string[right].lower() not in vowels: r.

2020-06-18 21:14:47 161

原创 Leetcode342.power of four

def isPowerOfFour(num): while num >= 4: if num % 4 == 0: num /= 4 else: return False if num ==1: return True return False

2020-06-17 22:45:46 160

原创 Leetcode344.reverse string

def reverseString(s): left, right = 0, len(s) - 1 while left < right: s[left], s[right] = s[right], s[left] left += 1 right -= 1 return sl = ['H','e','l','l','o']print(l)

2020-06-17 22:45:05 169

原创 Leetcode326.power of three

def isPowerOfThree(n): if n == 1: return True elif n <= 2: return False while n >= 3: if n == 3: return True if n % 3 == 0: n /= 3 else: return False

2020-06-17 22:44:24 63

原创 Leetcode349.intersection of two arrays

def intersection(nums1,nums2): lookup = set(nums1) res = [] for i in nums2: if i in lookup: res.append(i) lookup.discard(i) return res

2020-06-17 22:43:43 127

原创 Leetcode367.valid perfect square

def isPerfectSquare(num): if num == 1: return True left, right = 2, num // 2 while left <= right: mid = (left + right) // 2 guess_num = mid * mid if guess_num == num: return True elif gue.

2020-06-17 22:42:47 139

原创 Leetcode350.intersection of two arrays

from collections import defaultdictdef intersect(nums1, nums2): if len(nums1)>len(nums2): return intersect(nums2,nums1) lookup = defaultdict(int) for i in nums1: lookup[i] += 1 res = [] for i in nums2: if .

2020-06-17 22:42:06 134

原创 Leetcode374.guess number higher or lower

def guess(s): passdef guessNumber(n): left = 1 right = n while left <= right: mid = (left + right) // 2#python2的‘/’和python3的‘//’相同 ret = guess(mid) if ret == 0: return mid elif ret == -1: .

2020-06-17 22:41:08 142

原创 动态规划相关内容

所以一个问题是该用递推、贪心、搜索还是动态规划,完全是由这个问题本身阶段间状态的转移方式决定的!每个阶段只有一个状态->递推;每个阶段的最优状态都是由上一个阶段的最优状态得到的->贪心;每个阶段的最优状态是由之前所有阶段的状态的组合得到的->搜索;每个阶段的最优状态可以从之前某个阶段的某个或某些状态直接得到而不管之前这个状态是如何得到的->动态规划。枚举是枚举所有答案,然后依次验证。dp是分析问题的结构找到dp方程来求解动态规划的本质,是对问题状态的定义..

2020-06-17 22:40:14 83

原创 Leetcode387.first unique character in a string

def firstUniqchar(s): from collections import Counter lookup = Counter(s) # print(lookup) for i, c in enumerate(s): if lookup[c] == 0: return i return -1s = 'leetcode'print(firstUniqchar(s))

2020-06-17 22:40:04 129

转载 长度为n的整数数组,找出其中任意(n-1)个乘积最大的那一组,只能用乘法,不可 以用除法。要求对算法的时间复杂度和空间复杂度作出分析,可以写思路也可以写程序。

法一:时间空间复杂度都为O(n)s1[i]:从前往后遍历到i位置的乘积(0<=i<n)s2[j]:从后往前遍历到j位置的乘积(0<=j<n)最后遍历一遍找出s1[k+1]*s2[k-1]的最小值(0<k<n-1),讲结果与s1[1]和s2[n-2]比较大小,最大的那个即为最大值。法二:设这n个数的乘积为P当P=0时,除去n个数中的一个0,计算剩下的数的乘积,记为Q: 当Q=0时,说明n个数中至少有两...

2020-06-17 16:04:04 738

原创 Leetcode389.find the difference

def findTheDifference(s, t): s_dict = {} for x in s: if x in s_dict: s_dict[x] += 1 else: s_dict[x] = 1 for y in t: if y not in s_dict or s_dict[y] == 0: return y else: .

2020-06-16 23:47:53 219 1

原创 Leetcode404.Sum of left leaves

class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution: def sumOfLeftLeaves(self, root): def helper(root, direction): if not root: return .

2020-06-16 23:47:09 111

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除