MCPcopy Create free account

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

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

MethodaddTwoNumbers
Math. Check the two input list first. If one is null, return the other. Now the two heads are not null. Create a dummy node and a current pointer star
src/main/java/com/freetymekiyan/algorithms/level/medium/AddTwoNum.java:38
MethodaddWord
(String word)
src/main/java/com/freetymekiyan/algorithms/level/medium/AddAndSearchWord.java:44
MethodalienOrder
Graph. Topological Sort. BFS. Two steps: 1) Build a graph and in-degree from the given words. 2) Do topological sort. <p> Topological sort based on Ka
src/main/java/com/freetymekiyan/algorithms/level/hard/AlienDictionary.java:55
MethodasteroidCollision2
Directly using java.util.stack. Not recommended since stack has been deprecated. Create a stack to deal with collisions. For each asteroids, ast: | Ch
src/main/java/com/freetymekiyan/algorithms/level/medium/Asteroids.java:135
Methodatoi2
(String str)
src/main/java/com/freetymekiyan/algorithms/level/easy/atoi.java:90
Methodbacktrack
backtracking from top-left to bottom-right
src/main/cpp/037_Sudoku_Solver.cpp:62
Methodbacktrack
src/main/cpp/040_Combination_Sum_II.cpp:35
Methodbacktrack
src/main/cpp/017_Letter_Combinations_of_a_Phone_Number.cpp:35
Methodbacktrack
src/main/cpp/077_Combinations.cpp:30
Methodbacktrack2
Backtracking. Duplicate with previous number will only be added if: The previous number is already in subset.
src/main/java/com/freetymekiyan/algorithms/level/medium/Subsets2.java:70
Methodbfs
create a queue to track the distance of each word in wordList from beginWord
src/main/cpp/126_Word_Ladder_II.cpp:65
Methodbfs
looking for connection from both ends
src/main/cpp/126_Word_Ladder_II.cpp:195
MethodbinaryTreePaths
DFS. The path from root to leaf can be obtained by backtracking. While backtrack we maintain a current path, and a list of result paths. To traverse t
src/main/java/com/freetymekiyan/algorithms/level/easy/BinaryTreePaths.java:40
MethodbinaryTreePaths2
DFS. Recurrence relation: The paths consist of root + all subtrees paths. Base case: If root == null, the resukt list would be empty. If root is a lea
src/main/java/com/freetymekiyan/algorithms/level/easy/BinaryTreePaths.java:70
MethodbuildTree
Get root in preorder and search root in inorder. Then find range for left subtree and right subtree. Recurse down to build subtrees and connect to roo
src/main/java/com/freetymekiyan/algorithms/level/medium/ConstructBTPreInOrder.java:25
MethodbuildTree
DFS, find root, find range of left and right sub trees The calculation of post array is trivial For left subtree, ps = ps, pe = ps - is - 1 + pos(offs
src/main/java/com/freetymekiyan/algorithms/level/medium/ConstructBTFromInPostOrder.java:19
MethodbuildTree2
Optimized. Replace linear scan with a map. So that the index of root can be returned in O(1).
src/main/java/com/freetymekiyan/algorithms/level/medium/ConstructBTPreInOrder.java:54
MethodbulbSwitch
Math. A bulb ends up on if it's switched an odd number of times. Bulb i is switched in round d iff d divides i. So bulb i ends up on iff it has an odd
src/main/java/com/freetymekiyan/algorithms/level/medium/BulbSwitcher.java:34
MethodcalcEquation
(String[][] equations, double[] values, String[][] queries)
src/main/java/com/freetymekiyan/algorithms/level/medium/EvaluateDivision.java:41
Methodcalculate
(String s)
src/main/java/com/freetymekiyan/algorithms/level/hard/BasicCalculator.java:32
Methodcalculate
The easy way would be using a stack to store high priority results and integers. Then go through the stack to get final result. How to remove the stac
src/main/java/com/freetymekiyan/algorithms/level/medium/BasicCalculator2.java:32
MethodcalculateMinimumHP
Build a table for the minimum hp needed to get to the bottom right Build from bottom right to get minimum from i, j to the end Instead of build from t
src/main/java/com/freetymekiyan/algorithms/level/hard/DungeonGame.java:45
MethodcanAttendMeetings
Sort. The actual problem is detecting whether there is any overlap between meetings. Sort the intervals. From second interval, check each interval wit
src/main/java/com/freetymekiyan/algorithms/level/easy/MeetingRooms.java:31
MethodcanBePalindrome
use a hash table to store count iterate the hash table to check whether no or only 1 character appeared odd times
src/main/java/com/freetymekiyan/algorithms/other/CanBePalindrome.java:14
MethodcanCompleteCircuit
src/main/cpp/134_Gas_Station.cpp:24
MethodcanCompleteCircuit
Use restGas to store the gas left for current trip Use previous to store the gas needed for previous trips Go through the list and calculate restGas I
src/main/java/com/freetymekiyan/algorithms/level/medium/GasStation.java:29
MethodcanConstruct
(String ransomNote, String magazine)
src/main/java/com/freetymekiyan/algorithms/level/easy/RansomNote.java:26
MethodcanCross2
DP, top-down. Recurrence relation: canCross(n, k) denotes whether (n + 1)th stone can be reached with k steps for all k from [0, n-1]: | canCross(n, k
src/main/java/com/freetymekiyan/algorithms/level/hard/FrogJump.java:100
MethodcanFinish
Topological Sort. BFS. Detect whether a cycle exists in a directed graph. Also make sure it's connected. Implementation: Build an array of in-degrees
src/main/java/com/freetymekiyan/algorithms/level/medium/CourseSchedule.java:57
MethodcanFinishB
Topological Sort. DFS. Detect whether there is a cycle. Build an adjacency list with edge list. The DFS toposort on the graph.
src/main/java/com/freetymekiyan/algorithms/level/medium/CourseSchedule.java:96
MethodcanJump2
(int[] A)
src/main/java/com/freetymekiyan/algorithms/level/medium/JumpGame.java:53
MethodcanWin
(String s)
src/main/java/com/freetymekiyan/algorithms/level/medium/FlipGame2.java:31
MethodcanWinNim
when n is 4, no matter you remove 1 or 2 or 3 stones, you always lose suppose n = 4k, always lose when k is k+1, n is 4k+4, which is 4 more stones to
src/main/java/com/freetymekiyan/algorithms/level/easy/NimGame.java:32
Methodcandy
O(n) Time, O(n) Space From left to right, if ratings[i] increase, give one more From right to left, if ratings[i] increase, give one more Answer shoul
src/main/java/com/freetymekiyan/algorithms/level/hard/Candy.java:30
MethodcandyB
O(n) Time, O(1) Space Use a var to store decreasing sequence length Use a var to store previous candies
src/main/java/com/freetymekiyan/algorithms/level/hard/Candy.java:50
Methodchar2val
convert char to bitcode
src/main/cpp/187_Repeated_DNA_Sequences.cpp:35
Methodchar2val
convert char to bitcode
src/main/cpp/187_Repeated_DNA_Sequences.cpp:98
Methodcheck
Check if a number is available or not.
src/main/java/com/freetymekiyan/algorithms/level/medium/DesignPhoneDirectory.java:76
MethodcheckDupWithinKB
Simple Solution, 2 loops, O(kn)
src/main/java/com/freetymekiyan/algorithms/other/DupWithinKDistance.java:56
MethodcheckTree
# of edges = # of vertices - 1 No cycles, use dfs to do cycle detection Connected, which means all nodes are visited
src/main/java/com/freetymekiyan/algorithms/other/IsGraphTree.java:20
MethodcheckValidString2
Recursive. This is a search problem. Recurrence relation: The whole string is valid if: Goes without is valid or goes with left it's valid or goes wit
src/main/java/com/freetymekiyan/algorithms/level/medium/ValidParenthesisString.java:64
MethodclimbStairs
Bottom-up approach Remember the previous two solutions
src/main/java/com/freetymekiyan/algorithms/level/easy/ClimbingStairs.java:17
MethodclimbStairs2
Top-down approach with memory function
src/main/java/com/freetymekiyan/algorithms/level/easy/ClimbingStairs.java:32
MethodclimbStairsBottomUp
(int n)
src/main/java/com/freetymekiyan/algorithms/level/easy/ClimbingStairs.java:48
MethodcloneGraph
DFS. Pass the node and cloned map to its neighbors. Add neighbors backtrack result to its neighbors and return.
src/main/java/com/freetymekiyan/algorithms/level/medium/CloneGraph.java:46
MethodcloneGraph2
BFS. O(V) Time. O(V) Space. Use map<Integer, UndirectedGraphNode> to store cloned new graph nodes. For each visit, connect the node with neighboring n
src/main/java/com/freetymekiyan/algorithms/level/medium/CloneGraph.java:87
Methodclosest
(int[] nums)
src/main/java/com/freetymekiyan/algorithms/other/TwoSumClosestPair.java:12
MethodclosestB
(int[] nums)
src/main/java/com/freetymekiyan/algorithms/other/TwoSumClosestPair.java:28
MethodclosestKValues
(TreeNode root, double target, int k)
src/main/java/com/freetymekiyan/algorithms/level/hard/ClosestBinarySearchTreeValue2.java:40
MethodclosestValue
Binary Search. Recursive Get root's value first, a. If target < root's value, the next root will be left child. Else it should be right child. If the
src/main/java/com/freetymekiyan/algorithms/level/easy/ClosestBinarySearchTreeValue.java:30
MethodclosestValue2
Binary Search. Iterative. Initialize answer as root's value. While root is not null: | If target is closer to current node's value then result: | Up
src/main/java/com/freetymekiyan/algorithms/level/easy/ClosestBinarySearchTreeValue.java:51
MethodcoinChange
(int[] coins, int amount)
src/main/java/com/freetymekiyan/algorithms/level/medium/CoinChange.java:25
Methodcollide2
Non-recursive.
src/main/java/com/freetymekiyan/algorithms/level/medium/Asteroids.java:101
MethodcombinationSum
src/main/cpp/039_Combination_Sum.cpp:24
MethodcombinationSum
Sort the array [2, 3, 6, 7], 7
src/main/java/com/freetymekiyan/algorithms/level/medium/CombinationSum.java:33
MethodcombineB
Backtracking. From n to 1.
src/main/java/com/freetymekiyan/algorithms/level/medium/Combinations.java:62
Methodcompare
(String num1, String num2)
src/main/java/com/freetymekiyan/algorithms/level/medium/LargestNumber_shuna.java:22
Methodcompare
(int[] person1, int[] person2)
src/main/java/com/freetymekiyan/algorithms/level/medium/QueueReconstructionByHeight.java:57
MethodcomputeArea
The total area covered by two rectilinear rectangles is the sum of the two rectangles subtracted by their overlapping area.
src/main/java/com/freetymekiyan/algorithms/level/easy/RectangleArea.java:24
MethodcomputeAreaB
More concise version. Max right with left, so that when right is smaller that left, right = left, thus right - left = 0. So with top and bottom.
src/main/java/com/freetymekiyan/algorithms/level/easy/RectangleArea.java:44
Methodconnect
src/main/cpp/117_Populating_Next_Right_Pointers_in_Each_Node_II.cpp:47
Methodconnect
src/main/cpp/116_Populating_Next_Right_Pointers_in_Each_Node.cpp:56
Methodconnect
src/main/cpp/126_Word_Ladder_II.cpp:178
Methodconnect
Iterative. Connect next level at current level. Current level is already connected by its previous level. The first level, which is the root level, do
src/main/java/com/freetymekiyan/algorithms/level/hard/PopulatingNextRight2.java:52
Methodconnect
Iterative. Connect the next level from current level. For each level, get the left most node. Start from the left most node, while node is not null: |
src/main/java/com/freetymekiyan/algorithms/level/medium/PopulatingNextRight.java:54
Methodconnect2
Recursive. Connect next level at current level. Then recursively connect the next level. Stop until level head is null. For each level, the previous l
src/main/java/com/freetymekiyan/algorithms/level/hard/PopulatingNextRight2.java:91
Methodconnected
(int p, int q)
src/main/java/com/freetymekiyan/algorithms/other/QuickUnionUF.java:20
MethodcontainsNearbyDuplicate
(int[] nums, int k)
src/main/java/com/freetymekiyan/algorithms/level/easy/ContainsDuplicate2.java:17
MethodcontainsNearbyDuplicate2
Don't need to save number and index in a map. Just loop through the array with a sliding window(set of size k).
src/main/java/com/freetymekiyan/algorithms/level/easy/ContainsDuplicate2.java:33
MethodcontainsTree
t1 root of tree, t2 root of subtree
src/main/java/com/freetymekiyan/algorithms/other/IsSubtree.java:15
Methodconvert
src/main/cpp/006_ZigZag_Conversion.cpp:25
Methodconvert
Build an array of StringBuffers Traverse the given string and append characters in correct StringBuffer Append all other rows to first row to get outp
src/main/java/com/freetymekiyan/algorithms/level/easy/ZigZagConversion.java:28
MethodconvertToTitle
Iterative. Map 0 to A, 1 to B, ..., 25 to Z. So we need to decrease n by 1 first. For each iteration, reduce n by 1. Convert n to a character and add
src/main/java/com/freetymekiyan/algorithms/level/easy/ExcelSheetColumnTitle.java:33
MethodconvertToTitle
Get the remainder in each loop It should be the last digit Note that the map shall have 1 offset
src/main/java/com/freetymekiyan/algorithms/level/easy/ExcelSheetColTitle.java:41
MethodconvertToTitle2
Recursive. Recurrence relation: Convert the rest of the characters + Convert current char. Base case: n <= 0, return abn empty string.
src/main/java/com/freetymekiyan/algorithms/level/easy/ExcelSheetColumnTitle.java:50
MethodconvertToTitle3
Recursive. More readable.
src/main/java/com/freetymekiyan/algorithms/level/easy/ExcelSheetColumnTitle.java:58
MethodcopyRandomList
Use a hashmap to store map between original node and copy node
src/main/java/com/freetymekiyan/algorithms/level/hard/CopyListWithRandomP.java:22
MethodcopyRandomList2
Insert a same node after current node Then split into two lists
src/main/java/com/freetymekiyan/algorithms/level/hard/CopyListWithRandomP.java:44
MethodcopyRandomList3
Build a new list and connect original and new node together
src/main/java/com/freetymekiyan/algorithms/level/hard/CopyListWithRandomP.java:75
MethodcountAndSay
String. Simulate the process. Generate the next sequence. Repeat n-1 times.
src/main/java/com/freetymekiyan/algorithms/level/easy/CountAndSay.java:27
MethodcountComponents
Quick union
src/main/java/com/freetymekiyan/algorithms/level/medium/NumberofConnectedComponentsinanUndirectedGraph.java:35
MethodcountDigitOne
e.g n = 3141592, m = 100, a = 31415, b = 92 hundreds-digit of n is 1 for 3142 times, each has a 100 long streak, (a / 10 100) + 100 when m = 1000, a =
src/main/java/com/freetymekiyan/algorithms/level/hard/NumberOfDigitOne.java:29
MethodcountNodes
(TreeNode root)
src/main/java/com/freetymekiyan/algorithms/level/medium/CountCompleteTreeNodes.java:19
MethodcountNumbersWithUniqueDigits
DP. Think about how to use combination to build a number with unique digits. f(1) = 10, f(2) = 9 9, f(3) = 9 9 8, f(4) = 9 9 8 7 ... f(k) = 9 9 8 ...
src/main/java/com/freetymekiyan/algorithms/level/medium/CountNumbersWithUniqueDigits.java:33
MethodcountPrimes
(int n)
src/main/java/com/freetymekiyan/algorithms/level/easy/CountPrimes.java:23
MethodcountRangeSum
(int[] nums, int lower, int upper)
src/main/java/com/freetymekiyan/algorithms/level/hard/CountOfRangeSum.java:28
MethodcountRangeSum2
(int[] nums, int l, int r, int lower, int upper)
src/main/java/com/freetymekiyan/algorithms/level/hard/CountOfRangeSum.java:54
MethodcountSmaller
(int[] nums)
src/main/java/com/freetymekiyan/algorithms/level/hard/CountOfSmallerNumbersAfterSelf.java:30
MethodcountUnivalSubtrees
(TreeNode root)
src/main/java/com/freetymekiyan/algorithms/level/medium/CountUnivalueSubtrees.java:32
MethodcreateExamples
()
src/test/java/com/freetymekiyan/algorithms/level/medium/FindRightIntervalTest.java:13
Methoddecode
(String shortUrl)
src/main/java/com/freetymekiyan/algorithms/level/medium/EncodeAndDecodeTinyURL.java:39
Methoddecode
(String s)
src/main/java/com/freetymekiyan/algorithms/level/medium/EncodeAndDecodeStrings.java:61
MethoddecodeString
Stack. Use two stacks to save current level information: One is the # of repeat times, the other is the pattern so far. For nested brackets, think abo
src/main/java/com/freetymekiyan/algorithms/level/medium/DecodeString.java:39
MethoddeepestLeftLeaf
(TreeNode root)
src/main/java/com/freetymekiyan/algorithms/other/DeepestLeftLeafNode.java:19
MethoddeepestNode
(Node root)
src/main/java/com/freetymekiyan/algorithms/other/DeepestNode.java:14
MethoddelMax
should throw exception when empty
src/main/java/com/freetymekiyan/datastructures/MaxPQ.java:42
Methoddelete
(Key key)
src/main/java/com/freetymekiyan/datastructures/ST.java:16
Methoddelete
(Key key)
src/main/java/com/freetymekiyan/datastructures/BST.java:132
Methoddelete
delete a key and its corresponding value from the trie
src/main/java/com/freetymekiyan/datastructures/TrieST.java:187
Methoddelete
Copy the data from the next node to the node to be deleted and delete the next node
src/main/java/com/freetymekiyan/algorithms/other/DeleteLinkedListNode.java:18
← previousnext →901–1,000 of 1,974, ranked by callers