MCPcopy Create free account

hub / github.com/FreeTymeKiyan/LeetCode-Sol-Res / functions

Functions1,974 in github.com/FreeTymeKiyan/LeetCode-Sol-Res

MethodisInteger
()
src/main/java/com/freetymekiyan/algorithms/level/medium/MiniParser.java:121
MethodisNumber
Trim heading and trailing whitespaces. Use 3 booleans to remember whether dot, exp, and number have shown. Then check violations. Situations are: 1) N
src/main/java/com/freetymekiyan/algorithms/level/hard/ValidNumber.java:39
MethodisNumber2
Two Pointers. Whitespace, +, -, digit, ., e. First skip whitespaces at the two ends with two pointers i and e. If out of bound, return false. Then ski
src/main/java/com/freetymekiyan/algorithms/level/hard/ValidNumber.java:92
MethodisNumber3
Trim and convert trimmed string to char array Deal with sign Deal with point Deal with digit after point Deal with no point or e Deal with digit after
src/main/java/com/freetymekiyan/algorithms/level/hard/ValidNumber.java:149
MethodisOneEditDistance
Edit distance: add, remove, or replace. Three possible situations: 1) s and t are of same length, replace 2) delete 1 char from s 3) delete 1 char fro
src/main/java/com/freetymekiyan/algorithms/level/medium/OneEditDistance.java:32
MethodisPalindrome
src/main/cpp/131_Palindrome_Partitioning.cpp:29
MethodisPowerOfFour
Power of 4 has only a single bit 1 on the highest bit. Plus, this single bit should be on odd digit. So create a number that has 1 on each and every d
src/main/java/com/freetymekiyan/algorithms/level/easy/PowerOfFour.java:24
MethodisPowerOfFourB
Math 4^n % 3 = 1 Prove by induction. Suppose 4^n - 1 can be divided by 3. 4^(n + 1) - 1 = 4 4^n - 1 = 3 4^n + (4^n - 1) So (4^n - 1) % 3 = 0
src/main/java/com/freetymekiyan/algorithms/level/easy/PowerOfFour.java:36
MethodisPowerOfThree
Check if n's modular of 3 is zero. If yes, divide n by 3 and check again. Stop till n is 1.
src/main/java/com/freetymekiyan/algorithms/level/easy/PowerOfThree.java:21
MethodisPowerOfThreeB
Find the max power of 3 within int range. It should be divisible by all power of 3s.
src/main/java/com/freetymekiyan/algorithms/level/easy/PowerOfThree.java:40
MethodisPowerOfThreeRecursive
Recursive version.
src/main/java/com/freetymekiyan/algorithms/level/easy/PowerOfThree.java:32
MethodisPowerOfTwo
2's power only has a single 1 at the highest bit. So n & (n - 1) should be 0. Check also if n is positive.
src/main/java/com/freetymekiyan/algorithms/level/easy/PowerOfTwo.java:18
MethodisPowerOfTwoB
One-liner using Integer.bitCount because only a single bit should be 1 for 2's power.
src/main/java/com/freetymekiyan/algorithms/level/easy/PowerOfTwo.java:25
MethodisRectangleCover
Count corners. For an exact overlap, the four most outside corners should show exactly once. The other corners should show 2 or 4 times. Otherwise, it
src/main/java/com/freetymekiyan/algorithms/level/hard/PerfectRectangle.java:69
MethodisReflected
(int[][] points)
src/main/java/com/freetymekiyan/algorithms/level/medium/LineReflection.java:30
MethodisSameTree
src/main/cpp/100_Same_Tree.cpp:27
MethodisSameTree
Recursive, pre-order check If both node's values are the same, left subtrees are same and so right Return true, otherwise return false
src/main/java/com/freetymekiyan/algorithms/level/easy/SameTree.java:18
MethodisScramble2
separate s1 into two parts, namely --s11--, --------s12-------- separate s2 into two parts, namely --s21--, --------s22--------, and test the correspo
src/main/java/com/freetymekiyan/algorithms/level/hard/ScrambleStr.java:71
MethodisSelfCrossing
(int[] x)
src/main/java/com/freetymekiyan/algorithms/level/medium/SelfCrossing.java:43
MethodisSubsequence
Two pointers. For each character in T, check whether it's the same as current character in S. If it's the same, move index in S by 1. If it reaches th
src/main/java/com/freetymekiyan/algorithms/level/medium/IsSubsequence.java:48
MethodisSubsequenceB
Recursion. Recurrence relation: whether s is the subsequence of t can be divided into 2 parts: 1) the first character of s is found in t 2) the rest o
src/main/java/com/freetymekiyan/algorithms/level/medium/IsSubsequence.java:70
MethodisSubtree
Recursive. If s has a subtree like t, t can be: 1. A subtree starting at s. 2. A subtree in s's left subtree. 3. A subtree in s's right subtree.
src/main/java/com/freetymekiyan/algorithms/level/easy/SubtreeOfAnotherTree.java:53
MethodisSymmetric
src/main/cpp/101_Symmetric_Tree.cpp:73
MethodisSymmetric
src/main/cpp/101_Symmetric_Tree.cpp:86
MethodisSymmetric
src/main/cpp/101_Symmetric_Tree.cpp:134
MethodisSymmetric
Iterative. Stack. DFS. Skip root since it's definitely symmetric. Push root's children onto a stack if they both exists. While stack is not empty: | P
src/main/java/com/freetymekiyan/algorithms/level/easy/SymmetricTree.java:44
MethodisSymmetricRec
Recursive. DFS. Depth first search both subtrees, n1 and n2. If at least one node is null: | Return true if they are both null, otherwise false. Then
src/main/java/com/freetymekiyan/algorithms/level/easy/SymmetricTree.java:86
MethodisTwoSym
src/main/cpp/101_Symmetric_Tree.cpp:44
MethodisValid
src/main/cpp/037_Sudoku_Solver.cpp:49
MethodisValidBST
Recursive. Pre-order. Check if root.val is bigger than value of rightmost node in left subtree. And smaller than value of leftmost node in right subtr
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidateBinarySearchTree.java:42
MethodisValidBST2
Recursive. In-order traversal. Base case: Check current root, if its null, return true. Recurrence relation: root must be larger than the largest in l
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidateBinarySearchTree.java:83
MethodisValidBST3
Recursive. Build a helper function with range. Check whether root's val is in range. Then check left and right recursively. Will fail if input include
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidateBinarySearchTree.java:108
MethodisValidBST4
Recursive. In-order. O(n) Time, O(n) Space. Inorder traversal, generate a list. The list should be in increasing order.
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidateBinarySearchTree.java:124
MethodisValidBSTB
Failed if input include Integer MAX and Integer MIN
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidateBST.java:48
MethodisValidBSTC
Inorder traversal, generate a list, should be increasing order
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidateBST.java:60
MethodisValidBSTD
Preorder Check if root.val is bigger than value of rightmost node in left subtree and smaller than value of leftmost node in right subtree.
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidateBST.java:84
MethodisValidSerialization
Stack. Iterate through the string characters. If it's a number, just push to stack. If it's a '#', we need to figure out some sub situations: 1) If th
src/main/java/com/freetymekiyan/algorithms/level/medium/VerifyPreorderSerializationOfABinaryTree.java:57
MethodisValidSerializationB
Degree. A value provide 2 out degrees and 1 in degree, except root. A '#' provide no out degree and 1 in degree. https://discuss.leetcode.com/topic/35
src/main/java/com/freetymekiyan/algorithms/level/medium/VerifyPreorderSerializationOfABinaryTree.java:80
MethodisValidSudoku2
Use three arrays of integers to do masking
src/main/java/com/freetymekiyan/algorithms/level/easy/ValidSudoku.java:61
MethodisValidSudoku3
hashtable, index as key, mask as value
src/main/java/com/freetymekiyan/algorithms/level/easy/ValidSudoku.java:89
Methoditerator
()
src/main/java/com/freetymekiyan/datastructures/LinkedListBag.java:17
Methodjump
src/main/cpp/045_Jump_Game_II.cpp:34
Methodjump
Use last to store how far we already can reach Compare i with last If we run out of it, update and add 1 more step to result Return if last is already
src/main/java/com/freetymekiyan/algorithms/level/hard/JumpGame2.java:29
MethodkSmallestPairs
Heap. Suppose len(nums1) = m, len(nums2) = n, there can be m n pairs. All integers in nums1 should start from the first integer in nums2. So add the f
src/main/java/com/freetymekiyan/algorithms/level/medium/FindKPairsWithSmallestSums.java:56
Methodkeys
()
src/main/java/com/freetymekiyan/datastructures/BST.java:104
Methodkeys
()
src/main/java/com/freetymekiyan/datastructures/TrieST.java:109
MethodkeysThatMatch
support '.' only @param pat string pattern with or without '.' @return all keys that match the given pattern
src/main/java/com/freetymekiyan/datastructures/TrieST.java:137
MethodkthSmallest
Heap (Priority Queue). Put first column or row into the min heap. Poll the root and put the next one in the same column or row into heap. Need to keep
src/main/java/com/freetymekiyan/algorithms/level/medium/KthSmallestElementInASortedMatrix.java:38
MethodkthSmallestBinarySearch
Binary Search. Binary search the rank of a certain value and find the one that's kth smallest. The initial range is matrix[0][0] to matrix[n - 1][n -
src/main/java/com/freetymekiyan/algorithms/level/medium/KthSmallestElementInASortedMatrix.java:65
MethodkthSmallestRec
Binary Search of A and B O(lg m + lg n) make i + j = k - 1 If Bj-1 < Ai < Bj, then Ai must be the k-th smallest, or else if Ai-1 < Bj < Ai, then Bj mu
src/main/java/com/freetymekiyan/algorithms/other/KthSmallestSortedArrays.java:29
MethodladderLength
create a queue to track the distance of each word in wordList from beginWord
src/main/cpp/127_Word_Ladder.cpp:93
MethodlargestBSTSubtree
(TreeNode root)
src/main/java/com/freetymekiyan/algorithms/level/medium/LargestBstSubtree.java:40
MethodlargestBSTSubtree
(TreeNode root)
src/main/java/com/freetymekiyan/algorithms/level/medium/LargestBstSubtree.java:79
MethodlargestDivisibleSubset
For a new integer S, S can be placed into the set as long as it can divide the smallest number of the set or is divisible by the largest number of the
src/main/java/com/freetymekiyan/algorithms/level/medium/LargestDivisibleSubset.java:46
MethodlargestNumber
(int[] nums)
src/main/java/com/freetymekiyan/algorithms/level/medium/LargestNumber_shuna.java:20
MethodlargestRectangleArea
src/main/cpp/085_Maximal_Rectangle.cpp:46
MethodleastBricks
Hash Table. The # of bricks crossed, B = # of rows, R - # of edges, E. min(B) = R - max(E) Crossing least bricks actually means crossing most edges. H
src/main/java/com/freetymekiyan/algorithms/level/medium/BrickWall.java:53
MethodleastBricks2
Same idea. More efficient. Use integer array instead of map. The size of the array can derive from provided note. Then use ternary operator instead of
src/main/java/com/freetymekiyan/algorithms/level/medium/BrickWall.java:74
MethodleastInterval
Priority Queue. The actual task name does not matter. As long as we know the number of tasks for each task, we can get the intervals needed. First, go
src/main/java/com/freetymekiyan/algorithms/level/medium/TaskScheduler.java:49
MethodleastInterval2
Math. Find the idle intervals. Assuming A is the task with most count, and all of them are scheduled in a grid. The maximum number of idle intervals c
src/main/java/com/freetymekiyan/algorithms/level/medium/TaskScheduler.java:95
MethodlengthOfLastWord
src/main/cpp/058_Length_of_Last_Word.cpp:25
MethodlengthOfLastWord
(String s)
src/main/java/com/freetymekiyan/algorithms/level/easy/LengthOfLastWord.java:22
MethodlengthOfLongestSubstringKDistinct
(String s, int k)
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestSubstringWithAtMostKDistinctCharacters.java:34
MethodlengthOfLongestSubstringKDistinct2
(String s, int k)
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestSubstringWithAtMostKDistinctCharacters.java:70
MethodlengthOfLongestSubstringTwoDistinct2
(String s)
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestSubstringWithAtMostTwoDistinctCharacters.java:69
MethodletterCasePermutation
Backtracking Since S is alphanumerical, numbers can be skipped and only check letters. To generate all possible strings, all possibilities must be sea
src/main/java/com/freetymekiyan/algorithms/level/easy/LetterCasePermutation.java:37
MethodletterCombinations
Backtracking. DFS. The backtrack is like a DFS function that generates all possible combinations by: 1) Assign one letter at current level 2) Call the
src/main/java/com/freetymekiyan/algorithms/level/medium/LetterCombinationsOfPhoneNum.java:50
MethodletterCombinations2
BFS. Build combination level by level. The length of the combination is the same as the level. Add all possible letters to each of the result in previ
src/main/java/com/freetymekiyan/algorithms/level/medium/LetterCombinationsOfPhoneNum.java:85
MethodlevelOrder
src/main/cpp/102_Binary_Tree_Level_Order_Traversal.cpp:42
MethodlevelOrder
Queue Get size of the queue each time Iterate that many times to build current level
src/main/java/com/freetymekiyan/algorithms/level/easy/BTLevelOrder.java:36
MethodlevelOrder
BFS. Instead of regular BFS, visit one level at each iteration. By getting the size of the queue, we know how many nodes in current level.
src/main/java/com/freetymekiyan/algorithms/level/easy/BinaryTreeLevelOrderTraversal.java:42
MethodlevelOrder2
DFS. Root is level 0, pass level + 1 to its children during DFS. Add the node to its level list in the result. Stop when reach null node.
src/main/java/com/freetymekiyan/algorithms/level/easy/BinaryTreeLevelOrderTraversal.java:72
MethodlevelOrderBottom
see 102. Binary Tree Level Order Traversal
src/main/cpp/107_Binary_Tree_Level_Order_Traversal_II.cpp:43
MethodlevelOrderBottom
(TreeNode root)
src/main/java/com/freetymekiyan/algorithms/level/easy/BinaryTreeLevelOrderTraversal2.java:36
MethodlevelOrderBottom
Use a level list to store the nodes of this level addRecursive root to it to begin Build next level with current level, add current level value to res
src/main/java/com/freetymekiyan/algorithms/level/easy/LevelOrderBottomUp.java:42
MethodlexicalOrder
(int n)
src/main/java/com/freetymekiyan/algorithms/level/medium/LexicographicalNumbers.java:19
MethodlongestCommonPrefix
Find common prefix one by one from the end of the input string array Overwrite the ith string with common prefix result Thus space usage is reduced Re
src/main/java/com/freetymekiyan/algorithms/level/easy/LongestCommonPrefix.java:21
MethodlongestCommonPrefix2
Only need to know the length of prefix Initialize result with first word Traverse from second word to last word Get minimum length of current result a
src/main/java/com/freetymekiyan/algorithms/level/easy/LongestCommonPrefix.java:55
MethodlongestConsecutive
src/main/cpp/128_Longest_Consecutive_Sequence.cpp:24
MethodlongestPalindrome
Hash Table. From input string we can get character counts. Characters with even count can all be used in a palindrome. For characters with odd count,
src/main/java/com/freetymekiyan/algorithms/level/easy/LongestPalindrome.java:41
MethodlongestPalindromeB
Manacher's Algorithm, O(n) Time. S = “abba” => T = “^#a#b#b#a#$”. http://www.felix021.com/blog/read.php?2040 http://leetcode.com/2011/11/longest-palin
src/main/java/com/freetymekiyan/algorithms/level/medium/LongestPalindromicSubstring.java:60
MethodlongestPalindromeC
O(n^2) Time, O(1) Space Expand from center character and center of two chars Update result according to the returned length
src/main/java/com/freetymekiyan/algorithms/level/medium/LongestPalindromicSubstring.java:103
MethodlongestPrefix
@return the longest prefix exists in the trie
src/main/java/com/freetymekiyan/datastructures/TrieST.java:165
MethodlongestSubstring
Divide and conquer. For those characters in string which repeat less than k times, we try to divide the string into two parts: 1) left part: from the
src/main/java/com/freetymekiyan/algorithms/level/medium/LongestSubstringwithAtLeastKRepeatingCharacters.java:38
MethodlongestValidParenthesesB
DP
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestValidParen.java:71
MethodlongestValidParenthesesC
Two pass Use a stack to store index of unmatched parentheses Go through the stack and find maximum of difference between indices Include len - first a
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestValidParen.java:98
MethodlowestCommonAncestor
Iterative. In BST, the lca's value can only be [p, q]. And lca is the first from top to bottom that lies in range. If the value is less than both p an
src/main/java/com/freetymekiyan/algorithms/level/easy/LowestCommonAncestorOfABinarySearchTree.java:41
MethodlowestCommonAncestorB
Recursion. LCA's value must be between [p, q]. Recurrent relation: If root's value < p's and q's, LCA is in the right subtree. If root's value > p's a
src/main/java/com/freetymekiyan/algorithms/level/easy/LowestCommonAncestorOfABinarySearchTree.java:66
Functionmain
src/main/cpp/092_Reverse_Linked_List_II.cpp:50
Functionmain
src/main/cpp/199_Binary_Tree_Right_Side_View.cpp:62
Functionmain
src/main/cpp/171_Excel_Sheet_Column_Number.cpp:41
Functionmain
src/main/cpp/072_Edit_Distance.cpp:47
Functionmain
src/main/cpp/150_Evaluate_Reverse_Polish_Notation.cpp:46
Functionmain
src/main/cpp/011_Container_With_Most_Water.cpp:34
Functionmain
src/main/cpp/010_Regular_Expression_Matching.cpp:43
Functionmain
src/main/cpp/141_Linked_List_Cycle.cpp:42
Functionmain
src/main/cpp/039_Combination_Sum.cpp:50
Functionmain
src/main/cpp/055_Jump_Game.cpp:37
Functionmain
src/main/cpp/089_Gray_Code.cpp:33
Functionmain
src/main/cpp/073_Set_Matrix_Zeroes.cpp:84
← previousnext →1,201–1,300 of 1,974, ranked by callers