成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Gray Code

cocopeak / 2847人閱讀

摘要:參考了的算法,簡(jiǎn)化了一下每個(gè)循環(huán)更新對(duì)應(yīng)最高位是第位,就增加個(gè)數(shù)為倒序循環(huán)次,會(huì)鏡像提取上一次循環(huán)產(chǎn)生的結(jié)果鏡像鏡像鏡像

Problem

The gray code is a binary numeral system where two successive values differ in only one bit.

Given a non-negative integer n representing the total number of bits in the code, find the sequence of gray code. A gray code sequence must begin with 0 and with cover all 2n integers.
http://mathworld.wolfram.com/...

Example

Given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2
Solution

其實(shí)就是找規(guī)律,0-01-0132-01326754這樣,每個(gè)循環(huán)(i):

增加當(dāng)前res.size()個(gè)新數(shù);

新的循環(huán)先在2進(jìn)制的第(i+1)位加1,同時(shí)與之前res所存的數(shù)字倒序相加產(chǎn)生新數(shù);

存入新數(shù),進(jìn)入下一個(gè)循環(huán)后更新size。

參考了codesolutiony的算法,簡(jiǎn)化了一下:

public class Solution {
    public ArrayList grayCode(int n) {
        ArrayList res = new ArrayList();
        res.add(0);
        for (int i = 0; i < n; i++) {
            //每個(gè)循環(huán)更新size: 1, 3, 7...
            int size = res.size() - 1;
            //i對(duì)應(yīng)最高位是第i+1位,res就增加2^(i+1)個(gè)數(shù):(1<= 0; j--) {
                res.add((1<

Recursion

public class Solution {
    public List grayCode(int n) {
        List res = new ArrayList();
        if (n == 0) {
            res.add(0);
            return res;
        }
        res = grayCode(n-1);
        for (int i = res.size()-1; i >= 0; i--) {
            int num = res.get(i);
            res.add(num+(1<<(n-1)));
        }
        return res;
    }
}

Using XOR

public class Solution {
    public List grayCode(int n) {
        List res = new ArrayList();
        for(int i = 0; i < Math.pow(2,n); i++) res.add(i >> 1 ^ i);
        return res;
    }
}

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

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

相關(guān)文章

  • [LeetCode/LintCode] First Bad Version

    摘要:分析最后一次循環(huán)的時(shí)刻當(dāng)與相差小于時(shí),總是那么如果是,下一次直接跳出循環(huán),返回當(dāng)與相差大于時(shí),是,變成,如果是,循環(huán)結(jié)束的條件將是循環(huán)結(jié)束,返回 Problem The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case,...

    lowett 評(píng)論0 收藏0
  • [LintCode/LeetCode] Word Break

    Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...

    dunizb 評(píng)論0 收藏0
  • [LintCode] Coins in a Line I & Coins in a Line

    摘要:第一個(gè)游戲者永遠(yuǎn)拿不到第枚硬幣,所以在硬幣總數(shù)不能被整除的情況下,都可以贏。做法,設(shè)為第一個(gè)游戲者從第枚硬幣到能獲得硬幣價(jià)值的最大值。主要參考這篇文章的解釋 Coins in a Line I Solution 第一個(gè)游戲者永遠(yuǎn)拿不到第3n枚硬幣,所以在硬幣總數(shù)不能被3整除的情況下,都可以贏。 public class Solution { public boolean fi...

    xzavier 評(píng)論0 收藏0
  • [Leetcode] Gray Code 格雷碼

    摘要:找規(guī)律復(fù)雜度時(shí)間空間思路仔細(xì)觀察格雷碼當(dāng)時(shí)當(dāng)時(shí)當(dāng)時(shí)可以發(fā)現(xiàn),的格雷碼,就是的格雷碼,再加上它們的逆序前面多一個(gè)。 Grey Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n re...

    Code4App 評(píng)論0 收藏0
  • [LintCode] Nuts & Bolts Problem

    Problem Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not ...

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

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

0條評(píng)論

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