成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[Leetcode]PermutationsI II Next Permutation Permut

ChristmasBoy / 3627人閱讀

摘要:解題思路這道題是要將排列按字典序排列,然后求出下一個排列,一種辦法是我們先求出所有的排序情況,但是題目規(guī)定不能占有額外空間。每次求出一個數(shù)字后,要及時的把它從中刪除掉。采用來構(gòu)造結(jié)果序列。

Permutations
Given a collection of distinct 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],
  [3,2,1]
]

1.解題思路

此題為求排列,與組合相比較,排列是順序相關(guān)的,但是已經(jīng)被選中的數(shù)字是不能再次出現(xiàn)的,所以我們想到維護(hù)一個isUsed[]數(shù)組來表示某個數(shù)字是否已被選中過。

2.代碼

public class Solution {
    List> res=new ArrayList>();
    public List> permute(int[] nums) {
        if(nums.length==0) return res;
        boolean[] isUsed=new boolean[nums.length];
        permuteHelper(nums,isUsed,nums.length,new ArrayList());
        return res;
    }
    private void permuteHelper(int[] nums, boolean[] isUsed,int count,List pre){
        if(count==0){
            res.add(pre);
            return;
        }
        for(int i=0;i cur=new ArrayList(pre);
                cur.add(nums[i]);
                isUsed[i]=true;
                permuteHelper(nums,isUsed,count-1,cur);
                isUsed[i]=false;
            }
        }
    }
}

Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

1.解題思路

包含重復(fù)的數(shù)字,所以我們要對重復(fù)的排列序列進(jìn)行排除,首先我們對數(shù)組進(jìn)行排序,然后當(dāng)發(fā)現(xiàn)某個數(shù)與前一個數(shù)相同時,就判斷前一個數(shù)是否被選中,如果未被選中,則排除掉當(dāng)前重復(fù)數(shù),直接進(jìn)入下一個;因?yàn)槿绻耙粋€數(shù)已被選中,說明現(xiàn)在正處于前一個數(shù)的遞歸選數(shù)字過程中,則不能排除當(dāng)前重復(fù)數(shù)。

public class Solution {
    List> res=new ArrayList>();
    public List> permuteUnique(int[] nums) {
        if(nums.length==0) return res;
        Arrays.sort(nums);
        permuteUniqueHelper(nums,new boolean[nums.length],nums.length,new ArrayList());
        return res;
    }
    private void permuteUniqueHelper(int[] nums,boolean[] used,int count, List pre){
        if(count==0){
            res.add(pre);
            return;
        }
        for(int i=0;i0&&nums[i]==nums[i-1]&&!used[i-1]) continue;
            List cur=new ArrayList(pre);
            if(!used[i]){
                cur.add(nums[i]);
                used[i]=true;
                permuteUniqueHelper(nums,used,count-1,cur);
                used[i]=false;
            }
        }
    }
}

Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

1.解題思路

這道題是要將排列按字典序排列,然后求出下一個排列,一種辦法是我們先求出所有的排序情況,但是題目規(guī)定不能占有額外空間。根據(jù)example, 我們發(fā)現(xiàn)其實(shí)我們可以從數(shù)組后面開始比較相鄰的兩個數(shù),如果后面的數(shù)小于前面的數(shù),則繼續(xù)往前;如果后面的數(shù)大于前面的數(shù),將前面那個數(shù)下標(biāo)標(biāo)記為i-1,則我們知道i-1這個數(shù)的位置是需要交換,那和后面的哪個交換呢?肯定是和從i開始往后找比i-1數(shù)大,但是又是后面最小的數(shù)作交換。
這樣i-1這個位置的數(shù)就確定下來了。因?yàn)閯偛盼覀円宦飞蠑?shù)組末尾往前,經(jīng)過的數(shù)字都是逆序的,我們現(xiàn)在要做的就是反轉(zhuǎn)這些數(shù),這樣就得到了next permutation.

public class Solution {
    public void nextPermutation(int[] nums) {
        if(nums.length==0) return;
        int i=nums.length-1;
        for(;i>0;i--){
            if(nums[i]>nums[i-1]) break;
            
        }
        if(i==0) {
            reverse(nums,0,nums.length-1);
            return;
        }
        int first=i-1; //the first num need to be swapped.
        int nextbig=nums[i];
        int nextbig_index=i;
        for(int j=i+1;j=nums[j]&&nums[j]>nums[first]){
                nextbig=nums[j];
                nextbig_index=j;
            }
        }
        swap(nums,first,nextbig_index);
        reverse(nums,first+1,nums.length-1);
    }
    private void reverse(int[] nums, int start,int end){
        while(start

Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

"123"
"132"
"213"
"231"
"312"
"321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

1.解題思路

這個題目其實(shí)涉及到一些數(shù)學(xué)規(guī)律,我們仔細(xì)看例子會發(fā)現(xiàn)其實(shí)以每個數(shù)字開頭的序列都會有(n-1)!個,所以我們只要用k/(n-1)!就可以得到第一個數(shù)字,在求出第一個數(shù)字后,我們只要將k%(n-1)!,就可以繼續(xù)循環(huán)求第二個數(shù)。
需要注意的地方有:
1)代碼里我們是把數(shù)字放入了一個List中,而list的下標(biāo)是以0開始的,所以我們首先將k-1。
2)每次求出一個數(shù)字后,要及時的把它從List中刪除掉。
3)采用StringBuilder來構(gòu)造結(jié)果序列。

2.代碼

public class Solution {
    public String getPermutation(int n, int k) {
        List nums=new ArrayList();
        int factorial=1;
        int i=1;
        while(i<=n){
            factorial*=i;
            nums.add(i);
            i++;
        }
        k--; 
        int j=n;
        StringBuilder sb=new StringBuilder();
        while(nums.size()>0){
            factorial=factorial/j;
            int index=k/factorial;
            sb.append(nums.get(index));
            nums.remove(index);
            j--;
            k=k%factorial;
        }
        
        return sb.toString();
    }
}
  

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/69790.html

相關(guān)文章

  • [LeetCode] 267. Palindrome Permutation II

    Problem Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. Example Given s = aabb, return [abba,baa...

    huashiou 評論0 收藏0
  • [LintCode] Next Permutation II [Next Permutation]

    摘要:從末位向前遍歷,假設(shè)循環(huán)開始全是倒序排列,如當(dāng)?shù)谝淮纬霈F(xiàn)正序的時候,如的和此時從數(shù)列末尾向前循環(huán)到,找到第一個比大的交換這兩個數(shù),變成倒置第位到末位的數(shù)為正序排列這里的是完全倒置的排列,如,即上面循環(huán)的情況完全沒有出現(xiàn), Problem Implement next permutation, which rearranges numbers into the lexicographic...

    mikasa 評論0 收藏0
  • leetcode47 Permutations II

    摘要:當(dāng)前的值如果已經(jīng)被使用過,則繼續(xù)判斷下一個數(shù)值。則當(dāng)?shù)谝粋€被添加進(jìn)結(jié)果集時,可以繼續(xù)使用第二個作為元素添加進(jìn)結(jié)果集從而生成。假設(shè)將表示為那么結(jié)果集中會確保永遠(yuǎn)在數(shù)值的前面,從而避免了和的重復(fù)情況出現(xiàn)。 題目要求 Given a collection of numbers that might contain duplicates, return all possible unique ...

    taoszu 評論0 收藏0
  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖恪K栽谶@里做個索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。 順序整理 1~50 1...

    leo108 評論0 收藏0
  • [Leetcode] Next Permutation 下一個排列

    摘要:因?yàn)樵黾痈呶粫砀蟮脑鲆?。所以對于一個長為的序列,我們增加第位的前提是,前位已經(jīng)達(dá)到了最大排列方法。因?yàn)槭钦蚁乱粋€數(shù),所以我們要找一個比小卻盡可能大的數(shù),所以找到。把換到的位置后,后三位仍然是個降序的排列。 Next Permutation Implement next permutation, which rearranges numbers into the lexicogr...

    young.li 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<