亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

484. Find Permutation

OBKoro1 / 3428人閱讀

摘要:題目鏈接,用一個(gè)來指向現(xiàn)在到的數(shù),每次碰到,就的數(shù)量,最后碰到把到放入結(jié)果,更新。還有一種寫法是先賦值,之后檢查,再。

484. Find Permutation

題目鏈接:
https://leetcode.com/problems...

greedy,用一個(gè)point:number來指向現(xiàn)在到的數(shù),每次碰到"D",就count"D"的數(shù)量,最后碰到I把number + count到number放入結(jié)果,更新count = 0。

public class Solution {
    public int[] findPermutation(String s) {
        int n = s.length();
        // the value in result
        int number = 1;
        int count = 0;
        int[] result = new int[n + 1];
        for(int i = 0; i <= n; i++) {
            if(i == n || s.charAt(i) == "I") {
                for(int j = i; j >= i - count; j--) result[j] = number++;
                count = 0;
            }
            else count++;
        }
        
        return result;
    }
}

還有一種寫法是先賦值,之后檢查"D",再reverse。

public class Solution {
    public int[] findPermutation(String s) {
        int n = s.length();
        
        int[] result = new int[n + 1];
        // assign value
        for(int i = 0; i <= n; i++) result[i] = i + 1;
        // reverse for D
        for(int i = 0; i < n; i++) {
            if(s.charAt(i) == "D") {
                int j = i + 1;
                while(j < n && s.charAt(j) == "D") j++;
                reverse(result, i, j);
                i = j;
            }
        }
        return result;
    }
    
    private void reverse(int[] result, int i, int j) {
        while(i < j) {
            int temp = result[i];
            result[i] = result[j];
            result[j] = temp;
            i++; j--;
        }
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66664.html

相關(guān)文章

  • Find and Replace Pattern

    1. 題目 You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replaci...

    ityouknow 評(píng)論0 收藏0
  • [LintCode] Permutation Index I & Permutation I

    摘要:我覺得雖然在里分類是,但其實(shí)是一道難題。思路如下搞一個(gè)哈希表,存儲(chǔ)數(shù)組中每一位的后面小于它的數(shù)的個(gè)數(shù)。與上一題的不同之處時(shí)會(huì)有重復(fù)的數(shù)。當(dāng)然,每個(gè)重復(fù)數(shù)的都要階乘,例如有個(gè),個(gè),就是。是所有排列的次數(shù)和,返回下一次。 Permutation Index Problem Given a permutation which contains no repeated number, find...

    lucas 評(píng)論0 收藏0
  • [LintCode] Previous Permutation

    Problem Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the previous per...

    Pines_Cheng 評(píng)論0 收藏0
  • [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 評(píng)論0 收藏0
  • 【LC總結(jié)】回溯 (Subsets I II/Permutation I II/Combinatio

    摘要:不同數(shù)包含重復(fù)數(shù)為的時(shí)候,表示在外層的循環(huán)正在被使用,所以當(dāng)前循環(huán)遇到為一定要跳過。對(duì)當(dāng)前循環(huán)要添加的數(shù)組,在添加當(dāng)前元素后進(jìn)行遞歸,遞歸之后要將當(dāng)前元素的使用標(biāo)記改為,表示已經(jīng)使用和遞歸完畢,然后再將這個(gè)元素從的末位刪除。 Subsets Problem Given a set of distinct integers, nums, return all possible subse...

    tuomao 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<