A magical string S consists of only '1' and '2' and obeys the following rules:

The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself.

The first few elements of string S is the following: S = "1221121221221121122……"

If we group the consecutive '1's and '2's in S, it will be:

1 22 11 2 1 22 1 22 11 2 11 22 ......

and the occurrences of '1's or '2's in each group are:

1 2 2 1 1 2 1 2 2 1 2 2 ......

You can see that the occurrence sequence above is the S itself.

Given an integer N as input, return the number of '1's in the first N number in the magical string S.

Note: N will not exceed 100,000.

Example 1:

Input: 6
Output: 3
Explanation: The first 6 elements of magical string S is "12211" and it contains three 1's, so return 3.

这道题介绍了一种神奇字符串,只由1和2组成,通过计数1组和2组的个数,又能生成相同的字符串。而让我们求前n个数字中1的个数说白了其实就是让我们按规律生成这个神奇字符串,只有生成了字符串的前n个字符,才能统计出1的个数。其实这道题的难点就是在于找到规律来生成字符串,这里我们就直接说规律了,因为博主也没有自己找到,都是看了网上大神们的解法。根据第三个数字2开始往后生成数字,此时生成两个1,然后根据第四个数字1,生成一个2,再根据第五个数字1,生成一个1,以此类推,生成的数字1或2可能通过异或3来交替生成,在生成的过程中同时统计1的个数即可,参见代码如下:

解法一:

class Solution {
public:
int magicalString(int n) {
if (n <= ) return ;
if (n <= ) return ;
int res = , head = , tail = , num = ;
vector<int> v{, , };
while (tail < n) {
for (int i = ; i < v[head]; ++i) {
v.push_back(num);
if (num == && tail < n) ++res;
++tail;
}
num ^= ;
++head;
}
return res;
}
};

下面这种解法的思路跟上面一样,但是写法上面大大的简洁了,感觉很叼!

解法二:

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

参考资料:

https://discuss.leetcode.com/topic/74637/short-c

https://discuss.leetcode.com/topic/74917/simple-java-solution-using-one-array-and-two-pointers

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Magical String 神奇字符串的更多相关文章

  1. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  2. [LeetCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. [LeetCode] Reorganize String 重构字符串

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  4. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  5. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  6. 481 Magical String 魔力字符串

    详见:https://leetcode.com/problems/magical-string/description/ C++: 方法一: class Solution { public: int ...

  7. [Swift]LeetCode481. 神奇字符串 | Magical String

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  8. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  9. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

随机推荐

  1. javascript动画效果之缓冲动画(修改版)

    在编写多块同时触发运动的时候,发现一个BUG, timer = setInterval(show, 30);本来show是一个自定义函数,当设为timer = setInterval(show(one ...

  2. SQL 课程 连接查询

    今天,我主要是对前面所学习过的子查询进行了复习,然后学习了连接查询join on 的内容. 如: select renyuan.code,name,age ,sex , bumen.bname,bce ...

  3. spring AOP 代理机制、执行过程、四种实现方式及示例详解

    1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...

  4. php 图像处理类

    <?php/** file: image.class.php 类名为Image 图像处理类,可以完成对各种类型的图像进行缩放.加图片水印和剪裁的操作. http://www.lai18.com ...

  5. javascript中函数的call,apply及bind方法

    call 方法调用一个对象的一个方法,以另一个对象替换当前对象.call([thisObj[,arg1[, arg2[,  [,.argN]]]]])参数thisObj可选项.将被用作当前对象的对象. ...

  6. .net core nlog记录日志

    1.通过nuget 查找 下载 NLog.Extensions.Logging 2.配置nlog.config文件 <?xml version="1.0" encoding= ...

  7. 《C++ Primer》之面向对象编程(三)

    继承情况下的类作用域 在继承情况下,派生类的作用域嵌套在基类作用域中.如果不能在派生类作用域中确定名字,就在外围基类作用域中查找该名字的定义.正是这种类作用域的层次嵌套使我们能够直接访问基类的成员,就 ...

  8. Android 调试工具集【转】

    1.TraceView1)功能:用于热点分析和性能优化,分析每个函数占用的CPU时间,调用次数,函数调用关系等 2)方法: a)在程序代码中加入追踪开关 import android.os.Debug ...

  9. 反编译app方法

    如果你没有代码,那么可以反编译该app. 这里将用到2个工具,分别是dex2jar和jd-gui.你可以在这里下载目前为止的最新版本以及示例apk. 我们以工具包里的ContactManager.ap ...

  10. JavaScript高级程序设计:第十七章

    错误处理与调试 一.错误处理 1.try-catch语句: ECMA-262第3版引入了try-catch语句,作为javascript中处理异常的一种标准方式.基本的语法如下: try { //可能 ...