[LeetCode] Magical String 神奇字符串
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 神奇字符串的更多相关文章
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [LeetCode] Rotate String 旋转字符串
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- 481 Magical String 魔力字符串
详见:https://leetcode.com/problems/magical-string/description/ C++: 方法一: class Solution { public: int ...
- [Swift]LeetCode481. 神奇字符串 | Magical String
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
随机推荐
- MVC view视图获取Html.RenderAction方式带来的参数
通过Html.RenderAction这种方式传递的参数,在view视图中获取要使用viewContext上下文来获取:Html.ViewContext.RouteData.Values[" ...
- for计算100以内的偶数和
#include "stdio.h" void main() { ,sum=; ;d++) { ==) { sum=sum+d; } }printf("100以内所有偶数 ...
- 《Windows驱动开发技术详解》之驱动程序调用驱动程序——通过设备指针调用其他驱动程序
本节介绍“手动”构造各个IRP,然后将IRP传递到相应驱动程序的派遣函数里. 获得设备指针 每个内核中的句柄都会和一个内核对象的指针联系起来.ZwCreateFile内核函数可以通过设备名打开设备句柄 ...
- HOSTS文件修改后不起作用的原因
如果是通过记事本修改,其实是没有问题的,如果有问题,网上搜到的是ipconfig /flushdns之类的 如果是批量程序写的,那就要小心了, 一定要ANSI(美标格式的,忘了英文是什么来着) 保存后 ...
- css3-多列显示文字
-------------------------- <style> #demo{ margin: 20px auto; ...
- Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLIC
在mysql5中遇到的问题: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) f ...
- Linux教程之配置权限受限制的SFTP
SFTP 在Linux下是一个很方便很安全的文件传输工具,我常常用它在Linux服务器上替代传统的ftp来传输文件.众所周知SFTP账号是基于SSH账号的,默认情况下访问服务器的权限很大,下面的教程就 ...
- JSP内置对象--request对象 (setCharacterEncoding("GBK"),getParameter(),getParameterValues(),getParameterNames(),getServletPath(),getContextPath()
使用最多,主要用来接收客户端发送而来的请求信息,他是javax.servlet.http.HttpServletRequest接口的实例化对象. public interface HttpServle ...
- Linux下 Unison 实现文件双向同步
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://hx100.blog.51cto.com/44326/612301 一.Uniso ...
- Qt 学习之路 2(84):Repeater
前面的章节我 们介绍过模型视图.这是一种数据和显示相分离的技术,在 Qt 中有着非常重要的地位.在 QtQuick 中,数据和显示的分离同样也是利用这种"模型-视图"技术实现的.对 ...