This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Definition for singly-linked list. | |
| # class ListNode: | |
| # def __init__(self, val=0, next=None): | |
| # self.val = val | |
| # self.next = next | |
| ''' | |
| Key Takeaway: | |
| 1. the one pass algorithm gracefully helps the second pointer to locate at the node prior to the deletion node. | |
| 2. the use the of dummy node is also important here to initilize the pointers and to serve as a returning node. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| # Definition for a Node. | |
| class Node: | |
| def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): | |
| self.val = int(x) | |
| self.next = next | |
| self.random = random | |
| https://leetcode.com/problems/copy-list-with-random-pointer/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| https://leetcode.com/problems/3sum/ | |
| Key takeaways: | |
| 1. sorting is necessary | |
| 2. skipping the duplicate value is a bit tricky | |
| 3. this is a good example of using two pointers and be clear with your condition to move the pointers | |
| ''' | |
| class Solution: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Key takeaway: | |
| 1. You need to be familiar with binary search to begin with. | |
| 2. Observe the new pattern and tweak the binary search. | |
| ''' | |
| class Solution: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| https://leetcode.com/problems/valid-palindrome-ii/ | |
| Key Takeaways: | |
| 1. Valid palindrome means the string is identical to its reversed string: s == s[::-1] | |
| 2. You can skip the character in a string and no need to check on the bountry: s[:left] + s[left+1:] | |
| ''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| https://leetcode.com/problems/permutations-ii/ | |
| apply the backtracking technique. | |
| Here we have the exit: when your path has len(path) == len(self.n) | |
| ''' | |
| from collections import Counter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| https://leetcode.com/problems/next-permutation/ | |
| Key takeaways: | |
| It's hard to see a pattern at the beginning. | |
| This methold uses two pointers i and j to slide from left to right and compare for swap. | |
| Don't forget to reserve the partial nums in the end. | |
| ''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| https://leetcode.com/problems/add-strings | |
| ''' | |
| class Solution: | |
| def addStrings(self, num1: str, num2: str) -> str: | |
| if (not num1 and num2): | |
| return num2 | |
| elif (not num2 and num1): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| https://leetcode.com/problems/add-two-numbers/ | |
| Key takeaways: | |
| Thinking about how the value can be carried over from one position to another position. | |
| This also requires one to understand the basic math of the difference between "carry%10" and "carry//10" | |
| ''' | |
| class Solution: | |
| def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def longestPalindrome(self, s: str) -> str: | |
| ''' | |
| https://leetcode.com/problems/longest-palindromic-substring/submissions/ | |
| dp | |
| 复杂度分析 | |
| 设字符串长度为n。 | |
| 时间复杂度O(n^2) | |
| 枚举端点,O(1)时间转移,时间复杂度为O(n^2)。 | |
| 空间复杂度O(n^2) |