Skip to content

Instantly share code, notes, and snippets.

View yangjun2020's full-sized avatar
🏝️
Working from home

Jun yangjun2020

🏝️
Working from home
  • Cleveland, OH
View GitHub Profile
@yangjun2020
yangjun2020 / removeNthFromEnd.py
Created July 19, 2020 17:18
LinkedList - Medium
# 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.
@yangjun2020
yangjun2020 / copyRandomList.py
Created July 19, 2020 15:02
LinkedList - Medium
"""
# 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/
'''
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:
'''
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:
'''
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:]
'''
@yangjun2020
yangjun2020 / PermutationsII.py
Created July 2, 2020 19:52
Permutations II: Medium
'''
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
'''
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.
'''
'''
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):
'''
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:
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)