Jan 17 - Permutations; BackTracking; Array; Recursion;

代码:

public class Solution {
    List<List<Integer>> resultList = new ArrayList<>();
    boolean flag = false;
    public List<List<Integer>> permute(int[] nums) {
        int len = nums.length;
        if(len == 0) return resultList;
        Arrays.sort(nums);
        permute_array(nums, len);
        return resultList;
    }

    public void permute_array(int[] nums, int len){
        if(flag) return;
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < nums.length; i++) list.add(nums[i]);
        resultList.add(list);
        int maxIndex = len - 1, minIndex = len -1;
        int max = nums[maxIndex];
        int min = nums[minIndex];
        for(int i = len-1; i>=0; i--){
            if(nums[i] < min){
                nums[minIndex] = nums[i];
                nums[i] = min;
                permute_array(nums, len);
            }
            if(nums[i] < max){
                int j = maxIndex;
                while(j >= 1 && nums[i] < nums[j-1]) j--;
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                permute_array(nums, len);
            }
            else{
                if(i == 0) flag = true;;
                max = nums[i];
                for(int j = i;j < len-1; j++) nums[j] = nums[j+1];
                nums[len-1] = max;
                minIndex = i;
                maxIndex = len-1;
            }
        }
    }
}

  

时间: 2025-01-02 03:55:04

Jan 17 - Permutations; BackTracking; Array; Recursion;的相关文章

Jan 16 - Combination Sum; Array; BackTracking;

添加两个List:List<Integer> listSum 存储对应组合的和:List<List<Integer>> list 存储可能的组合 resultList存储和==target的组合 代码: public class Solution { public List<Integer> listSum; public List<List<Integer>> list; public List<List<Integer

Jan 17 - Rotate Image; 2D Array; xPos and yPos;

代码: public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i = 0; i < n/2; i++){ int num = n-i*2; for(int j = 0; j < num-1; j++){ int temp = matrix[i][i+j]; matrix[i][i+j] = matrix[i+num-1-j][i]; matrix[i+num-1-j

Permutations of Array

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. Thoughts: You take first element of an array (k=0) and exchange it with any

[CareerCup] 17.6 Sort Array 排列数组

17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n - m (that is, find the smallest such sequence).

Jan 11 - Contains Duplicate; Array; Traverse; Integer;

遍历数组找出有没有重复的元素 首先想到用一个数组记录出现的元素的个数 代码: public class Solution { public boolean containsDuplicate(int[] nums) { if(nums.length == 0) return true; int[] checkElement = new int[Integer.MAX_VALUE]; for(int i = 0; i < nums.length; i++){ if(checkElement[num

Jan 19 - Unique Paths; Array; DP;

first of all, we're goin to create a 2D array(int[][] grid) to store the number of paths to each position. we get the minimum value of m and n, mark it as min, then look through the row and column of grid[i][i] in each loop, when i == 0, we know we a

Jan 18 - Jump Game; Array; Greedy;

一开始以为是用DP解决,create a boolean array to store the status which mark whether the person can get to the end from current position. But, by this methods, we would encounter the worst case, which is there're hugh number of index and from which person would

Jan 12 - Summary Ranges;Array;String

Functions of StringBuilder: 代码: public class Solution { public List<String> summaryRanges(int[] nums) { List<String> list = new ArrayList<>(); if(nums.length == 0) return list; int head = 0; int k = -1; StringBuilder sb = new StringBuild

Intermediate Jan 17 2016