├── .idea
├── .gitignore
├── misc.xml
└── vcs.xml
├── others
├── Recursion & Backtracking
│ ├── backtracking.md
│ ├── .DS_Store
│ ├── sudoku.png
│ ├── sudoku1.png
│ ├── backtracking.jpg
│ ├── phone_number.png
│ ├── .idea
│ │ ├── vcs.xml
│ │ ├── .gitignore
│ │ ├── misc.xml
│ │ └── Recursion & Backtracking.iml
│ ├── subsets
│ │ └── subsets.iml
│ ├── subsets-ii
│ │ └── subsets-ii.iml
│ ├── combinations
│ │ └── combinations.iml
│ ├── permutations
│ │ └── permutations.iml
│ ├── generate-parentheses
│ │ └── generate-parentheses.iml
│ ├── count-sorted-vowel-strings
│ │ └── count-sorted-vowel-strings.iml
│ ├── iterator-for-combination
│ │ └── iterator-for-combination.iml
│ └── letter-combinations-of-a-phone-number
│ │ └── letter-combinations-of-a-phone-number.iml
├── .DS_Store
├── Stacks & Queues
│ └── .DS_Store
├── cpp
│ ├── 500.Keyboard Row.cpp
│ ├── 645.Set Mismatch.cpp
│ ├── 724.Find Pivot Index.cpp
│ ├── 766.Toeplitz Matrix.cpp
│ ├── 942.DI String Match.cpp
│ ├── 1046.Last Stone Weight.cpp
│ ├── 622.Design Circular Queue.cpp
│ ├── 917.Reverse Only Letters.cpp
│ ├── 941.Valid Mountain Array.cpp
│ ├── 496.Next Greater Element I.cpp
│ ├── 532.K-diff Pairs in an Array.cpp
│ ├── 345.Reverse Vowels of a String.cpp
│ ├── 830.Positions of Large Groups.cpp
│ ├── 350.Intersection of Two Arrays II.cpp
│ ├── 374.Guess Number Higher or Lower.cpp
│ ├── 557.Reverse Words in a String III.cpp
│ ├── 387.First Unique Character in a String.cpp
│ ├── 747.Largest Number At Least Twice of Others.cpp
│ ├── 674.Longest Continuous Increasing Subsequence.cpp
│ ├── 709.To Lower Case.cpp
│ ├── 001.Two Sum.cpp
│ └── 231.Power of Two.cpp
├── Tree, Binary Tree, BST
│ ├── 538.png
│ ├── .DS_Store
│ ├── se_de.jpg
│ ├── unival.png
│ ├── ex_depth.jpg
│ ├── mirror_1.png
│ ├── mirror_2.png
│ ├── 116_sample.png
│ ├── binary_tree.png
│ ├── insert_bst.jpg
│ ├── .idea
│ │ ├── vcs.xml
│ │ ├── .gitignore
│ │ ├── misc.xml
│ │ └── Tree, Binary Tree, BST.iml
│ ├── maximum-depth-of-binary-tree
│ │ ├── src
│ │ │ └── Solution.java
│ │ └── maximum-depth-of-binary-tree.iml
│ ├── count-complete-tree-nodes
│ │ ├── src
│ │ │ └── Solution.java
│ │ └── count-complete-tree-nodes.iml
│ ├── path-sum
│ │ ├── path-sum.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── symmetric-tree
│ │ ├── symmetric-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── inorder-traversal
│ │ ├── inorder-traversal.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── invert-binary-tree
│ │ ├── invert-binary-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── maximum-binary-tree
│ │ └── maximum-binary-tree.iml
│ ├── postorder-traversal
│ │ ├── postorder-traversal.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── preorder-traversal
│ │ ├── preorder-traversal.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── univalued-binary-tree
│ │ ├── univalued-binary-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── convert-bst-to-greater-tree
│ │ ├── convert-bst-to-greater-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── validate-binary-search-tree
│ │ ├── validate-binary-search-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── kth-smallest-element-in-a-bst
│ │ ├── kth-smallest-element-in-a-bst.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── minimum-depth-of-binary-tree
│ │ ├── minimum-depth-of-binary-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── insert-into-a-binary-search-tree
│ │ ├── insert-into-a-binary-search-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── search-in-a-binary-search-tree
│ │ ├── search-in-a-binary-search-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── flatten-binary-tree-to-linked-list
│ │ ├── flatten-binary-tree-to-linked-list.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── serialize-and-deserialize-binary-tree
│ │ └── serialize-and-deserialize-binary-tree.iml
│ ├── convert-sorted-array-to-binary-search-tree
│ │ ├── convert-sorted-array-to-binary-search-tree.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── populating-next-right-pointers-in-each-node
│ │ ├── populating-next-right-pointers-in-each-node.iml
│ │ └── src
│ │ │ └── Solution.java
│ ├── construct-binary-tree-from-preorder-and-inorder-traversal
│ │ └── construct-binary-tree-from-preorder-and-inorder-traversal.iml
│ └── construct-binary-tree-from-inorder-and-postorder-traversal
│ │ └── construct-binary-tree-from-inorder-and-postorder-traversal.iml
├── .idea
│ ├── vcs.xml
│ ├── .gitignore
│ ├── misc.xml
│ └── others.iml
└── Python3
│ ├── 1108.Defanging an IP Address.py
│ ├── 292.Nim Game.py
│ ├── 1389.Create Target Array in the Given Order.py
│ ├── 5315.Maximum 69 Number.py
│ ├── 557.Reverse Words in a String III.py
│ ├── 1185.Day of the Week.py
│ ├── 1351.Count Negative Numbers in a Sorted Matrix.py
│ ├── 151.Reverse Words in a String.py
│ ├── 908.Smallest Range I.py
│ ├── 461.Hamming Distance.py
│ ├── 1184.Distance Between Bus Stops.py
│ ├── 1287.Element Appearing More Than 25% In Sorted Array.py
│ ├── 888.Fair Candy Swap.py
│ ├── 1347.Minimum Number of Steps to Make Two Strings Anagram.py
│ ├── 223.Rectangle Area.py
│ ├── 989.Add to Array-Form of Integer.py
│ ├── 1221.Split a String in Balanced Strings.py
│ ├── 1346.Check If N and Its Double Exist.py
│ ├── 415.Add Strings.py
│ ├── 049.Group Anagrams.py
│ ├── 169.Majority Element.py
│ ├── 1217.Play with Chips.py
│ ├── 1360.Number of Days Between Two Dates.py
│ ├── 448.Find All Numbers Disappeared in an Array.py
│ ├── 066.Plus One.py
│ ├── 168.Excel Sheet Column Title.py
│ ├── 171.Excel Sheet Column Number.py
│ ├── 509.Fibonacci Number.py
│ ├── 1342.Number of Steps to Reduce a Number to Zero.py
│ ├── 1344.Angle Between Hands of a Clock.py
│ ├── 1207.Unique Number of Occurrences.py
│ ├── 217.Contains Duplicate.py
│ ├── 1016.Binary String With Substrings Representing 1 To N.py
│ ├── 946.Validate Stack Sequences.py
│ ├── 1313.Decompress Run-Length Encoded List.py
│ ├── 1324.Print Words Vertically.py
│ ├── 389.Find the Difference.py
│ ├── 1281.Subtract the Product and Sum of Digits of an Integer.py
│ ├── 836.Rectangle Overlap.py
│ ├── 1078.Occurrences After Bigram.py
│ ├── 070.Climbing Stairs.py
│ ├── 1306.Jump Game III.py
│ ├── 242.Valid Anagram.py
│ ├── 347.Top K Frequent Elements.py
│ ├── 1154.Day of the Year.py
│ ├── 824.Goat Latin.py
│ ├── 859.Buddy Strings.py
│ ├── 1189.Maximum Number of Balloons.py
│ ├── 442.Find All Duplicates in an Array.py
│ ├── 007.Reverse Integer.py
│ ├── 1390.Four Divisors.py
│ ├── 162.Find Peak Element.py
│ ├── 189.Rotate Array.py
│ ├── 118.Pascal's Triangle.py
│ ├── 122.Best Time to Buy and Sell Stock II.py
│ ├── 929.Unique Email Addresses.py
│ ├── 1089.Duplicate Zeros.py
│ ├── 062.Unique Paths.py
│ ├── 412.Fizz Buzz.py
│ ├── 006.ZigZag Conversion.py
│ ├── 1266.Minimum Time Visiting All Points.py
│ ├── 1331.Rank Transform of an Array.py
│ ├── 104.Maximum Depth of Binary Tree.py
│ ├── 1137.N-th Tribonacci Number.py
│ ├── 056.Merge Intervals.py
│ ├── 219.Contains Duplicate II.py
│ ├── 013.Roman to Integer.py
│ ├── 234.Palindrome Linked List.py
│ ├── 1051.Height Checker.py
│ ├── 020.Valid Parentheses.py
│ ├── 011.Container With Most Water.py
│ ├── 141.Linked List Cycle.py
│ ├── 077.Combinations.py
│ ├── 209.Minimum Size Subarray Sum.py
│ ├── 1252.Cells with Odd Values in a Matrix.py
│ ├── 1295.Find Numbers with Even Number of Digits.py
│ ├── 148.Sort List.py
│ ├── 121.Best Time to Buy and Sell Stock.py
│ ├── 1317.Convert Integer to the Sum of Two No-Zero Integers.py
│ ├── 1328.Break a Palindrome.py
│ ├── 590.N-ary Tree Postorder Traversal.py
│ ├── 1338.Reduce Array Size to The Half.py
│ ├── 844.Backspace String Compare.py
│ ├── 643.Maximum Average Subarray I.py
│ ├── 965.Univalued Binary Tree.py
│ ├── 1304.Find N Unique Integers Sum up to Zero.py
│ ├── 1103.Distribute Candies to People.py
│ ├── 1160.Find Words That Can Be Formed by Characters.py
│ ├── 819.Most Common Word.py
│ ├── 704.Binary Search.py
│ ├── 1337.The K Weakest Rows in a Matrix.py
│ ├── 290.Word Pattern.py
│ ├── 746.Min Cost Climbing Stairs.py
│ ├── 1200.Minimum Absolute Difference.py
│ ├── 1232.Check If It Is a Straight Line.py
│ ├── 053.Maximum Subarray.py
│ ├── 1260.Shift 2D Grid.py
│ ├── 206.Reverse Linked List.py
│ ├── 100.Same Tree.py
│ ├── 589.N-ary Tree Preorder Traversal.py
│ └── 788.Rotated Digits.py
├── .DS_Store
├── circular-queue.png
├── exercises
├── exercises.iml
├── two-pointers
│ ├── two-pointers.iml
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ ├── TwoSumII.java
│ │ │ ├── RemoveDuplicates.java
│ │ │ └── TwoSumLessThanK.java
│ └── pom.xml
├── array
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ ├── RotateArray.java
│ │ │ ├── ToeplitzMatrix.java
│ │ │ └── ConsecutiveCharacters.java
│ └── pom.xml
├── .idea
│ ├── vcs.xml
│ ├── .gitignore
│ ├── modules.xml
│ ├── misc.xml
│ ├── compiler.xml
│ └── jarRepositories.xml
├── linked-list
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ └── ListNode.java
│ └── pom.xml
├── tree
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ ├── TreeNode.java
│ │ │ ├── ConvertBST.java
│ │ │ ├── SearchBST.java
│ │ │ ├── IsUnivalTree.java
│ │ │ ├── IsSameTree.java
│ │ │ ├── GoodNodes.java
│ │ │ ├── SortedArrayToBST.java
│ │ │ ├── SumEvenGrandparent.java
│ │ │ ├── MaxAncestorDiff.java
│ │ │ ├── RangeSumBST.java
│ │ │ ├── BinaryTreePaths.java
│ │ │ └── GetTargetCopy.java
│ └── pom.xml
├── string
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ ├── ReverseString.java
│ │ │ └── FizzBuzz.java
│ └── pom.xml
├── hashtable
│ └── pom.xml
├── searching
│ └── pom.xml
└── pom.xml
└── interviews
├── interviews.iml
├── bit-manipulation
├── bit-manipulation.iml
└── pom.xml
├── sorting
├── target
│ └── classes
│ │ ├── App.class
│ │ ├── MergeSort.class
│ │ ├── QuickSort.class
│ │ ├── InsertionSort.class
│ │ └── SelectionSort.class
├── src
│ └── main
│ │ └── java
│ │ ├── App.java
│ │ └── InsertionSort.java
└── pom.xml
├── .idea
├── vcs.xml
├── .gitignore
├── misc.xml
└── jarRepositories.xml
├── linked-list
├── src
│ └── main
│ │ └── java
│ │ ├── ListNode.java
│ │ ├── ReverseList.java
│ │ └── RemoveNthFromEnd.java
└── pom.xml
├── two-pointers
├── src
│ └── main
│ │ └── java
│ │ ├── ListNode.java
│ │ └── ReverseString.java
└── pom.xml
├── tree
├── src
│ └── main
│ │ └── java
│ │ ├── MaxDepth.java
│ │ ├── TreeNode.java
│ │ ├── PathSum.java
│ │ ├── MinDepth.java
│ │ ├── InvertBinaryTree.java
│ │ ├── KthSmallest.java
│ │ ├── DiameterOfBinaryTree.java
│ │ └── LowestCommonAncestor.java
└── pom.xml
├── math
├── src
│ └── main
│ │ └── java
│ │ ├── MissingNumber.java
│ │ ├── ExcelSheetColumnNumber.java
│ │ └── PlusOne.java
└── pom.xml
├── dynamic-programming
├── src
│ └── main
│ │ └── java
│ │ └── ClimbStairs.java
└── pom.xml
├── array
├── src
│ └── main
│ │ └── java
│ │ ├── MaxConsecutiveOnes.java
│ │ ├── MoveZeroes.java
│ │ ├── SearchMatrix.java
│ │ ├── MajorityElement.java
│ │ └── RotateArray.java
└── pom.xml
├── graph
└── pom.xml
├── greedy
└── pom.xml
├── string
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── ValidAnagram.java
│ └── ValidPalindrome.java
├── hashtable
├── pom.xml
└── src
│ └── main
│ └── java
│ └── TwoSum.java
├── searching
└── pom.xml
├── binary-search
└── pom.xml
├── stack-and-queue
├── pom.xml
└── src
│ └── main
│ └── java
│ └── DailyTemperatures.java
└── divide-and-conquer
└── pom.xml
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /workspace.xml
--------------------------------------------------------------------------------
/others/Recursion & Backtracking/backtracking.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ceezyyy/leetcode/HEAD/.DS_Store
--------------------------------------------------------------------------------
/circular-queue.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ceezyyy/leetcode/HEAD/circular-queue.png
--------------------------------------------------------------------------------
/others/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ceezyyy/leetcode/HEAD/others/.DS_Store
--------------------------------------------------------------------------------
/exercises/exercises.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/4/19 8 | */ 9 | public class RotateArray { 10 | } 11 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 167. 两数之和 II - 输入有序数组 (Easy) 4 | * Two Sum 变式 5 | * https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/ 6 | *
7 | * 8 | * @author ceezyyy 9 | * @since 2021/4/18 10 | */ 11 | public class TwoSumII { 12 | } 13 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/3/24 8 | */ 9 | class ListNode { 10 | 11 | int val; 12 | ListNode next; 13 | 14 | ListNode(int x) { 15 | val = x; 16 | next = null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /interviews/linked-list/src/main/java/ListNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/3/24 8 | */ 9 | class ListNode { 10 | 11 | int val; 12 | ListNode next; 13 | 14 | ListNode(int x) { 15 | val = x; 16 | next = null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /interviews/two-pointers/src/main/java/ListNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/3/24 8 | */ 9 | class ListNode { 10 | 11 | int val; 12 | ListNode next; 13 | 14 | ListNode(int x) { 15 | val = x; 16 | next = null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /exercises/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 104. Maximum Depth of Binary Tree (Easy) 4 | * https://leetcode.com/problems/maximum-depth-of-binary-tree/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/3/24 9 | */ 10 | class MaxDepth { 11 | public int maxDepth(TreeNode root) { 12 | if (root == null) return 0; 13 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /others/Python3/1108.Defanging an IP Address.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1108. Defanging an IP Address 3 | @Tag: str 4 | @Date: Oct-22 2019 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def defangIPaddr(self, address: str) -> str: 12 | return address.replace('.','[.]') 13 | 14 | 15 | def main(): 16 | s=Solution() 17 | print(s.defangIPaddr("1.1.1.1")) 18 | 19 | 20 | if __name__=="__main__": 21 | main() 22 | 23 | -------------------------------------------------------------------------------- /others/Python3/292.Nim Game.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 292. Nim Game 3 | @Tag: brainteaser 4 | @Date: Jan-25 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def canWinNim(self, n: int) -> bool: 12 | return n % 4 != 0 13 | 14 | 15 | """ 16 | Runtime: 40 ms, faster than 6.96% of Python3 online submissions for Nim Game. 17 | Memory Usage: 12.6 MB, less than 100.00% of Python3 online submissions for Nim Game. 18 | """ 19 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/maximum-depth-of-binary-tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Maximum Depth of Binary Tree 3 | */ 4 | class Solution { 5 | 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | } 11 | 12 | // Bottom-up 13 | public int maxDepth(TreeNode root) { 14 | if (root == null) return 0; 15 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 16 | } 17 | } -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/count-complete-tree-nodes/src/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Count Complete Tree Nodes 3 | */ 4 | class Solution { 5 | 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | } 11 | 12 | public int countNodes(TreeNode root) { 13 | 14 | // Corner case 15 | if (root == null) return 0; 16 | 17 | return countNodes(root.left) + countNodes(root.right) + 1; 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /interviews/math/src/main/java/MissingNumber.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/4/22 8 | */ 9 | public class MissingNumber { 10 | public int missingNumber(int[] nums) { 11 | 12 | int n = nums.length; 13 | int actual = 0; 14 | 15 | for (int num : nums) actual += num; 16 | // Gauss' Formula 17 | int expected = (1 + n) * n / 2; 18 | 19 | return expected - actual; 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /interviews/tree/src/main/java/TreeNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/3/24 8 | */ 9 | class TreeNode { 10 | int val; 11 | TreeNode left; 12 | TreeNode right; 13 | 14 | public TreeNode(int val) { 15 | this.val = val; 16 | } 17 | 18 | public TreeNode(int val, TreeNode left, TreeNode right) { 19 | this.val = val; 20 | this.left = left; 21 | this.right = right; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /exercises/tree/src/main/java/TreeNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/3/24 8 | */ 9 | public class TreeNode { 10 | int val; 11 | TreeNode left; 12 | TreeNode right; 13 | 14 | public TreeNode(int val) { 15 | this.val = val; 16 | } 17 | 18 | public TreeNode(int val, TreeNode left, TreeNode right) { 19 | this.val = val; 20 | this.left = left; 21 | this.right = right; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /others/Recursion & Backtracking/subsets/subsets.iml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/3/26 8 | */ 9 | public class App { 10 | public static void main(String[] args) { 11 | 12 | // int[] arr = new int[]{3, 4, 1, 32, 0, 1, 5, 12, 2, 5, 7, 8, 9, 2, 44, 111, 5}; 13 | 14 | int[] arr = new int[]{1, -1, 2, 0 }; 15 | QuickSort.sort(arr, 0, arr.length - 1); 16 | 17 | for (int i = 0; i < arr.length; i++) { 18 | System.out.print(arr[i]); 19 | System.out.print(" "); 20 | } 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/construct-binary-tree-from-preorder-and-inorder-traversal/construct-binary-tree-from-preorder-and-inorder-traversal.iml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 206. Reverse Linked List (Easy) 4 | * https://leetcode.com/problems/reverse-linked-list/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/16 9 | */ 10 | public class ReverseList { 11 | public ListNode reverseList(ListNode head) { 12 | 13 | ListNode pre = null; 14 | ListNode cur = head; 15 | ListNode post; 16 | 17 | while (cur != null) { 18 | post = cur.next; 19 | cur.next = pre; 20 | pre = cur; 21 | cur = post; 22 | } 23 | 24 | return pre; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /others/Python3/461.Hamming Distance.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 461. Hamming Distance 3 | @Tag: bit shifting 4 | @Date: Jan-15 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def hammingDistance(self, x: int, y: int) -> int: 12 | return bin(x ^ y).count('1') 13 | 14 | 15 | """ 16 | Runtime: 28 ms, faster than 61.17% of Python3 online submissions for Hamming Distance. 17 | Memory Usage: 12.6 MB, less than 100.00% of Python3 online submissions for Hamming Distance. 18 | """ 19 | 20 | 21 | """ 22 | Thanks to: @yuyuyu0905 23 | https://leetcode.com/problems/hamming-distance/discuss/94697/Python-1-line-49ms 24 | """ 25 | -------------------------------------------------------------------------------- /others/Python3/1184.Distance Between Bus Stops.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1184. Distance Between Bus Stops 3 | @Tag: array 4 | @Date: Jan-24 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int: 12 | start, destination = min(start, destination), max( 13 | start, destination) # redefine 14 | distance *= 2 # cycle 15 | return min(sum(distance[start: destination]), sum(distance[destination: start + len(distance) // 2])) 16 | 17 | 18 | """ 19 | Time Complexity: O(n) 20 | Space Complexity: O(n) 21 | """ 22 | -------------------------------------------------------------------------------- /exercises/tree/src/main/java/ConvertBST.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 538. Convert BST to Greater Tree (Medium) 4 | * https://leetcode.com/problems/convert-bst-to-greater-tree/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/3 9 | */ 10 | public class ConvertBST { 11 | 12 | private int sum = 0; 13 | 14 | public TreeNode convertBST(TreeNode root) { 15 | inorder(root); 16 | return root; 17 | } 18 | 19 | public void inorder(TreeNode root) { 20 | 21 | if (root == null) return; 22 | 23 | inorder(root.right); 24 | sum += root.val; 25 | root.val = sum; 26 | inorder(root.left); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/search-in-a-binary-search-tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Search in a Binary Search Tree 3 | */ 4 | class Solution { 5 | 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | } 11 | 12 | public TreeNode searchBST(TreeNode root, int val) { 13 | 14 | // Corner case 15 | if (root == null) return null; 16 | 17 | // Bingo 18 | if (root.val == val) return root; 19 | 20 | if (val < root.val) { 21 | return searchBST(root.left, val); 22 | } else { 23 | return searchBST(root.right, val); 24 | } 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /others/Python3/1287.Element Appearing More Than 25% In Sorted Array.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1287. Element Appearing More Than 25% In Sorted Array 3 | @Tag: array 4 | @Date: Jan-26 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | import collections 11 | 12 | 13 | class Solution: 14 | def findSpecialInteger(self, arr: List[int]) -> int: 15 | return collections.Counter(arr).most_common(1)[0][0] 16 | 17 | 18 | """ 19 | Runtime: 104 ms, faster than 21.14% of Python3 online submissions for Element Appearing More Than 25% In Sorted Array. 20 | Memory Usage: 14.1 MB, less than 100.00% of Python3 online submissions for Element Appearing More Than 25% In Sorted Array. 21 | """ 22 | -------------------------------------------------------------------------------- /exercises/tree/src/main/java/SearchBST.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 700. Search in a Binary Search Tree (Easy) 4 | * https://leetcode.com/problems/search-in-a-binary-search-tree/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/3/30 9 | */ 10 | public class SearchBST { 11 | public TreeNode searchBST(TreeNode root, int val) { 12 | 13 | if (root == null) return null; 14 | 15 | /* 16 | Preorder traversal 17 | */ 18 | if (val == root.val) return root; 19 | 20 | if (val < root.val) { 21 | return searchBST(root.left, val); 22 | } else { 23 | return searchBST(root.right, val); 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /interviews/dynamic-programming/src/main/java/ClimbStairs.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 70. Climbing Stairs (Easy) 4 | * https://leetcode.com/problems/climbing-stairs/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/18 9 | */ 10 | public class ClimbStairs { 11 | public int climbStairs(int n) { 12 | 13 | // Corner case 14 | if (n == 1) return 1; 15 | 16 | int[] dp = new int[n + 1]; 17 | // One step 18 | dp[1] = 1; 19 | // Two steps 20 | dp[2] = 2; 21 | 22 | for (int i = 3; i <= n; i++) { 23 | dp[i] = dp[i - 1] + dp[i - 2]; 24 | } 25 | 26 | // N steps 27 | return dp[n]; 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /others/Python3/888.Fair Candy Swap.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 888. Fair Candy Swap 3 | @Tag: array 4 | @Date: Feb-02 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]: 12 | for j in set(B): # accelerate 13 | # solve the equation: "y - x = (sum(B) - sum(A)) / 2" 14 | if j - (sum(B) - sum(A)) // 2 in A: 15 | return [j - (sum(B) - sum(A)) // 2, j] 16 | 17 | 18 | """ 19 | Runtime: 6864 ms, faster than 5.16% of Python3 online submissions for Fair Candy Swap. 20 | Memory Usage: 15.1 MB, less than 41.67% of Python3 online submissions for Fair Candy Swap. 21 | """ 22 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/symmetric-tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Symmetric Tree 3 | */ 4 | class Solution { 5 | 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | } 11 | 12 | public boolean isSymmetric(TreeNode root) { 13 | return isMirror(root, root); 14 | } 15 | 16 | // Top-down 17 | public boolean isMirror(TreeNode n1, TreeNode n2) { 18 | // Corner cases 19 | if (n1 == null && n2 == null) return true; 20 | if (n1 == null || n2 == null) return false; 21 | 22 | return (n1.val == n2.val) && isMirror(n1.left, n2.right) && isMirror(n1.right, n2.left); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /interviews/two-pointers/src/main/java/ReverseString.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 344. Reverse String 4 | * https://leetcode.com/problems/reverse-string/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/15 9 | */ 10 | class ReverseString { 11 | 12 | private char[] s; 13 | 14 | public void reverseString(char[] s) { 15 | 16 | this.s = s; 17 | int i = 0; 18 | int j = s.length - 1; 19 | 20 | while (i < j) { 21 | swap(i, j); 22 | i++; 23 | j--; 24 | } 25 | 26 | } 27 | 28 | public void swap(int i, int j) { 29 | char tmp = s[i]; 30 | s[i] = s[j]; 31 | s[j] = tmp; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /exercises/string/src/main/java/ReverseString.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 344. Reverse String (Easy) 4 | * https://leetcode.com/problems/reverse-string/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/19 9 | */ 10 | public class ReverseString { 11 | 12 | private char[] s; 13 | 14 | public void reverseString(char[] s) { 15 | 16 | this.s = s; 17 | int i = 0; 18 | int j = s.length - 1; 19 | 20 | while (i < j) { 21 | swap(i, j); 22 | i++; 23 | j--; 24 | } 25 | 26 | } 27 | 28 | public void swap(int i, int j) { 29 | char tmp = s[i]; 30 | s[i] = s[j]; 31 | s[j] = tmp; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /others/Python3/1347.Minimum Number of Steps to Make Two Strings Anagram.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1347. Minimum Number of Steps to Make Two Strings Anagram 3 | @Tag: string 4 | @Date: Feb-09 2020 5 | @Author: ceezyyy 6 | @Difficulty: Medium 7 | """ 8 | 9 | 10 | class Solution: 11 | def minSteps(self, s: str, t: str) -> int: 12 | res, freq = 0, [0] * 26 # lower-case English letters only 13 | for ch in s: 14 | freq[ord(ch) - ord('a')] += 1 15 | for ch in t: 16 | freq[ord(ch) - ord('a')] -= 1 17 | if freq[ord(ch) - ord('a')] < 0: 18 | res += 1 19 | return res 20 | 21 | 22 | """ 23 | Time Complexity: O(n) 24 | Space Complexity: O(1) 25 | """ 26 | -------------------------------------------------------------------------------- /others/Python3/223.Rectangle Area.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 223. Rectangle Area 3 | @Tag: math 4 | @Date: Jan-15 2020 5 | @Author: ceezyyy 6 | @Difficulty: Medium 7 | """ 8 | 9 | 10 | class Solution: 11 | def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int: 12 | width, height = min(C, G) - max(A, E), min(D, H) - max(B, F) 13 | overlap = height * width if (height > 0 and width > 0) else 0 14 | return (C - A) * (D - B) + (G - E) * (H - F) - overlap 15 | 16 | 17 | """ 18 | Runtime: 44 ms, faster than 90.49% of Python3 online submissions for Rectangle Area. 19 | Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Rectangle Area. 20 | """ 21 | -------------------------------------------------------------------------------- /others/Python3/989.Add to Array-Form of Integer.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 989. Add to Array-Form of Integer 3 | @Tag: list 4 | @Date: Oct-31 2019 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | from typing import List 11 | 12 | 13 | class Solution: 14 | def addToArrayForm(self, A: List[int], K: int) -> List[int]: 15 | total = 0 16 | for i in range(len(A)): 17 | total = total*10+A[i] 18 | total += K 19 | return list(str(total)) 20 | 21 | 22 | ''' 23 | Runtime: 440 ms, faster than 15.26% of Python3 online submissions for Add to Array-Form of Integer. 24 | Memory Usage: 14.1 MB, less than 5.00% of Python3 online submissions for Add to Array-Form of Integer. 25 | ''' 26 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/inorder-traversal/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.List; 3 | 4 | 5 | /** 6 | * Binary Tree Inorder Traversal 7 | */ 8 | class Solution { 9 | 10 | public class TreeNode { 11 | int val; 12 | TreeNode left; 13 | TreeNode right; 14 | } 15 | 16 | private List3 | * 485. Max Consecutive Ones (Easy) 4 | * https://leetcode.com/problems/max-consecutive-ones/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/22 9 | */ 10 | public class MaxConsecutiveOnes { 11 | public int findMaxConsecutiveOnes(int[] nums) { 12 | 13 | int res = 0; 14 | int n = nums.length; 15 | int cnt = 0; 16 | 17 | for (int num : nums) { 18 | if (num == 1) { 19 | cnt++; 20 | } else { 21 | res = Math.max(cnt, res); 22 | cnt = 0; 23 | } 24 | } 25 | 26 | return Math.max(cnt, res); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /interviews/tree/src/main/java/PathSum.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 112. Path Sum (Easy) 4 | * https://leetcode.com/problems/path-sum/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/3/29 9 | */ 10 | public class PathSum { 11 | public boolean hasPathSum(TreeNode root, int target) { 12 | 13 | // root-to-leaf path 14 | // e.g: [1, null, 2, null, 3] -> path sum: 6 15 | if (root == null) return false; 16 | 17 | // leaf 18 | if (root.left == null && root.right == null && root.val == target) return true; 19 | 20 | // If current node is not a leaf 21 | return hasPathSum(root.left, target - root.val) || hasPathSum(root.right, target - root.val); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /others/Python3/1346.Check If N and Its Double Exist.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1346. Check If N and Its Double Exist 3 | @Tag: array 4 | @Date: Feb-09 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def checkIfExist(self, arr: List[int]) -> bool: 12 | if arr.count(0) >= 2: # corner case 13 | return True 14 | for a in set(sorted(arr)): 15 | if a < 0: # negative 16 | if a / 2 in arr: 17 | return True 18 | if a > 0: # positive 19 | if a * 2 in arr: 20 | return True 21 | return False 22 | 23 | 24 | """ 25 | Time Complexity: O(n) 26 | Space Complexity: O(1) 27 | """ 28 | -------------------------------------------------------------------------------- /exercises/array/pom.xml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 766. Toeplitz Matrix (Easy) 4 | * https://leetcode.com/problems/toeplitz-matrix/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/22 9 | */ 10 | public class ToeplitzMatrix { 11 | public boolean isToeplitzMatrix(int[][] matrix) { 12 | 13 | // 1 <= m, n <= 20 14 | int rows = matrix.length; 15 | int cols = matrix[0].length; 16 | 17 | for (int i = 0; i < rows; i++) { 18 | for (int j = 0; j < cols; j++) { 19 | if (i == 0 || j == 0) continue; 20 | if (matrix[i - 1][j - 1] != matrix[i][j]) return false; 21 | } 22 | } 23 | 24 | return true; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /exercises/two-pointers/pom.xml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 965. Univalued Binary Tree 4 | * https://leetcode.com/problems/univalued-binary-tree/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/3/30 9 | */ 10 | public class IsUnivalTree { 11 | 12 | private int uniqueVal; 13 | 14 | public boolean isUnivalTree(TreeNode root) { 15 | 16 | if (root == null) return true; 17 | uniqueVal = root.val; 18 | 19 | return preorder(root); 20 | 21 | } 22 | 23 | public boolean preorder(TreeNode root) { 24 | 25 | if (root == null) return true; 26 | 27 | if (root.val != uniqueVal) return false; 28 | 29 | return preorder(root.left) && preorder(root.right); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /interviews/binary-search/pom.xml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 171. Excel Sheet Column Number (Easy) 4 | * https://leetcode.com/problems/excel-sheet-column-number/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/22 9 | */ 10 | public class ExcelSheetColumnNumber { 11 | public int titleToNumber(String columnTitle) { 12 | 13 | String s = new StringBuilder(columnTitle).reverse().toString(); 14 | char[] nums = s.toCharArray(); 15 | int n = nums.length; 16 | int res = 0; 17 | 18 | for (int i = 0; i < n; i++) { 19 | int cur = nums[i] - 'A' + 1; 20 | // 26-Base 21 | res += cur * (int) Math.pow(26, i); 22 | } 23 | 24 | return res; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /interviews/array/src/main/java/MoveZeroes.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 283. Move Zeroes (Easy) 4 | * https://leetcode.com/problems/move-zeroes/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/18 9 | */ 10 | public class MoveZeroes { 11 | public void moveZeroes(int[] nums) { 12 | 13 | // 1 <= nums.length <= 10^4 14 | int n = nums.length; 15 | 16 | int i = 0; 17 | 18 | // "j" should start from zero! 19 | for (int j = 0; j < n; j++) { 20 | if (nums[j] != 0) { 21 | nums[i] = nums[j]; 22 | i++; 23 | } 24 | } 25 | 26 | // Filling w/ zero 27 | while (i < n) { 28 | nums[i] = 0; 29 | i++; 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /others/Python3/509.Fibonacci Number.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 509. Fibonacci Number 3 | @Tag: dp 4 | @Date: Dec-23 2019 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def fib(self, N: int) -> int: 12 | # corner case 13 | if N == 0: 14 | return 0 15 | if N == 1: 16 | return 1 17 | dp = [0 for i in range(N+1)] # memorization 18 | dp[0], dp[1] = 0, 1 # initialization 19 | for n in range(2, N+1): 20 | dp[n] = dp[n-1]+dp[n-2] 21 | return dp[n] 22 | 23 | 24 | """ 25 | Runtime: 20 ms, faster than 99.01% of Python3 online submissions for Fibonacci Number. 26 | Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Fibonacci Number. 27 | """ 28 | -------------------------------------------------------------------------------- /exercises/tree/src/main/java/IsSameTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 100. Same Tree (Easy) 4 | * https://leetcode.com/problems/same-tree/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/3/30 9 | */ 10 | public class IsSameTree { 11 | public boolean isSameTree(TreeNode p, TreeNode q) { 12 | 13 | /* 14 | Preorder traversal 15 | */ 16 | if (p == null && q == null) return true; 17 | 18 | if (p != null && q != null && p.val != q.val) return false; 19 | 20 | if (p == null || q == null) return false; 21 | 22 | return isSameTree(p.left == null ? null : p.left, q.left == null ? null : q.left) 23 | && isSameTree(p.right == null ? null : p.right, q.right == null ? null : q.right); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /others/Python3/1342.Number of Steps to Reduce a Number to Zero.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1342. Number of Steps to Reduce a Number to Zero 3 | @Tag: bit manipulation 4 | @Date: Feb-09 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def numberOfSteps(self, num: int) -> int: 12 | res = 0 13 | while num: 14 | if num % 2 == 0: 15 | num //= 2 16 | else: 17 | num -= 1 18 | res += 1 19 | return res 20 | 21 | 22 | """ 23 | Runtime: 28 ms, faster than 66.67% of Python3 online submissions for Number of Steps to Reduce a Number to Zero. 24 | Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Number of Steps to Reduce a Number to Zero. 25 | """ 26 | -------------------------------------------------------------------------------- /others/Python3/1344.Angle Between Hands of a Clock.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1344. Angle Between Hands of a Clock 3 | @Tag: math 4 | @Date: Feb-10 2020 5 | @Author: ceezyyy 6 | @Difficulty: Medium 7 | """ 8 | 9 | 10 | class Solution: 11 | def angleClock(self, h: int, m: int) -> float: 12 | # "m / 60 * 5" means how the minute hand affects the position of the hour hand 13 | h = 0 + m / 60 * 5 if h == 12 else h * 5 + m / 60 * 5 # convert hrs to mins 14 | return min(abs(m - h) * 6, 360 - abs(m - h) * 6) # the smaller one 15 | 16 | 17 | """ 18 | Runtime: 32 ms, faster than 22.88% of Python3 online submissions for Angle Between Hands of a Clock. 19 | Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Angle Between Hands of a Clock. 20 | """ 21 | -------------------------------------------------------------------------------- /interviews/tree/src/main/java/MinDepth.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 111. Minimum Depth of Binary Tree (Easy) 4 | *
5 | * 6 | * @author ceezyyy 7 | * @since 2021/3/29 8 | */ 9 | public class MinDepth { 10 | public int minDepth(TreeNode root) { 11 | 12 | if (root == null) return 0; 13 | 14 | int leftDepth = minDepth(root.left); 15 | int rightDepth = minDepth(root.right); 16 | 17 | /* 18 | min path is from root to leaf 19 | so when we decide to choose a non-empty element between a and b 20 | we can just add them together ("a + b") 21 | */ 22 | if (leftDepth == 0 || rightDepth == 0) return leftDepth + rightDepth + 1; 23 | 24 | return Math.min(leftDepth, rightDepth) + 1; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /others/Python3/1207.Unique Number of Occurrences.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1207. Unique Number of Occurrences 3 | @Tag: dict 4 | @Date: Nov-11 2019 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def uniqueOccurrences(self, arr: List[int]) -> bool: 12 | res = [] 13 | d = {item: {'occurrence': 0} for item in arr} # dict 14 | for c in arr: 15 | d[c]['occurrence'] += 1 16 | for c in d: 17 | res.append(d[c]['occurrence']) 18 | return len(set(res)) == len(res) 19 | 20 | 21 | """ 22 | Runtime: 36 ms, faster than 95.70% of Python3 online submissions for Unique Number of Occurrences. 23 | Memory Usage: 12.9 MB, less than 100.00% of Python3 online submissions for Unique Number of Occurrences. 24 | """ 25 | -------------------------------------------------------------------------------- /others/Python3/217.Contains Duplicate.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 217. Contains Duplicate 3 | @Tag: array / hashtable 4 | @Date: Feb-22 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | import collections 11 | 12 | 13 | class Solution: 14 | def containsDuplicate(self, A: List[int]) -> bool: 15 | d = collections.defaultdict(int) 16 | for a in A: 17 | d[a] += 1 18 | if d[a] > 1: 19 | return True 20 | return False 21 | 22 | 23 | """ 24 | Runtime: 124 ms, faster than 84.43% of Python3 online submissions for Contains Duplicate. 25 | Memory Usage: 19.1 MB, less than 16.98% of Python3 online submissions for Contains Duplicate. 26 | """ 27 | 28 | 29 | """ 30 | Time Complexity: O(n) 31 | Space Complexity: O(n) 32 | """ 33 | -------------------------------------------------------------------------------- /interviews/math/src/main/java/PlusOne.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 66. Plus One (Easy) 4 | * https://leetcode.com/problems/plus-one/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/18 9 | */ 10 | public class PlusOne { 11 | public int[] plusOne(int[] digits) { 12 | 13 | int n = digits.length; 14 | 15 | // Backwards 16 | for (int i = n - 1; i >= 0; i--) { 17 | 18 | // No carry 19 | if (digits[i] < 9) { 20 | digits[i]++; 21 | return digits; 22 | } 23 | 24 | // Carry 25 | digits[i] = 0; 26 | 27 | } 28 | 29 | // Still not returned 30 | digits = new int[n + 1]; 31 | digits[0] = 1; 32 | return digits; 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /others/Python3/1016.Binary String With Substrings Representing 1 To N.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1016. Binary String With Substrings Representing 1 To N 3 | @Tag: str 4 | @Date: Jan-26 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def queryString(self, S: str, N: int) -> bool: 12 | for i in range(1, N + 1): # from 1 to N 13 | sub = "{0:b}".format(i) # int -> binary string 14 | if sub not in S: 15 | return False 16 | return True 17 | 18 | 19 | """ 20 | Runtime: 76 ms, faster than 8.28% of Python3 online submissions for Binary String With Substrings Representing 1 To N. 21 | Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Binary String With Substrings Representing 1 To N. 22 | """ 23 | -------------------------------------------------------------------------------- /others/Python3/946.Validate Stack Sequences.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 946. Validate Stack Sequences 3 | @Tag: stack 4 | @Date: Oct-26 2019 5 | @Author: ceezyyy 6 | @Difficulty: Medium 7 | """ 8 | 9 | 10 | import collections 11 | from typing import List 12 | 13 | class Solution: 14 | def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: 15 | stack = [] 16 | for i in pushed: 17 | stack.append(i) 18 | while stack and popped and stack[-1] == popped[0]: # stack top 19 | stack.pop() 20 | popped.pop(0) 21 | return not stack 22 | 23 | 24 | def test(): 25 | s = Solution() 26 | print(s.validateStackSequences([1, 2, 3, 4, 5], [4, 5, 3, 2, 1])) 27 | 28 | 29 | if __name__ == "__main__": 30 | test() 31 | -------------------------------------------------------------------------------- /others/Python3/1313.Decompress Run-Length Encoded List.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1313. Decompress Run-Length Encoded List 3 | @Tag: array 4 | @Date: Feb-06 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def decompressRLElist(self, nums: List[int]) -> List[int]: 12 | res, n = [], len(nums) 13 | for i in range(1, n, 2): 14 | temp = [nums[i]] * nums[i-1] 15 | res.extend(temp) 16 | return res 17 | 18 | 19 | """ 20 | Runtime: 68 ms, faster than 71.87% of Python3 online submissions for Decompress Run-Length Encoded List. 21 | Memory Usage: 13 MB, less than 100.00% of Python3 online submissions for Decompress Run-Length Encoded List. 22 | """ 23 | 24 | 25 | """ 26 | Time Complexity: O(n) 27 | Space Complexity: O(1) 28 | """ 29 | -------------------------------------------------------------------------------- /others/Python3/1324.Print Words Vertically.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1324. Print Words Vertically 3 | @Tag: string 4 | @Date: Feb-14 2020 5 | @Author: ceezyyy 6 | @Difficulty: Medium 7 | """ 8 | 9 | 10 | class Solution: 11 | def printVertically(self, s: str) -> List[str]: 12 | s = s.split(" ") 13 | n = max([len(w) for w in s]) 14 | res = [""] * n 15 | for i in range(n): 16 | for word in s: 17 | res[i] += word[i] if i < len(word) else " " 18 | res[i] = res[i].rstrip() # remove trailing spaces 19 | return res 20 | 21 | 22 | """ 23 | Runtime: 20 ms, faster than 97.48% of Python3 online submissions for Print Words Vertically. 24 | Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Print Words Vertically. 25 | """ 26 | -------------------------------------------------------------------------------- /others/Python3/389.Find the Difference.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 389. Find the Difference 3 | @Tag: hashmap 4 | @Date: Feb-23 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | import collections as c 11 | 12 | 13 | class Solution: 14 | def findTheDifference(self, s: str, t: str) -> str: 15 | d = c.defaultdict(int) 16 | for ch in s: 17 | d[ch] += 1 18 | for ch in t: 19 | d[ch] -= 1 20 | if d[ch] < 0: 21 | return ch 22 | 23 | 24 | """ 25 | Runtime: 32 ms, faster than 69.08% of Python3 online submissions for Find the Difference. 26 | Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Find the Difference. 27 | """ 28 | 29 | 30 | """ 31 | Time Complexity: O(n) 32 | Space Complexity: O(n) 33 | """ 34 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/insert-into-a-binary-search-tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Insert into a Binary Search Tree 3 | */ 4 | class Solution { 5 | 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | 11 | TreeNode(int val) { 12 | this.val = val; 13 | } 14 | } 15 | 16 | public TreeNode insertIntoBST(TreeNode root, int val) { 17 | 18 | if (root == null) return new TreeNode(val); 19 | 20 | // It's guaranteed that val does not exist in the original BST 21 | if (val < root.val) { 22 | root.left = insertIntoBST(root.left, val); 23 | } else { 24 | root.right = insertIntoBST(root.right, val); 25 | } 26 | 27 | return root; 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /exercises/tree/src/main/java/GoodNodes.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 1448. Count Good Nodes in Binary Tree (Medium) 4 | * https://leetcode.com/problems/count-good-nodes-in-binary-tree/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/3/30 9 | */ 10 | public class GoodNodes { 11 | 12 | private int res = 0; 13 | 14 | public int goodNodes(TreeNode root) { 15 | preorder(root, Integer.MIN_VALUE); 16 | return res; 17 | } 18 | 19 | 20 | public void preorder(TreeNode root, int max) { 21 | 22 | // Base case 23 | if (root == null) return; 24 | 25 | if (root.val >= max) { 26 | res++; 27 | max = root.val; 28 | } 29 | 30 | preorder(root.left, max); 31 | preorder(root.right, max); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /exercises/two-pointers/src/main/java/RemoveDuplicates.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 26. Remove Duplicates from Sorted Array (Easy) 4 | * https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/18 9 | */ 10 | public class RemoveDuplicates { 11 | public int removeDuplicates(int[] nums) { 12 | 13 | if (nums == null || nums.length == 0) return 0; 14 | int index = 0; 15 | int cmp = nums[0]; 16 | int cur; 17 | 18 | for (int i = 1; i < nums.length; i++) { 19 | 20 | cur = nums[i]; 21 | 22 | if (cur != cmp) { 23 | nums[++index] = cur; 24 | cmp = cur; 25 | } 26 | 27 | } 28 | 29 | return index + 1; 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /others/Python3/1281.Subtract the Product and Sum of Digits of an Integer.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1281. Subtract the Product and Sum of Digits of an Integer 3 | @Tag: 4 | @Date: Jan-12 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def subtractProductAndSum(self, n: int) -> int: 12 | res = [] 13 | while n > 0: 14 | res.append(n % 10) 15 | n //= 10 16 | # math.prod() is available in Python 3.8 17 | return math.prod(res) - sum(res) 18 | 19 | 20 | """ 21 | Runtime: 24 ms, faster than 83.24% of Python3 online submissions for Subtract the Product and Sum of Digits of an Integer. 22 | Memory Usage: 12.6 MB, less than 100.00% of Python3 online submissions for Subtract the Product and Sum of Digits of an Integer. 23 | """ 24 | -------------------------------------------------------------------------------- /others/Python3/836.Rectangle Overlap.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 836. Rectangle Overlap 3 | @Tag: math 4 | @Date: Jan-15 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool: 12 | width = min(rec1[2], rec2[2]) - max(rec1[0], 13 | rec2[0]) # min_right - max_left 14 | height = min(rec1[3], rec2[3]) - max(rec1[1], 15 | rec2[1]) # min_high - max_low 16 | return width > 0 and height > 0 17 | 18 | 19 | """ 20 | Runtime: 12 ms, faster than 100.00% of Python3 online submissions for Rectangle Overlap. 21 | Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Rectangle Overlap. 22 | """ 23 | -------------------------------------------------------------------------------- /interviews/array/src/main/java/SearchMatrix.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 240. Search a 2D Matrix II (Medium) 4 | * https://leetcode.com/problems/search-a-2d-matrix-ii/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/22 9 | */ 10 | public class SearchMatrix { 11 | public boolean searchMatrix(int[][] matrix, int target) { 12 | 13 | // 1 <= n, m <= 300 14 | int rows = matrix.length; 15 | int cols = matrix[0].length; 16 | // Upper-right 17 | int i = 0; 18 | int j = cols - 1; 19 | 20 | while (i < rows && j >= 0) { 21 | int cur = matrix[i][j]; 22 | if (target == cur) return true; 23 | if (target < cur) j--; 24 | if (target > cur) i++; 25 | } 26 | 27 | return false; 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /others/Python3/1078.Occurrences After Bigram.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1078. Occurrences After Bigram 3 | @Tag: str 4 | @Date: Nov-05 2019 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | def findOccurrences(self, text: str, first: str, second: str) -> List[str]: 14 | res = [] 15 | # str.split(separator, maxsplit) 16 | text = text.split() #3 | * 169. Majority Element (Easy) 4 | * Moore-Voting Algorithm 5 | * https://leetcode.com/problems/majority-element/ 6 | *
7 | * 8 | * @author ceezyyy 9 | * @since 2021/4/22 10 | */ 11 | public class MajorityElement { 12 | public int majorityElement(int[] nums) { 13 | 14 | int n = nums.length; 15 | // Init 16 | int cand = nums[0]; 17 | int cnt = 1; 18 | 19 | for (int i = 1; i < n; i++) { 20 | if (cnt == 0) { 21 | // Change candidate 22 | cand = nums[i]; 23 | cnt = 1; 24 | continue; 25 | } 26 | cnt = nums[i] == cand ? cnt + 1 : cnt - 1; 27 | } 28 | 29 | return cand; 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /others/Python3/070.Climbing Stairs.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Topic: 070. Climbing Stairs 3 | @Tag: dp 4 | @Date: Dec-14 2019 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def climbStairs(self, n: int) -> int: 12 | # corner case 13 | if n == 0 or n == 1: 14 | return 1 15 | if n == 2: 16 | return 2 17 | dp = [0]*(n+1) # memory 18 | # exit 19 | dp[0] = 1 20 | dp[1] = 1 21 | dp[2] = 2 22 | for i in range(3, n+1): 23 | dp[i] = dp[i-1]+dp[i-2] # recursion 24 | return dp[i] 25 | 26 | 27 | """ 28 | Runtime: 24 ms, faster than 94.38% of Python3 online submissions for Climbing Stairs. 29 | Memory Usage: 12.6 MB, less than 100.00% of Python3 online submissions for Climbing Stairs. 30 | """ 31 | -------------------------------------------------------------------------------- /exercises/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 |3 | * 242. Valid Anagram (Easy) 4 | * https://leetcode.com/problems/valid-anagram/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/19 9 | */ 10 | public class ValidAnagram { 11 | public boolean isAnagram(String s, String t) { 12 | 13 | // s and t consist of lowercase English letters 14 | int[] visited = new int[26]; 15 | 16 | for (char c : s.toCharArray()) { 17 | int e = c - 'a'; 18 | visited[e]++; 19 | } 20 | 21 | for (char c : t.toCharArray()) { 22 | int e = c - 'a'; 23 | visited[e]--; 24 | } 25 | 26 | for (int e : visited) { 27 | if (e != 0) return false; 28 | } 29 | 30 | return true; 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /interviews/tree/src/main/java/InvertBinaryTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 226. Invert Binary Tree (Easy) 4 | * https://leetcode.com/problems/invert-binary-tree/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/3/24 9 | */ 10 | class InvertBinaryTree { 11 | public TreeNode invertTree(TreeNode root) { 12 | 13 | if (root == null) return root; 14 | 15 | /* 16 | For each node: 17 | 18 | 1) invert its left & right child 19 | 2) the left / right pointer points to the "invertedRight" / "invertedLeft" 20 | */ 21 | TreeNode invertedLeft = invertTree(root.left); 22 | TreeNode invertedRight = invertTree(root.right); 23 | 24 | root.left = invertedRight; 25 | root.right = invertedLeft; 26 | 27 | return root; 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /others/Python3/1306.Jump Game III.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1306. Jump Game III 3 | @Tag: recursion 4 | @Date: Feb-14 2020 5 | @Author: ceezyyy 6 | @Difficulty: Medium 7 | """ 8 | 9 | 10 | class Solution: 11 | def canReach(self, A: List[int], i: int) -> bool: 12 | if (0 <= i < len(A)) and A[i] >= 0: 13 | if A[i] == 0: 14 | return True 15 | A[i] = -A[i] # have seen before 16 | # either 17 | return self.canReach(A, i + A[i]) or self.canReach(A, i - A[i]) 18 | return False 19 | 20 | 21 | """ 22 | Runtime: 248 ms, faster than 45.58% of Python3 online submissions for Jump Game III. 23 | Memory Usage: 19.2 MB, less than 100.00% of Python3 online submissions for Jump Game III. 24 | """ 25 | 26 | 27 | """ 28 | Time Complexity: O(n) 29 | Space Complexity: O(n) 30 | """ 31 | -------------------------------------------------------------------------------- /others/Python3/242.Valid Anagram.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 242. Valid Anagram 3 | @Tag: str 4 | @Date: Jan-04 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def isAnagram(self, s: str, t: str) -> bool: 12 | freq = [0] * 26 # the frequency of each letters 13 | if len(s) != len(t): # s and t should have the same length 14 | return False 15 | for i in range(len(s)): 16 | freq[ord(s[i])-ord('a')] += 1 17 | freq[ord(t[i])-ord('a')] -= 1 18 | # all the element should be the same -- zero 19 | return len(set(freq)) == 1 20 | 21 | 22 | """ 23 | Runtime: 56 ms, faster than 25.61% of Python3 online submissions for Valid Anagram. 24 | Memory Usage: 13.1 MB, less than 96.88% of Python3 online submissions for Valid Anagram. 25 | """ 26 | -------------------------------------------------------------------------------- /others/Python3/347.Top K Frequent Elements.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 347. Top K Frequent Elements 3 | @Tag: Counter 4 | @Date: Nov-13 2019 5 | @Author: ceezyyy 6 | @Difficulty: Medium 7 | """ 8 | 9 | 10 | import collections 11 | from typing import List 12 | 13 | 14 | class Solution: 15 | def topKFrequent(self, nums: List[int], k: int) -> List[int]: 16 | return [item[0] for item in collections.Counter(nums).most_common(k)] 17 | 18 | 19 | def test(): 20 | s = Solution() 21 | res = s.topKFrequent([1, 1, 1, 2, 2, 3], 3) 22 | print(res) 23 | 24 | 25 | if __name__ == "__main__": 26 | test() 27 | 28 | 29 | """ 30 | Runtime: 104 ms, faster than 98.05% of Python3 online submissions for Top K Frequent Elements. 31 | Memory Usage: 17.1 MB, less than 6.25% of Python3 online submissions for Top K Frequent Elements. 32 | """ 33 | -------------------------------------------------------------------------------- /interviews/tree/src/main/java/KthSmallest.java: -------------------------------------------------------------------------------- 1 | /** 2 | *3 | * 230. Kth Smallest Element in a BST (Medium) 4 | * https://leetcode.com/problems/kth-smallest-element-in-a-bst/ 5 | *
6 | * 7 | * @author ceezyyy 8 | * @since 2021/4/3 9 | */ 10 | public class KthSmallest { 11 | 12 | private int res = 0; 13 | private int counter = 0; 14 | private int k = 0; 15 | 16 | public int kthSmallest(TreeNode root, int k) { 17 | this.k = k; 18 | inorder(root); 19 | return res; 20 | } 21 | 22 | public void inorder(TreeNode root) { 23 | 24 | if (root == null) return; 25 | 26 | inorder(root.left); 27 | counter++; 28 | if (counter == k) { 29 | res = root.val; 30 | return; 31 | } 32 | inorder(root.right); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /others/Python3/1154.Day of the Year.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 1154. Day of the Year 3 | @Tag: math 4 | @Date: Jan-22 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | import calendar 11 | 12 | 13 | class Solution: 14 | def dayOfYear(self, date: str) -> int: 15 | month_days, leap_month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [ 16 | 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 17 | year, month, day = int(date[:4]), int(date[5:7]), int(date[8:10]) 18 | return sum(leap_month_days[:month - 1]) + day if calendar.isleap(year) else sum(month_days[:month - 1]) + day 19 | 20 | 21 | """ 22 | Runtime: 32 ms, faster than 24.55% of Python3 online submissions for Day of the Year. 23 | Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Day of the Year. 24 | """ 25 | -------------------------------------------------------------------------------- /others/Python3/824.Goat Latin.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 824. Goat Latin 3 | @Tag: string 4 | @Date: Jan-29 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def toGoatLatin(self, S: str) -> str: 12 | index, vowels, res = 1, list("aeiouAEIOU"), [] 13 | for word in S.split(): 14 | if word[0] in vowels: # rule one 15 | word += "ma" 16 | else: # rule two 17 | word = word[1:] + word[0] + "ma" 18 | word += index * 'a' # rule three 19 | index += 1 20 | res.append(word) 21 | return " ".join(res) 22 | 23 | 24 | """ 25 | Runtime: 28 ms, faster than 65.95% of Python3 online submissions for Goat Latin. 26 | Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Goat Latin. 27 | """ 28 | -------------------------------------------------------------------------------- /others/Python3/859.Buddy Strings.py: -------------------------------------------------------------------------------- 1 | """ 2 | @Title: 859. Buddy Strings 3 | @Tag: string 4 | @Date: Feb-25 2020 5 | @Author: ceezyyy 6 | @Difficulty: Easy 7 | """ 8 | 9 | 10 | class Solution: 11 | def buddyStrings(self, A: str, B: str) -> bool: 12 | if len(A) != len(B): 13 | return False 14 | if A == B: 15 | if len(A) == len(set(A)): # "abcdefg" 16 | return False 17 | else: # "aaaa" 18 | return True 19 | n = len(A) 20 | res = [(x, y) for x, y in zip(A, B) if x != y] # pairs 21 | return len(res) == 2 and res[0] == res[1][::-1] 22 | 23 | 24 | """ 25 | Runtime: 32 ms, faster than 61.54% of Python3 online submissions for Buddy Strings. 26 | Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Buddy Strings. 27 | """ 28 | -------------------------------------------------------------------------------- /others/Tree, Binary Tree, BST/univalued-binary-tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Univalued Binary Tree 3 | *
4 | * A binary tree is univalued if every node in the tree has the same value.
5 | */
6 | class Solution {
7 |
8 | public class TreeNode {
9 | int val;
10 | TreeNode left;
11 | TreeNode right;
12 | }
13 |
14 | public boolean isUnivalTree(TreeNode root) {
15 | // The number of nodes in the given tree will be in the range [1, 100]
16 | return isUnivalTree(root, root.val);
17 | }
18 |
19 | public boolean isUnivalTree(TreeNode root, int val) {
20 |
21 | // Corner case
22 | if (root == null) return true;
23 |
24 | if (root.val != val) return false;
25 |
26 | return isUnivalTree(root.left, val) && isUnivalTree(root.right, val);
27 |
28 | }
29 | }
--------------------------------------------------------------------------------
/others/Python3/1189.Maximum Number of Balloons.py:
--------------------------------------------------------------------------------
1 | """
2 | @Title: 1189. Maximum Number of Balloons
3 | @Tag: dict
4 | @Date: Oct-22 2019
5 | @Author: ceezyyy
6 | @Difficulty: Easy
7 | """
8 |
9 |
10 | class Solution:
11 | def maxNumberOfBalloons(self, text: str) -> int:
12 | aim='balon'
13 | # dict.fromkeys() is a classmethod as well as the initialization of dict
14 | # d={'b','a','l','o','n'}
15 | d=dict.fromkeys(aim,0)
16 | for i in text:
17 | if i in d:
18 | d[i]=d[i]+1
19 | d['l']=int(d['l']/2) # 'l' appears twice
20 | d['o']=int(d['o']/2) # 'o' appears twice
21 | return min(d.values())
22 |
23 |
24 | def main():
25 | s=Solution()
26 | print(s.maxNumberOfBalloons("loonbalxballpoon"))
27 |
28 |
29 | if __name__ == "__main__":
30 | main()
31 |
--------------------------------------------------------------------------------
/others/Python3/442.Find All Duplicates in an Array.py:
--------------------------------------------------------------------------------
1 | """
2 | @Title: 442. Find All Duplicates in an Array
3 | @Tag: array / hashtable
4 | @Date: Feb-22 2020
5 | @Author: ceezyyy
6 | @Difficulty: Medium
7 | """
8 |
9 |
10 | import collections as c
11 |
12 |
13 | class Solution:
14 | def findDuplicates(self, A: List[int]) -> List[int]:
15 | res, d = [], c.defaultdict(int)
16 | for a in A:
17 | d[a] += 1
18 | if d[a] == 2:
19 | res.append(a)
20 | return res
21 |
22 |
23 | """
24 | Runtime: 392 ms, faster than 65.88% of Python3 online submissions for Find All Duplicates in an Array.
25 | Memory Usage: 21.2 MB, less than 7.14% of Python3 online submissions for Find All Duplicates in an Array.
26 | """
27 |
28 |
29 | """
30 | Time Complexity: O(n)
31 | Space Complexity: O(n)
32 | """
33 |
--------------------------------------------------------------------------------
/others/Tree, Binary Tree, BST/populating-next-right-pointers-in-each-node/src/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Populating Next Right Pointers in Each Node
3 | */
4 | class Solution {
5 |
6 | public class Node {
7 | public int val;
8 | public Node left;
9 | public Node right;
10 | public Node next;
11 | }
12 |
13 | public Node connect(Node root) {
14 | if (root == null) return root;
15 | connect(root.left, root.right);
16 | return root;
17 | }
18 |
19 | public void connect(Node left, Node right) {
20 |
21 | if (left == null || right == null) return;
22 |
23 | left.next = right;
24 | right.next = null;
25 |
26 | connect(left.left, left.right);
27 | connect(left.right, right.left);
28 | connect(right.left, right.right);
29 |
30 | }
31 | }
--------------------------------------------------------------------------------
/others/Python3/007.Reverse Integer.py:
--------------------------------------------------------------------------------
1 | """
2 | @Title: 007. Reverse Integer
3 | @Tag: str
4 | @Date: Jan-03 2020
5 | @Author: ceezyyy
6 | @Difficulty: Easy
7 | """
8 |
9 |
10 | class Solution:
11 | def reverse(self, x: int) -> int:
12 | k = str(x) # int -> str
13 | if k[0] == '-': # negative number
14 | k = k[1:] # get rid of the '-' first
15 | return -(int(k[::-1])) if -(int(k[::-1])) > -2**31 else 0
16 | else: # positive number
17 | return int(k[::-1]) if int(k[::-1]) < 2**31-1 else 0
18 |
19 |
20 | """
21 | within the 32-bit signed integer range: [−231, 231 − 1]
22 | """
23 |
24 |
25 | """
26 | Runtime: 20 ms, faster than 98.66% of Python3 online submissions for Reverse Integer.
27 | Memory Usage: 12.9 MB, less than 100.00% of Python3 online submissions for Reverse Integer.
28 | """
29 |
--------------------------------------------------------------------------------
/others/cpp/709.To Lower Case.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Source: LeetCode 709. To Lower Case
3 | Author: ceezyyy
4 | Date: 03-04-2019
5 | */
6 |
7 | /*
8 |
9 | 709. To Lower Case
10 |
11 | Easy
12 |
13 | Share
14 | Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
15 |
16 | Example 1:
17 | Input: “Hello”
18 | Output: “hello”
19 |
20 | Example 2:
21 | Input: “here”
22 | Output: “here”
23 |
24 | Example 3:
25 | Input: “LOVELY”
26 | Output: “lovely”
27 |
28 | */
29 |
30 |
31 | #include
3 | * 189. Rotate Array (Medium)
4 | * https://leetcode.com/problems/rotate-array/
5 | *
6 | * 1. Two Sum
7 | * https://leetcode.com/problems/two-sum/
8 | *
5 | * 1099. 小于 K 的两数之和 (Easy)
6 | * Two Sum 变式
7 | * https://leetcode-cn.com/problems/two-sum-less-than-k/
8 | *
3 | * 108. Convert Sorted Array to Binary Search Tree (Easy)
4 | * https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
5 | *
6 | * 739. Daily Temperatures
7 | * https://leetcode.com/problems/daily-temperatures/
8 | *
3 | * 1446. Consecutive Characters (Easy)
4 | * Max Consecutive Ones 变式
5 | * https://leetcode.com/problems/consecutive-characters/
6 | *
3 | * 1315. Sum of Nodes with Even-Valued Grandparent (Medium)
4 | * https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/
5 | *
3 | * 19. Remove Nth Node From End of List (Medium)
4 | * https://leetcode.com/problems/remove-nth-node-from-end-of-list/
5 | *
3 | * 1026. Maximum Difference Between Node and Ancestor (Medium)
4 | * https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/
5 | *
3 | * 125. Valid Palindrome (Easy)
4 | * https://leetcode.com/problems/valid-palindrome/
5 | *
6 | * 412. Fizz Buzz (Easy)
7 | * https://leetcode.com/problems/fizz-buzz/
8 | *
3 | * Insertion Sort
4 | * Finding the location of data[i] within the sorted list
5 | *
3 | * 543. Diameter of Binary Tree (Easy)
4 | * https://leetcode.com/problems/diameter-of-binary-tree/
5 | *
3 | * 938. Range Sum of BST (Easy)
4 | * https://leetcode.com/problems/range-sum-of-bst/
5 | *
6 | * 257. Binary Tree Paths (Easy)
7 | * https://leetcode.com/problems/binary-tree-paths/
8 | *
3 | * 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree (Medium)
4 | * https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/
5 | *
3 | * 236. Lowest Common Ancestor of a Binary Tree (Medium)
4 | * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
5 | *