Given two integers n and k, return all possible combinations ofknumbers chosen from the range[1, n].
You may return the answer in any order.
Example 1:
1 2 3 4
Input: n = 4, k = 2 Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] Explanation: There are 4 choose 2 = 6 total combinations. Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
Example 1:
1 2 3 4 5
Input: k = 3, n = 7 Output: [[1,2,4]] Explanation: 1 + 2 + 4 = 7 There are no other valid combinations.
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations ofcandidateswhere the chosen numbers sum totarget. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the
frequency
of at least one of the chosen numbers is different.
The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
Example 1:
1 2 3 4 5 6
Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] Explanation: 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations.
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.
For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.
Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots intos. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.
Example 1:
1 2
Input: s = "25525511135" Output: ["255.255.11.135","255.255.111.35"]
classSolution { public List<String> res = newArrayList<>(); public List<String> restoreIpAddresses(String s) { backtrack(s,0,0); return res; } publicvoidbacktrack(String s, int index, int point){ // index is slash if(point == 3){ //check the last part, the fourth part if(isValid(s, index, s.length() - 1)){//left close right close res.add(s); } return; } for(inti= index; i < s.length(); i++){// i++是树的横向,左边划分的杠在动,划的线数量不变,因为++之前都removeLast了 if(isValid(s, index, i)){ s = s.substring(0, i+1)+"."+s.substring(i+1); //在str的后⾯插⼊⼀个逗点 point++; backtrack(s, i + 2, point); //vertical point--; s = s.substring(0, i+1) + s.substring(i+2); // 回溯删掉逗点 }else{ break; } } }
private Boolean isValid(String s, int start, int end) { if (start > end) { returnfalse; } if (s.charAt(start) == '0' && start != end) { // 0开头的数字不合法 returnfalse; } intnum=0; for (inti= start; i <= end; i++) { if (s.charAt(i) > '9' || s.charAt(i) < '0') { // 遇到⾮数字字符不合法 returnfalse; } num = num * 10 + (s.charAt(i) - '0'); if (num > 255) { // 如果⼤于255了不合法 returnfalse; } } returntrue; }
For 90, each level, the repeated element can only show up next to each other
but for 491, repeated elements can randomly show up not necessarily next to each other that’s why we use hashset here.
Medium
Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.
!!!!In the provided Java code, the HashSet<Integer> hs variable is declared and instantiated within the backTracking method. Each time the backTracking method is called, a new HashSet<Integer> is created. Therefore, each invocation of the backTracking method has its own separate HashSet instance, which means that the HashSet variables do not share the same memory address across different method calls.
Each time backTracking is called, a new HashSet<Integer> hs is created.
This new HashSet is used to track elements that have already been considered in the current invocation of backTracking to avoid duplicate subsequences starting from the same index.
The HashSet helps ensure that each element in the current invocation of the loop is p
The used array is indeed the same array throughout the recursion. This is expected and desirable because it allows each recursive call to modify and check the same state of used, ensuring consistency across different levels of recursion.