Algorithm
-
[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..
-
[Algo Rhythm🕺💃] LeetCode 784. Letter Case PermutationAlgo Rhythm🕺💃/LeetCode 2020. 7. 27. 15:12
📚 Problem description Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create. Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", "A1B2"] Input: S = "3z4" Output: ["3z4", "3Z4"] Input: S = "12345" Output: ["12345"] Note: S will be a string with length between 1 and 12. S wi..
-
[Algo Rhythm🕺💃] LeetCode - Number of Substrings With Only 1sAlgo Rhythm🕺💃/LeetCode 2020. 7. 19. 17:34
📚 Problem description Given a binary string s (a string consisting only of '0' and '1's). Return the number of substrings with all characters 1's. Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: s = "0110111" Output: 9 Explanation: There are 9 substring in total with only 1's characters. "1" -> 5 times. "11" -> 3 times. "111" -> 1 time. Example 2: Input: s = "101"..