/leetcode

Cpp & Python Solution for Leetcode with basic idea

Primary LanguagePython

leetcode

Cpp and Python Solution for Leetcode with Basic idea. And the specific method is described in the first solution.

Title Solution Difficulty Basic idea
1. Two Sum Python Easy 1. two-pointer
2. hashtable
7. Reverse Integer:star: Python Easy 1. mod and floor divide in Python3
12. Integer to Roman Python Medium decode mapping with greedy thinking
13. Roman to Integer Python Easy string traversal
20. Valid Parentheses Cpp Python Easy stack
24. Swap Nodes in Pairs Cpp Medium recursion
26. Remove Duplicates from Sorted Array Python Easy double pointer
27. Remove Element Python Easy double pointer
28. Implement strStr() Python Easy double pointer
33. Search in Rotated Sorted Array Python Medium binary search
53. Maximum Subarray Python Easy DP
70. Climbing Stairs Python Easy DP, recursive
80. Remove Duplicates from Sorted Array II Python Medium two pointer
81. Search in Rotated Sorted Array II Python Medium binary search
88. Merge Sorted Array Python Easy two pointer, reverse insertion
91. Decode Ways Python Medium DP
94. Binary Tree Inorder Traversal Cpp Python Medium 1. DFS recursion
2. explicit stack
118. Pascal's Triangle Python Easy iter/recur
119. Pascal's Triangle II Python Easy iter/recur
130. Surrounded Regions Python Medium BFS-2dimension
133. Clone Graph Cpp Python Medium 1. DFS, visited hashmap
2. BFS: add all neighbors to queue and traverse
137. Single Number II Python Medium bitwise
138. Copy List with Random Pointer Python Medium 1.backtracing
2.continuous copied node in space
150. Evaluate Reverse Polish Notation Python Medium stack
153. Find Minimum in Rotated Sorted Array Python Medium binary search
155. Min Stack Cpp Easy 1. two stack to store data and minimum
2. pair<data, minimum>, and var min to store current minimum.
168. Excel Sheet Column Title Python Easy DFS
iteration
171. Excel Sheet Column Number Python Easy iteration 26-binary form
179. Largest Number Python Medium sort through splicing two number as string
198. House Robber Python Medium DP through the first two results of optimal solution
200. Number of Islands Cpp Python Medium 1. DFS: search to the depth of one element
2. BFS: search all neighbors of one element
204. Count Primes Python Easy set multiple of prime as composite number through prime list
208. Implement Trie (Prefix Tree) ⭐ Python Medium root trie recursive
213. House Robber II Python Medium divide two part to get local solution through DP
218. The Skyline Problem Python Hard scan + priority-queue
220. Contains Duplicate III Python Medium 1. bisection
225. Implement Stack using Queues Python Easy two queue operation to implement stack when pushing elements
231. Power of Two Python Easy bitwise, n as divisor
232. Implement Queue using Stacks Python Easy twice stack operation to implement queue
263. Ugly Number Python Easy factorization through 2,3,5
264. Ugly Number II Python Medium minimum heap
274. H-Index Python Medium 1. Dichotomy
2. Counter array
275. H-Index II Python Medium 1. Dichotomy
2. traverse
279. Perfect Squares Cpp Medium BFS
300. Longest Increasing Subsequence Python Medium DP
307. Range Sum Query - Mutable Python Medium Segment Tree
342. Power of Four Python Easy 1. sqrt+powerOf2
2. Bitwise
344. Reverse String Cpp/Python Easy 1. double pointer
2. iteration or recursion of swap in place
345. Reverse Vowels of a String Python Medium 1. recursive with cache
2. DP
377. Combination Sum IV Python Medium 1. recursive with cache
2. DP
394. Decode String Python Medium 1. assistant stack
2. DFS
395. Longest Substring with At Least K Repeating Characters Python Medium DC
421. Maximum XOR of Two Numbers in an Array Python Medium bitwise
424. Longest Repeating Character Replacement Python Medium slide window with two pointer
- char number counter with list
- r-l+1-window_maxn > k
448. Find All Numbers Disappeared in an Array Python Easy ~
451. Sort Characters By Frequency Python Medium ordered dict
461. Hamming Distance Python Easy XOR+NumberOf1
477. Total Hamming Distance Python Medium Bitwise
485. Max Consecutive Ones Python Easy traverse and find max_count: return max(max_count, count)
494. Target Sum Cpp Medium 1. DFS: enumerate all possibility
509. Fibonacci Number Python Easy recur+hashmap (memerization to reduce repeated calculation)
561. Array Partition I Python Easy sort and combine
567. Permutation in String Python Medium slide window
576. Out of Boundary Paths Python Medium 1. DFS with memorization
2. DP
581. Shortest Unsorted Continuous Subarray Python Medium 1. sort and compare
2. find the law
633. Sum of Square Numbers Python Medium 1. enumeration
2. double pointer
643. Maximum Average Subarray I Python Easy slide window, start pointer is outside of initial window
645. Set Mismatch Python Easy count-array
665. Non-decreasing Array Python Easy tranverse to find valley and judge index of i-1, i, i+1
692. Top K Frequent Words Python Medium 1. hashtable+sort
2. priority queue
697. Degree of an Array Python Easy hashtable records [counts, left_one right_one]
703. Kth Largest Element in a Stream Python Easy python heapq in no need of sorted operation
704. Binary Search♥ Python Easy 1. two pointer
2. recursive
726. Number of Atoms Python Hard stack with hashtable
739. Daily Temperatures Python Medium Monotonic stack
740. Delete and Earn Python Medium DP(rob house)
743. Network Delay Time Python Medium Dijkstra
752. Open the Lock♥ Cpp Medium BFS: transform to graph to find the shortest path
783. Minimum Distance Between BST Nodes Python Easy in-order traverse
832. Flipping an Image Python Easy 1. double pointer to reverse list
2. ^ XOR
872. Leaf-Similar Trees Python Easy DFS yield
888. Fair Candy Swap Python Easy hash table
896. Monotonic Array Python Easy set inc and des flags
897. Increasing Order Search Tree Python Easy in_order traversal
912. Sort an Array Python Medium 1. quick sort
930. Binary Subarrays With Sum Python Medium 1. Prefix sum
2. slide window
938. Range Sum of BST Python Easy BFS, DFS
978. Longest Turbulent Subarray Python Medium 1. slide window
2. Dynamic Programming
993. Cousins in Binary Tree Python Easy BFS, DFS (nonlocal)
1006. Clumsy Factorial Python Medium 1. stack to expression calculator with operator
1011. Capacity To Ship Packages Within D Days Python Medium binary search with greedy thinking
1035. Uncrossed Lines Python Medium 2D-DP
1052. Grumpy Bookstore Owner Python Medium 1. slide window 生气的老板
1143. Longest Common Subsequence Python Medium 1. 2D-DP
1190. Reverse Substrings Between Each Pair of Parentheses Python Medium 1. stack/recursive
1310. XOR Queries of a Subarray Python Medium XOR, prefix sum
1337. The K Weakest Rows in a Matrix Python Easy Dichotomy+Mini-Heap
1418. Display Table of Food Orders in a Restaurant Python Medium Hashtable
1423. Maximum Points You Can Obtain from Cards Python Medium slide window + reverse thinking
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit Python Medium slide window + sortedlist(sortedcontainer)
1442. Count Triplets That Can Form Two Arrays of Equal XOR Python Medium XOR, prefix sum, mapping
1482. Minimum Number of Days to Make m Bouquets Python Medium binary search with checkDay func
1486. XOR Operation in an Array Python Easy XOR
1711. Count Good Meals Python Medium hashtable, traverse targets
1720. Decode XORed Array Python Easy XOR
1734. Decode XORed Permutation Python Medium XOR
1736. Latest Time by Replacing Hidden Digits Python Easy Greedy, bitwise judgement
1738. Find Kth Largest XOR Coordinate Value Python Medium Prefix sum and sort
1818. Minimum Absolute Sum Difference Python Medium bisect
1833. Maximum Ice Cream Bars Python Medium sort+greedy
1893. Check if All the Integers in a Range Are Covered Python Easy diff array + prefix sum
----- -------- :--------: ----------
LCP 07. 传递信息 Python Easy DFS
面试题 17.10. Find Majority Element LCCI Python Easy Boyer-Moore vote algo to find mode

Popular Tags

Breath-first Search

133. Clone Graph
200. Number of Islands

Depth-first Search

94. Binary Tree Inorder Traversal
133. Clone Graph
200. Number of Islands
394. Decode String
494. Target Sum
576. Out of Boundary Paths
872. Leaf-Similar Trees
938. Range Sum of BST
993. Cousins in Binary Tree

Dynamic Programming

494. Target Sum
576. Out of Boundary Paths

Slide Window

930. Binary Subarrays With Sum

Linked List

138. Copy List with Random Pointer

Prefix sum

1893. Check if All the Integers in a Range Are Covered