自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(239)
  • 收藏
  • 关注

原创 实用面试数组排序算法 - Sorting Algorithms

heapify的时间复杂度是O(nlogn),heappop的时间复杂度是O(logn)执行n次。每层合并(O(n)) - 如图,执行①②④⑤是O(n),执行③⑥是O(n),执行⑦是O(n)调用栈深度(O(logn)) * 每层合并(O(n)) = O(nlogn)调用栈深度(O(logn)) * 每层合并(O(n)) = O(nlogn)调用栈深度(O(n)) * 每层合并(O(n)) = O(n^2)倒数第二层有n/4个节点,倒数第三层有n/8个节点。调用栈深度(O(logn))2.复杂度为O(n)

2022-10-13 06:09:42 312

原创 LeetCode 327. Count of Range Sum

离散化 + 树状数组。

2022-10-09 06:31:11 115

原创 LeetCode 315. Count of Smaller Numbers After Self

【代码】LeetCode 315. Count of Smaller Numbers After Self。

2022-10-03 01:19:38 434

原创 LeetCode 324. Wiggle Sort II

LeetCode 题解

2022-09-26 06:50:33 85

原创 7 · Serialize and Deserialize Binary Tree

class Solution: """ @param root: An object of TreeNode, denote the root of the binary tree. This method will be invoked first, you should design your own algorithm to serialize a binary tree which denote by a root node to a string which .

2021-06-18 07:42:21 131

原创 LintCode Encode and Decode TinyURL

class Codec: codeDB, urlDB = defaultdict(), defaultdict() chars = string.ascii_letters + string.digits def getCode(self) -> str: code = ''.join(random.choice(self.chars) for i in range(6)) return "http://tinyurl.com/" + cod.

2021-06-18 07:02:58 142

原创 Binary Tree Traversal 二叉树遍历 基础 Python

Preorder TraversalRecursionclass Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: if not root: return [] return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)Iteration

2020-07-05 22:10:51 184

原创 LeetCode 809. Expressive Words | two pointer | 题意分析

Example:Input: S = "heeellooo"words = ["hello", "hi", "helo"]Output: 1Explanation: We can extend "e" and "o" in the word "hello" to get "heeellooo".We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.题意分析个人觉得反.

2020-06-05 07:12:27 174

原创 LeetCode - 92. Reverse Linked List II

class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: dummy = pre_s = ListNode(0) dummy.next = head for i in range(m-1): # point to one ...

2019-09-11 06:54:13 116

原创 LeetCode - 7. Reverse Integer

7. Reverse Integertreat as integerclass Solution: def reverse(self, x: int) -> int: res = 0 cur = abs(x) while cur!=0: res = res*10+cur%10 c...

2019-08-31 11:10:13 107

原创 LeetCode - 9. Palindrome Number

9. Palindrome NumberConvert to Stringclass Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False x = str(x) l, r = 0, len(x)-1 while l&l...

2019-08-31 02:29:46 176

原创 LeetCode - 120. Triangle | DFS | Divide & Conquer | DP | Greedy

120. TriangleGreedyclass Solution: def minimumTotal(self, triangle: List[List[int]]) -> int: # [ # [2], # [3,4], # [6,5,7], # [4...

2019-08-27 03:15:59 138

转载 LeetCode - 847. Shortest Path Visiting All Nodes

847. Shortest Path Visiting All Nodesclass Solution: def shortestPathLength(self, graph: List[List[int]]) -> int: nodeCount = len(graph) masks = [(1<<i) for i in range(...

2019-08-27 01:48:10 155

原创 LeetCode - 72. Edit Distance

72. Edit DistanceBrute Forceclass Solution: def minDistance(self, word1: str, word2: str) -> int: return self.dfs(word1, 0, word2, 0) def dfs(self, w1, i1, w2, i2): if ...

2019-08-26 23:47:46 89

原创 LeetCode - Two Sum / K Sum 总结

1. Two Sum1. input array not sorted2. looking for 2 numbers, cannot use the element multiple times3. exactly 1 answer (index)Dictionaryclass Solution: def twoSum(self, nums: List[int], ta...

2019-08-26 11:48:28 134

原创 LeetCode - 507. Perfect Number

class Solution: def checkPerfectNumber(self, num: int) -> bool: if num<=1: return False sum = 1 for i in range(2, int(num**0.5)+1): if num%i==0: ...

2019-08-25 23:15:50 103

原创 LeetCode - 633. Sum of Square Numbers

633. Sum of Square NumbersTwo Pointersclass Solution: def judgeSquareSum(self, c: int) -> bool: l, r = 0, int(c**0.5) while l<=r: s = l*l + r*r i...

2019-08-25 22:52:32 104

原创 LeetCode - 739. Daily Temperatures

739. Daily Temperaturesclass Solution: def dailyTemperatures(self, T: List[int]) -> List[int]: stack = [0] n = len(T) res = [0]*n for i in range(n): ...

2019-08-22 02:21:03 103

原创 LeetCode - 617. Merge Two Binary Trees

617. Merge Two Binary TreesRecursion1. update current value2. recurse left and rightnot in-placeclass Solution(object): def mergeTrees(self, t1, t2): """ :type t1: TreeNo...

2019-08-15 08:18:51 95

原创 CC189 8.3 Magic Index

Magic Indexpublic class MagicIndex { // perform modified binary search public int search(int[] A) { int l= 0; int r = A.length-1; while(l+1<r){ int mid = l+(r-l)/2; if(A[mid]...

2019-07-14 11:25:55 139

原创 LeetCode - 63. Unique Paths II

63. Unique Paths IIRecursion (TLE)class Solution: def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: return self.helper(obstacleGrid, len(obstacleGrid)-1, le...

2019-07-14 07:57:16 85

原创 LeetCode - 242. Valid Anagram

242. Valid Anagramclass Solution: def isAnagram(self, s: str, t: str) -> bool: d = {} for c in s: d[c] = d.get(c, 0)+1 for c in t: if not d.ge...

2019-07-07 11:12:17 57

原创 LeetCode - 179. Largest Number

179. Largest Numberimport functoolsclass Solution: def largestNumber(self, nums: List[int]) -> str: nums = sorted([str(x) for x in nums], key=functools.cmp_to_key(self.compare)) ...

2019-07-07 10:06:17 123

原创 LeetCode - 275. H-Index II

275. H-Index IIclass Solution: def hIndex(self, citations: List[int]) -> int: if not citations or len(citations)==0: return 0 n = len(citations) l, r = 0, n-1 ...

2019-07-07 09:37:20 95

原创 LeetCode - 274. H-Index

274. H-Index先来理解一下这道题目简单来说,寻一个最大的符合条件的数;条件:至少有h篇citation至少为h的papereg1.[3,0,6,1,5] -> [0,1,3,5,6]至少有6篇citation至少为6的paper(其实只有1篇,[6]) -> NO至少有5篇citation至少为5的paper(其实只有2篇,[5,6]) -> NO至少有...

2019-07-07 07:52:52 73

原创 LeetCode - 75. Sort Colors

75. Sort Colors两个易错点:1.什么时候p才可以+1?当前位置为12.nums[p]==2需要提前:换过来的数字可能为0或1,而nums[p]==0换过来的数字只可能是1class Solution: def sortColors(self, nums: List[int]) -> None: """ Do not retu...

2019-07-05 17:50:17 91

原创 LeetCode - 148. Sort List

148. Sort List# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def merge(self, h1, h2): ...

2019-07-05 12:49:24 156 1

原创 LeetCode - 147. Insertion Sort List

147. Insertion Sort List# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def insertionSortL...

2019-07-05 11:48:36 85

原创 LeetCode - 23. Merge k Sorted Lists

有k/2对这样的Linked List,所以第一次两两合并的time complexity是2n*k/2。(2^x)*n*k/(2^x)为最后一次运行,k/(2^x)为最后剩余Linked List的个数,所以k/(2^x)=1, 得x=logk。每次选择k lists中最小的 - O(logk),选择N次 ----> O(Nlogk)= n*k + n*k + ... + n*k (有x个这样的n*k)每次选择k lists中最小的。所有node sort一遍。,两两合并最后成一个。

2019-07-05 10:00:46 132 1

原创 LeetCode - 21. Merge Two Sorted Lists

21. Merge Two Sorted ListsIterative# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(ob...

2019-07-05 06:04:50 106

原创 LeetCode - 88. Merge Sorted Array

88. Merge Sorted Array遍历完第一个while loop,如果m仍大于0,不需要继续遍历,nums1剩下的部分已经有序了。class Solution(object): def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m:...

2019-07-05 05:38:15 75

原创 LeetCode - 436. Find Right Interval

436. Find Right Intervalclass Solution(object): def findRightInterval(self, intervals): """ :type intervals: List[List[int]] :rtype: List[int] """ invs =...

2019-07-03 13:04:26 99

原创 csacademy - Contained Intervals

Contained Intervals排序:前升序,后降序 -> 包含本区间的interval总在之前出现成为被包含区间的两种情况:1.后有相同区间2.前有大区间struct Interval { int l, r;};bool myFunc(Interval a, Interval b) { if(a.l==b.l) return a.r>b.r...

2019-07-01 16:44:00 131

原创 LeetCode - 253. Meeting Rooms II

253. Meeting Rooms IImapclass Solution {public: int minMeetingRooms(vector<Interval>& intervals) { map<int, int> m; for (auto a : intervals) { ++m...

2019-06-30 16:12:36 100

原创 QuickSort 快速排序

基本思想:divide &conquer and partition -把array分成>=pivot和<pivot(=随意)两部分重点:1.while l<r -> 最终l等于r2.因为最终start与l交换位置,l必须指向一个小于pivot的数 -> if else的顺序不能变Partitionsdef partitionSwap(a...

2019-06-30 06:14:31 153

原创 LeetCode - 929. Unique Email Addresses

929. Unique Email Addressesclass Solution(object): def numUniqueEmails(self, emails): """ :type emails: List[str] :rtype: int """ res = set() for...

2019-06-29 15:37:31 85

原创 LeetCode - 100. Same Tree

100. Same TreeIterative# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Nonec...

2019-06-28 16:56:05 68

原创 LeetCode - 67. Add Binary

67. Add BinaryIterativeclass Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ if len(a)>len(b): re...

2019-06-28 15:31:24 89

原创 LeetCode - 58. Length of Last Word

58. Length of Last Wordclass Solution(object): def lengthOfLastWord(self, s): """ :type s: str :rtype: int """ arr = s.split(" ") res = 0 ...

2019-06-28 01:39:23 89

原创 LeetCode - 389. Find the Difference

389. Find the Differenceclass Solution(object): def findTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ dic = {} ...

2019-06-28 01:05:10 97

空空如也

空空如也

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

TA关注的人

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