【LeetCode】481. Magical String 解题报告(Python)
【LeetCode】481. Magical String 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/magical-string/description/
题目描述:
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.
题目大意
有一个遵守一个规则的字符串S,求这个S前n位共有多少个1.这个规则是这样的:把相同的数字合并成一个组,然后统计这个组的长度,我们发现这个组的长度组成的新的字符串和S是完全相等的。
解题方法
一定要手动推算一下才明白这个逻辑。因为只有1和2两个数字,组的长度和S是相同的,所以是可以确定S的每个位置的。
S的开头一定是1,2,2;合并就是1个1、2个2,所以到了第三个数字,这个数字是2,所以一定是2个1,故S的下两位是1,即S是1,2,2,1,1;到了S的第四个数字是1,所以下面是1个2;即S是1,2,2,1,1,2……
通过上面的规律看出,需要一个列表一个指针,指针每次向后走一个位置,S增加的是指针指向的这个位置的数字 个 (3-列表最后一个数字)。
注意,因为列表每次可能会增加一个或者两个数字,我们需要的是前n个数字,所以最后统计1的时候,需要对S进行切片。
代码如下:
class Solution(object):
def magicalString(self, n):
"""
:type n: int
:rtype: int
"""
S = [1, 2, 2]
idx = 2
while len(S) < n:
S += [3 - S[-1]] * S[idx]
idx += 1
return S[:n].count(1)
参考资料:
https://leetcode.com/problems/magical-string/discuss/96472/Short-Python-using-queue
日期
2018 年 9 月 7 日 ———— 中午不睡,下午崩溃
【LeetCode】481. Magical String 解题报告(Python)的更多相关文章
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- EXCEL-名称管理器
1.怎么用? 两种方法 参考:https://jingyan.baidu.com/article/a378c960a26f26b3282830a6.html 2.有什么功能? (1)直接引用或者函数直 ...
- PHP socket Workerman实用的php框架
PHP socket Workerman是一款开源高性能异步PHP socket即时通讯框架. 非常好用的一款框架,可以支持在线聊天,长连接等 用法 官方 https://www.workerman. ...
- mysql 实现某年单季度内的品牌TOPn销量在此年此单季度内销量占比
数据表: 结果表: mysql语句:
- 强化学习实战 | 自定义Gym环境
新手的第一个强化学习示例一般都从Open Gym开始.在这些示例中,我们不断地向环境施加动作,并得到观测和奖励,这也是Gym Env的基本用法: state, reward, done, info = ...
- 文件和目录之间建立链接 (ln)
- Codeforces Round #754 (Div. 2) C. Dominant Character
题目:Problem - C - Codeforces 如代码,一共有七种情况,注意不要漏掉 "accabba" , "abbacca" 两种情况: 使用 ...
- Python初探——sklearn库中数据预处理函数fit_transform()和transform()的区别
敲<Python机器学习及实践>上的code的时候,对于数据预处理中涉及到的fit_transform()函数和transform()函数之间的区别很模糊,查阅了很多资料,这里整理一下: ...
- py脚本 获取当前运行服务的相关信息
一.简介 最近在统计系统中都部署了什么服务,但服务器太多,在没有标准化之前进行整理,还是写脚本收集方便一些. 当然还是需要人工去判断整理表格,为后面标准化做准备.脚本是python2.7的,默认的ce ...
- [BUUCTF]PWN——0ctf_2017_babyheap
0ctf_2017_babyheap 附件 步骤: 例行检查,64位程序,保护全开 本地试运行一下,看看大概的情况,经典的堆题的菜单 main函数 add() edit() delete() show ...
- CTF 自动拼图
忘记在哪个群里面看见有师傅说过这样一句加,百度搜索"CTF拼图脚本,有惊喜". 在做JUSTCTF的题时候,看到一道拼图题.就想着试一试. 先百度搜了,看到了fjh1997师傅的一 ...