Skip to content

lht19900714/Leetcode_Solutions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python solution for LeetCode

Language License Update SayThanks Travis

Introduction

As a non-CS background programmer. LeetCode is a great website to sharpen my programming/problem-solving skills. Of course, not all solutions here are optimal, but I will add detailed explaination to each of them.

Algorithms



Array

# Title Solution Difficulty Tag Note
0011 Container With Most Water Python Medium Two Pointer
0015 3Sum Python Medium Two Pointer
0016 3Sum Closest Python
Java
Medium Two Pointer
0018 4Sum Python Medium
0026 Remove Duplicates from Sorted Array Python Easy Two Pointer
0027 Remove Element Python Easy Two Pointer
0036 Valid Sudoku Python
Java
Medium
0042 Trapping Rain Water Python
Java
Hard Two Pointer
0045 Jump Game II Python
Java
Medium
0048 Rotate Image Python
Java
Medium
0054 Spiral Matrix Python Medium
0057 Insert Interval Python Hard
0059 Spiral Matrix II Python Medium
0066 Plus One Python Easy
0073 Set Matrix Zeroes Python
Java
Medium
0080 Remove Duplicates from Sorted Array II Python Medium Two Pointer
0118 Pascal's Triangle Python
Java
Easy
0119 Pascal's Triangle II Python Easy
0128 Longest Consecutive Sequence Python
Java
Medium
0134 Gas Station Python Medium
0153 Find Minimum in Rotated Sorted Array Python
Java
Medium Binary Search
0154 Find Minimum in Rotated Sorted Array II Python Hard Binary Search
0169 Majority Element Python Easy
0189 Rotate Array Python Medium
0209 Minimum Size Subarray Sum Python Medium Two Pointer
0229 Majority Element II Python Medium
0238 Product of Array Except Self Python
Java
Medium
0260 Single Number III Python Medium
0283 Move Zeroes Python Easy Two Pointer
0284 Peeking Iterator Python Medium
0289 Game of Life Python Medium
0304 Range Sum Query 2D - Immutable Python
Java
Medium
0307 Range Sum Query - Mutable Python
Java
Medium
0318 Maximum Product of Word Lengths Python
Java
Medium
0334 Increasing Triplet Subsequence Python Medium
0380 Insert Delete GetRandom O(1) Python Medium Hash Set
0384 Shuffle an Array Python
Java
Medium
0435 Non-overlapping Intervals Python Medium Stack/Greedy
0452 Minimum Number of Arrows to Burst Balloons Python Medium Customized Sorting
0454 4Sum II Python Medium
0459 Repeated Substring Pattern Python Easy
0462 Minimum Moves to Equal Array Elements II Python
Java
Medium
0498 Diagonal Traverse Python Medium
0509 Fibonacci Number Java Easy
0581 Shortest Unsorted Continuous Subarray Python Medium Stack
Selection Sort
0598 Range Addition II Python
Java
Medium
0621 Task Scheduler Python Medium Math
0633 Sum of Square Numbers Python
Java
Medium Two Pointer
0665 Non-decreasing Array Python
Java
Medium Two Pointer
0713 Subarray Product Less Than K Python Medium Two Pointer
0792 Number of Matching Subsequences Python
Java
Medium
0795 Number of Subarrays with Bounded Maximum Python
Java
Medium Two Pointer
0848 Shifting Letters Python
Java
Medium
0849 Maximize Distance to Closest Person Python Medium Two Pointer
0870 Advantage Shuffle Python Medium
0880 Decoded String at Index Python Medium
0881 Boats to Save People Python Medium Two Pointer
0915 Partition Array into Disjoint Intervals Python
Java
Medium
0916 Word Subsets Python Medium
0922 Sort Array By Parity II Python
Java
Easy Two Pointer
0929 Unique Email Addresses Python
Java
Easy
0949 Largest Time for Given Digits Python Easy
0973 K Closest Points to Origin Python Medium Customized Sorting
0986 Interval List Intersections Python
Java
Medium Two Pointer
1004 Max Consecutive Ones III Python
Java
Medium Two Pointer
1010 Pairs of Songs With Total Durations Divisible by 60 Python Medium
1094 Car Pooling Python Medium
1291 Sequential Digits Python Medium
1329 Sort the Matrix Diagonally Python Medium Customized Sorting
1338 Reduce Array Size to The Half Python
Java
Medium Customized Sorting
1423 Maximum Points You Can Obtain from Cards Python
Java
Medium Sliding Window
1465 Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts Python
Java
Medium
1658 Minimum Operations to Reduce X to Zero Python Medium Sliding Window
1663 Smallest String With A Given Numeric Value Python Medium
1679 Max Number of K-Sum Pairs Python Medium Two Pointer
1695 Maximum Erasure Value Python
Java
Medium Two Pointer
1710 Maximum Units on a Truck Python
Java
Easy


Binary Search

# Title Solution Difficulty Tag Note
0004 Median of Two Sorted Arrays Python Hard
0033 Search in Rotated Sorted Array Python Medium
0034 Find First and Last Position of Element in Sorted Array Python
Java
Medium
0069 Sqrt(x) Python Easy
0074 Search a 2D Matrix Python Medium
0081 Search in Rotated Sorted Array II Python Medium
0162 Find Peak Element Python Medium
0240 Search a 2D Matrix II Python Medium
0295 Find Median from Data Stream Python Hard
0367 Valid Perfect Square Python Easy
0436 Find Right Interval Python Medium
0540 Single Element in a Sorted Array Python
Java
Medium
0658 Find K Closest Elements Python
Java
Medium Customized Sorting
1283 Find the Smallest Divisor Given a Threshold Python Medium


Breadth-First Search

# Title Solution Difficulty Tag Note
0133 Clone Graph Python Medium BFS/DFS
0207 Course Schedule Python Medium Topological Ordering
0210 Course Schedule II Python Medium Topological Ordering
0310 Minimum Height Trees Python Medium Topological Ordering
0417 Pacific Atlantic Water Flow Python Medium BFS/DFS
0542 01 Matrix Python
Java
Medium BFS
0752 Open the Lock Python
Java
Medium BFS
0994 Rotting Oranges Python Medium BFS
1091 Shortest Path in Binary Matrix Python Medium BFS
1302 Deepest Leaves Sum Python
Java
Medium BFS/DFS
1306 Jump Game III Python
Java
Medium BFS/DFS


Depth-First Search

# Title Solution Difficulty Tag Note
0022 Generate Parentheses Python
Java
Medium Backtracking
0037 Sudoku Solver Python Hard Backtracking
0051 N-Queens Python
Java
Hard Backtracking
0052 N-Queens II Python
Java
Hard Backtracking
0079 Word Search Python
Java
Medium
0090 Subsets II Python
Java
Medium Backtracking
0131 Palindrome Partitioning Python Medium Backtracking
0200 Number of Islands Python Medium DFS/BFS
0212 Word Search II Python
Java
Hard DFS/Backtracking/Trie
0216 Combination Sum III Python Medium Backtracking
0377 Combination Sum IV Python
Java
Medium Recursion with Memoization
0473 Matchsticks to Square Python
Java
Medium
0576 Out of Boundary Paths Python
Java
Medium Recursion with Memoization
0684 Redundant Connection Python
Java
Medium
0695 Max Area of Island Python
Java
Medium
0745 Prefix and Suffix Search Python Hard Trie
0784 Letter Case Permutation Python Medium Backtracking
0841 Keys and Rooms Python Medium
0877 Stone Game Python
Java
Medium
1239 Maximum Length of a Concatenated String with Unique Characters Python
Java
Medium Backtracking
1510 Stone Game IV Python Hard Recursion with Memoization


Dynamic Programming

# Title Solution Difficulty Tag Note
0005 Longest Palindromic Substring Python Medium Two Pointer
0055 Jump Game Python Medium
0062 Unique Paths Python
Java
Medium
0063 Unique Paths II Python
Java
Medium
0070 Climbing Stairs Python Easy Recursion with Memoization
0091 Decode Ways Python Medium Recursion with Memoization
0097 Interleaving String Python
Java
Medium Recursion with Memoization
0120 Triangle Python
Java
Medium Recursion with Memoization
0121 Best Time to Buy and Sell Stock Python Easy
0122 Best Time to Buy and Sell Stock II Python Easy
0123 Best Time to Buy and Sell Stock III Python Hard
0139 Word Break Python Medium Recursion with Memoization
0140 Word Break II Python Hard Recursion with Memoization
0152 Maximum Product Subarray Python Medium
0198 House Robber Python
Java
Medium
0213 House Robber II Python Medium
0221 Maximal Square Python
Java
Medium
0309 Best Time to Buy and Sell Stock with Cooldown Python Medium
0322 Coin Change Python Medium DFS
0337 House Robber III Python Medium DFS
0376 Wiggle Subsequence Python Medium
0413 Arithmetic Slices Python Medium
0532 K-diff Pairs in an Array Python Medium
0583 Delete Operation for Two Strings Python
Java
Medium
0673 Number of Longest Increasing Subsequence Python Medium
0714 Best Time to Buy and Sell Stock with Transaction Fee Python Medium
0724 Find Pivot Index Python Easy
0746 Min Cost Climbing Stairs Python
Java
Easy
0799 Champagne Tower Python Medium
0838 Push Dominoes Python
Java
Medium
0845 Longest Mountain in Array Python Medium
0926 Flip String to Monotone Increasing Python
Java
Medium
0983 Minimum Cost For Tickets Python
Java
Medium
1048 Longest String Chain Python
Java
Medium
1143 Longest Common Subsequence Python Medium
1235 Maximum Profit in Job Scheduling Python
Java
Hard


Graph

# Title Solution Difficulty Tag Note
0332 Reconstruct Itinerary Python Medium DFS
0797 All Paths From Source to Target Python
Java
Medium BFS/DFS
0863 All Nodes Distance K in Binary Tree Python Medium


Hash Table

# Title Solution Difficulty Tag Note
0001 Two Sum Python Easy
0049 Group Anagrams Python
Java
Medium
0076 Minimum Window Substring Python Hard Sliding Window
0146 LRU Cache Python Medium
0290 Word Pattern Python Easy
0299 Bulls and Cows Python Easy
0347 Top K Frequent Elements Python Medium Heap
0460 LFU Cache Python Hard
0554 Brick Wall Python
Java
Medium
0609 Find Duplicate File in System Python
Java
Medium
0677 Map Sum Pairs Python
Java
Medium
0692 Top K Frequent Words Python Medium Customized Sort
0953 Verifying an Alien Dictionary Python Easy Customized Sort
0954 Array of Doubled Pairs Python
Java
Medium
0966 Vowel Spellchecker Python Medium


Linked List

# Title Solution Difficulty Tag Note
0002 Add Two Numbers Python Medium
0021 Merge Two Sorted Lists Python Easy
0023 Merge k Sorted Lists Python Hard
0024 Swap Nodes in Pairs Python Medium
0061 Rotate List Python Medium
0086 Partition List Python
Java
Medium
0092 Reverse Linked List II Python
Java
Medium
0138 Copy List with Random Pointer Python Medium
0148 Sort List Python Medium
0141 Linked List Cycle Python Easy Two Pointer
0142 Linked List Cycle II Python Medium Two Pointer
0143 Reorder List Python Medium
0147 Insertion Sort List Python Medium
0206 Reverse Linked List Python
Java
Easy
0234 Palindrome Linked List Python Easy
0328 Odd Even Linked List Python
Java
Medium
0382 Linked List Random Node Python Medium
0445 Add Two Numbers II Python Medium


Math

# Title Solution Difficulty Tag Note
0478 Generate Random Point in a Circle Python Medium
1344 Angle Between Hands of a Clock Python Medium
1492 The kth Factor of n Python Medium
1551 Minimum Operations to Make Array Equal Python Medium


Queue

# Title Solution Difficulty Tag Note
0622 Design Circular Queue Python Medium
0641 Design Circular Deque Python Medium
0933 Number of Recent Calls Python Easy
0948 Bag of Tokens Python Medium
1649 Jump Game VI Python
Java
Medium Deque


Recursion

# Title Solution Difficulty Tag Note
0046 Permutations Python Medium Backtracking/DFS
0047 Permutations II Python Medium Backtracking/DFS
0050 Pow(x,n) Python Medium
0077 Combinations Python Medium Backtracking/DFS
0967 Numbers With Same Consecutive Differences Python Medium BFS/DFS/Backtracking
1286 Iterator for Combination Python Medium Backtracking/DFS


String

# Title Solution Difficulty Tag Note
0012 Integer to Roman Python Medium
0013 Roman to Integer Python Easy
0017 Letter Combinations of a Phone Number Python Medium BFS/DFS/Backtracking
0065 Valid Number Python Hard Regex
0151 Reverse Words in a String Python Medium
0165 Compare Version Numbers Python Medium
0227 Basic Calculator II Python Medium Regex
0451 Sort Characters By Frequency Python
Java
Medium
0524 Longest Word in Dictionary through Deleting Python Medium Customized Sort
Two Pointer
0556 Next Greater Element III Python Medium
0567 Permutation in String Python Medium Sliding Window
Two Pointer
0763 Partition Labels Python Medium Two Pointer
0816 Ambiguous Coordinates Python
Java
Medium
0820 Short Encoding of Words Python Medium
0856 Score of Parentheses Python Medium Stack
DFS
0859 Buddy Strings Python Easy
0890 Find and Replace Pattern Python
Java
Medium
0923 3Sum With Multiplicity Python Medium Two Pointer
1268 Search Suggestions System Python
Java
Medium
1657 Determine if Two Strings Are Close Python Medium


Stack

# Title Solution Difficulty Tag Note
0056 Merge Intervals Python Medium
0071 Simplify Path Python Medium
0084 Largest Rectangle in Histogram Python Hard Monotonic stack
0150 Evaluate Reverse Polish Notation Python
Java
Medium
0155 Min Stack Python
Java
Easy
0232 Implement Queue using Stacks Java Easy
0316 Remove Duplicate Letters Python Medium
0394 Decode String Python Medium
0456 132 Pattern Python Medium
0496 Next Greater Element I PythonJava Easy
0503 Next Greater Element II Python Medium
0735 Asteroid Collision Python Medium
0739 Daily Temperatures Python Medium
0895 Maximum Frequency Stack Python Hard
0921 Minimum Add to Make Parentheses Valid Python Medium
0946 Validate Stack Sequences Python Medium
1209 Remove All Adjacent Duplicates in String II Python
Java
Medium
1249 Minimum Remove to Make Valid Parentheses Python Medium
1288 Remove Covered Intervals Python Medium Customized Sort
1673 Find the Most Competitive Subsequence Python Medium


Tree

# Title Solution Difficulty Tag Note
0082 Remove Duplicates from Sorted List II Python Medium
0083 Remove Duplicates from Sorted List Python Easy
0094 Binary Tree Inorder Traversal Python Medium
0095 Unique Binary Search Trees II Python
Java
Medium
0098 Validate Binary Search Tree Python Medium
0099 Recover Binary Search Tree Python Hard
0100 Same Tree Python Easy BFS
0101 Symmetric Tree Python Easy DFS
0102 Binary Tree Level Order Traversal Python
Java
Medium BFS
0103 Binary Tree Zigzag Level Order Traversal Python Medium BFS
0104 Maximum Depth of Binary Tree Python Medium BFS/DFS
0105 Construct Binary Tree from Preorder and Inorder Traversal Python
Java
Medium
0106 Construct Binary Tree from Inorder and Postorder Traversal Python
Java
Medium
0107 Binary Tree Level Order Traversal II Python Easy BFS
0108 Convert Sorted Array to Binary Search Tree Python
Java
Easy DFS
0109 Convert Sorted List to Binary Search Tree Python
Java
Medium DFS
0110 Balanced Binary Tree Python Easy DFS
0112 Path Sum Python Easy BFS/DFS
0113 Path Sum II Python
Java
Medium BFS/DFS
0114 Flatten Binary Tree to Linked List Python
Java
Medium
0116 Populating Next Right Pointers in Each Node Python Medium
0117 Populating Next Right Pointers in Each Node II Python Medium
0173 Binary Search Tree Iterator Python Medium
0199 Binary Tree Right Side View Python Medium
0208 Implement Trie (Prefix Tree) Python)
Java
Medium
0211 Add and Search Word - Data structure design Python Medium
0236 Lowest Common Ancestor of a Binary Tree Python
Java
Medium DFS
0404 Sum of Left Leaves Python
Java
Easy BFS/DFS
0429 N-ary Tree Level Order Traversal Python
Java
Medium BFS
0437 Path Sum III Python Medium BFS/DFS
0450 Delete Node in a BST Python
Java
Medium
0538 Convert BST to Greater Tree Python Medium DFS
0543 Diameter of Binary Tree Python
Java
Easy
0563 Binary Tree Tilt Python Easy
0589 N-ary Tree Preorder Traversal Python
Java
Easy BFS/DFS
0590 N-ary Tree Postorder Traversal Python
Java
Easy BFS/DFS
0623 Add One Row to Tree Python Medium DFS
0669 Trim a Binary Search Tree Python Medium
0700 Search in a Binary Search Tree Python Easy BFS/DFS
0701 Insert into a Binary Search Tree Python Medium
0814 Binary Tree Pruning Python
Java
Medium DFS
0865 Smallest Subtree with all the Deepest Nodes Python Medium
0897 Increasing Order Search Tree Python Easy
0938 Range Sum of BST Python
Java
Easy
0987 Vertical Order Traversal of a Binary Tree Python Medium
0993 Cousins in Binary Tree Python
Java
Easy
1008 Construct Binary Search Tree from Preorder Traversal Python
Java
Medium
1022 Sum of Root To Leaf Binary Numbers Python Easy BFS/DFS
1026 Maximum Difference Between Node and Ancestor Python Medium
1032 Stream of Characters Python Hard Trie
1305 All Elements in Two Binary Search Trees Python Medium
1339 Maximum Product of Splitted Binary Tree Python
Java
Medium
1379 Find a Corresponding Node of a Binary Tree in a Clone of That Tree Python Medium
1448 Count Good Nodes in Binary Tree Python
Java
Medium
1457 Pseudo-Palindromic Paths in a Binary Tree Python Medium
1721 Swapping Nodes in a Linked List Python Medium


About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages