lintcode-medium-Permutations

Given a list of numbers, return all possible permutations.

Example

For nums = [1,2,3], the permutations are:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

Challenge

Do it without recursion.

class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of permutations.
     */
    public ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> nums) {
        // write your code here

        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

        if(nums == null || nums.size() == 0)
            return result;

        ArrayList<Integer> line = new ArrayList<Integer>();
        boolean[] visited = new boolean[nums.size()];

        helper(result, line, visited, nums);

        return result;
    }

    public void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> line, boolean[] visited, ArrayList<Integer> nums){

        if(line.size() == nums.size()){
            result.add(new ArrayList<Integer>(line));
            return;
        }

        for(int i = 0; i < nums.size(); i++){
            if(!visited[i]){
                line.add(nums.get(i));
                visited[i] = true;
                helper(result, line, visited, nums);
                line.remove(line.size() - 1);
                visited[i] = false;
            }

        }

        return;
    }

}
时间: 2024-10-08 06:00:18

lintcode-medium-Permutations的相关文章

[Lintcode]15. Permutations/[Leetcode]46. Permutations

15. Permutations/46. Permutations 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers, return all possible permutations. Example Example 1: Input: [1] Output: [ [1]] Example 2: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3]

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium] digit counts

Digit Counts Count the number of k's between 0 and n. k can be 0 - 9. Example if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12) class Solution { /* * param k : As description. * param n : As description. * r

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium]Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return 2147483647 Example Given dividend = 100 and divisor = 9, return 11. public class Solution { /** * @param dividend the dividend

[lintcode medium] Two sum

Two Sum Given an array of integers, 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. Please no

[lintcode medium]Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Example IV -> 4 XII -> 12 XXI -> 21 XCIX -> 99 Clarification What is Roman Numeral? https://en.wikipedia.org/wiki