Algo Rhythm🕺💃/LeetCode
-
[Algo Rhythm🕺💃] LeetCode 720. Longest Word in DictionaryAlgo Rhythm🕺💃/LeetCode 2020. 9. 4. 01:05
📚 Problem description Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string. Example 1: Input: words = ["w","wo","wor","worl", "world..
-
[Algo Rhythm🕺💃] LeetCode 208. Implement Trie (Prefix Tree)Algo Rhythm🕺💃/LeetCode 2020. 9. 3. 16:04
📚 Problem description Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false trie.startsWith("app"); // returns true trie.insert("app"); trie.search("app"); // returns true Note: You may assume that all inputs are consist of lowercase letters a-z. All inputs ar..
-
[Algo Rhythm🕺💃] LeetCode 653. Two Sum IV - Input is a BSTAlgo Rhythm🕺💃/LeetCode 2020. 8. 24. 15:24
📚 Problem description Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 28 Output: False 🎯 Solution /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNo..
-
[Algo Rhythm🕺💃] LeetCode 167. Two Sum II - Input array is sortedAlgo Rhythm🕺💃/LeetCode 2020. 8. 24. 15:19
📚 Problem description Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Note: Your returned answers (both index1 and index2) are not zero-based. You may assume that each inp..
-
[Algo Rhythm🕺💃] LeetCode 78. SubsetsAlgo Rhythm🕺💃/LeetCode 2020. 8. 22. 23:38
📚 Problem description Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 🎯 Solution 1. Backtracking class Solution { List answer = new ArrayList(); public List subsets(int[] nums) { List list = new ArrayList(); bac..
-
[Algo Rhythm🕺💃] LeetCode 90. SubsetsIIAlgo Rhythm🕺💃/LeetCode 2020. 8. 22. 23:24
📚 Problem description Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 🎯 Solution class Solution { List ans = new ArrayList(); public List subsetsWithDup(int[] nums) { Arrays.sort(nums); backtrack(nums, ..
-
[Algo Rhythm🕺💃] LeetCode 46. PermutationsAlgo Rhythm🕺💃/LeetCode 2020. 8. 22. 22:35
📚 Problem description Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 🎯 Solution class Solution { List ans = new ArrayList(); public List permute(int[] nums) { boolean[] visited = new boolean[nums.length]; backtrack(nums, visited, new ArrayList()); return ans; } public void backtra..
-
[Algo Rhythm🕺💃] LeetCode 1. Two SumAlgo Rhythm🕺💃/LeetCode 2020. 8. 22. 22:13
📚 Problem description Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. 🎯 Solution 1. Brute Force class Solution { public int[] tw..