481 Magical String 魔力字符串

详见:https://leetcode.com/problems/magical-string/description/

C++:

方法一:

class Solution {
public:
    int magicalString(int n) {
        string s="122";
        int i=2;
        while(s.size()<n)
        {
            s+=string(s[i++]-'0',s.back()=='1'?'2':'1');
        }
        return count(s.begin(),s.begin()+n,'1');
    }
};

 方法二:

class Solution {
public:
    int magicalString(int n) {
        string s="122";
        int i=2;
        while(s.size()<n)
        {
            s+=string(s[i++]-'0',s.back()^3);
        }
        return count(s.begin(),s.begin()+n,'1');
    }
};

 参考:http://www.cnblogs.com/grandyang/p/6286540.html

https://blog.csdn.net/kakitgogogo/article/details/54864975

posted on 2018-04-21 21:09  lina2014  阅读(105)  评论(0编辑  收藏  举报

导航