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