for (int i = 0; i < letters.length(); i++) { String letter = letters.substring(i, i + 1); helper(digits.substring(1), combination + letter, result); } } }
public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> result = new ArrayList(); List<Integer> cur = new ArrayList(); helper(result, cur, 0, target, candidates, 0); return result; }
privatevoidhelper(List<List<Integer>> result, List<Integer> cur, int sum, int target, int[] candidates, int index){ if (sum > target) { return; }
if (sum == target) { result.add(new ArrayList(cur)); return; }
for (int i = 0;i < candidates.length; i++) { if (i < index) { continue; } cur.add(candidates[i]); helper(result, cur, sum + candidates[i], target, candidates, i); cur.remove(cur.size() - 1); } }
public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList(); helper(result, new ArrayList(), nums, newint[nums.length]); return result; }
privateint[] quickSort(int[] arr, int left, int right) { if (left < right) { int partitionIndex = partition(arr, left, right); quickSort(arr, left, partitionIndex - 1); quickSort(arr, partitionIndex + 1, right); } return arr; }
privateintpartition(int[] arr, int left, int right){ // 设定基准值(pivot) int pivot = left; int index = pivot + 1; for (int i = index; i <= right; i++) { if (arr[i] < arr[pivot]) { swap(arr, i, index); index++; } } swap(arr, pivot, index - 1); return index - 1; }
privatevoidswap(int[] arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
public ArrayList<Integer> GetLeastNumbers_Solution1(int [] arr, int k){ ArrayList<Integer> kNumbers = new ArrayList<Integer>(); if (arr == null || k <= 0 || k > arr.length) return kNumbers; int left = 0; int right = arr.length - 1; int index = partition(arr, left, right);
while(index!=k-1){ if(index<k-1){ start=index+1; index=partition(arr, left, right); }else{ end=index-1; index=partition(arr, left, right); } } for(int i=0;i<k;i++){ kNumbers.add(arr[i]); } return kNumbers; } privateintpartition(int[] arr, int left, int right){ // 设定基准值(pivot) int pivot = left; int index = pivot + 1; for (int i = index; i <= right; i++) { if (arr[i] < arr[pivot]) { swap(arr, i, index); index++; } } swap(arr, pivot, index - 1); return index - 1; }
privatevoidswap(int[] arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
publicvoidnextPermutation(int[] nums){ if (nums.length <= 0) return; int len = nums.length; for (int i = len - 1; i >= 0; i--) { if (i == 0) { Arrays.sort(nums); } else { if (nums[i] > nums[i-1]) { // 说明有逆序存在, 则下一个是将i-1 转变成i-1 后面比它大的数字 Arrays.sort(nums, i, len); for (int j=i;j<len;j++) { if (nums[j]>nums[i-1]) { swap(nums,i-1,j); return; } } } } } }
privatevoidswap(int[] nums, int i, int j){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; }
publicintmaxProduct(int[] nums){ int max = nums[0]; int min = nums[0]; int res = nums[0]; for(int i=1; i<nums.length;i++) { if (nums[i] < 0) { int temp = max; max = min; min = temp; } max = Integer.max(max * nums[i], nums[i]); min = Integer.min(min * nums[i], nums[i]); res = Integer.max(max, res); } return res; }
publicintsubarraySum(int[] nums, int k){ int count = 0; for (int i=0; i< nums.length;i++) { int sum = 0; for (int j=i;j < nums.length; j++) { sum += nums[j]; if (sum == k) { count++; } } } return count; }
public String multiply(String n1, String n2){ int n = n1.length(), m = n2.length(); int[] res = newint[n + m]; for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { int a = n1.charAt(i) - '0'; int b = n2.charAt(j) - '0'; int r = a * b; r += res[i + j + 1]; res[i + j + 1] = r % 10; res[i + j] += r / 10; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n + m; i++) { if (sb.length() == 0 && res[i] == 0) continue; sb.append(res[i]); } return sb.length() == 0 ? "0" : sb.toString(); }