神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则:

字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身。

字符串 S 的前几个元素如下:S = “1221121221221121122 ......”

如果我们将 S 中连续的 1 和 2 进行分组,它将变成:

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

并且每个组中 '1' 或 '2' 的出现次数分别是:

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

你可以看到上面的出现次数就是 S 本身。

给定一个整数 N 作为输入,返回神奇字符串 S 中前 N 个数字中的 '1' 的数目。

注意:N 不会超过 100,000。

示例:

输入:6
输出:3
解释:神奇字符串 S 的前 6 个元素是 “12211”,它包含三个 1,因此返回 3。
class Solution {
public int magicalString(int n) {
if (n <= 3)
return n > 1 ? 1 : n; int c = 2;
int idx = 2;
int k = 3;
int result = 1;
short[] nums = new short[n + 1]; nums[0] = (short)1;
nums[1] = (short)2;
nums[2] = (short)2; while (k < n) {
c = c == 2 ? 1 : 2;
if (nums[idx] == 1) {
nums[k++] = (short)c;
if (c == 1)
result++; } else {
nums[k++] = (short)c;
nums[k++] = (short)c; if (c == 1) {
result += (k <= n ? 2 : 1);
}
}
idx++;
} return result;
}
}

Q481 神奇字符串的更多相关文章

  1. [LeetCode] Magical String 神奇字符串

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

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

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

  3. Leetcode 481.神奇字符串

    神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下 ...

  4. Java实现 LeetCode 481 神奇字符串

    481. 神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几 ...

  5. hihocoder 神奇字符串

    思路: 暴力,模拟. 实现: #include <iostream> #include <algorithm> #include <cstdio> #include ...

  6. [题解]hihoCoder挑战赛18——题目1 神奇字符串

    题目地址:http://hihocoder.com/problemset/problem/1264 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 我们说两个字符串是非常 ...

  7. Websocket协议的学习、调研和实现

    本文章同时发在 cpper.info. 1. websocket是什么 Websocket是html5提出的一个协议规范,参考rfc6455. websocket约定了一个通信的规范,通过一个握手的机 ...

  8. [网页设计]Ajax、Comet与Websocket--转

    从http协议说起 1996年IETF  HTTP工作组发布了HTTP协议的1.0版本 ,到现在普遍使用的版本1.1,HTTP协议经历了17 年的发展.这种分布式.无状态.基于TCP的请求/响应式.在 ...

  9. Ajax、Comet与Websocket

    从 http 协议说起 1996年IETF  HTTP工作组发布了HTTP协议的1.0版本 ,到现在普遍使用的版本1.1,HTTP协议经历了17 年的发展.这种分布式.无状态.基于TCP的请求/响应式 ...

随机推荐

  1. hibernate的hibernate.cfg.properties

    1.hibernate.cfg.properties  配置文件要放在工程目录src下,编译的时候会自动放在/bin目录下 ,所以Configuration configuration=new Con ...

  2. 1118 Lining Up

    题目链接: http://poj.org/problem?id=1118 题意: 给定n个点, 求在同一直线上的点最多的直线上点的数目. 解法: 简单题目, 规模比较小,  暴力搜索. #includ ...

  3. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 12: ordinal not in range(128)问题解决

    今天在验证字符串是否包含的时候报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 12: ordinal n ...

  4. 启动samba服务--ubuntu 14.04

    1. 修改配置文件 /etc/samba/smb.conf文件末尾添加 [homes] comment = Home Directories browseable = yes read only = ...

  5. ADF文件数据结构解析和ADF文件读写

    包括位姿和特征点位置和描述信息. What does an Area Description File (ADF) looks like? 4down votefavorite 2 I'm start ...

  6. Tomcat配置文件与启动顺序

    三个配置应用的位置: 1.conf目录下的server.xml文件:此方式为Eclipse默认配置方法,同时也是三种方式中优先级最高的. <?xml version="1.0" ...

  7. [修正] Firemonkey Windows 控件有虚线残影问题

    说明:在 Wndows 显示时,有时控件左方会显示一条虚线 适用:Firemonkey Windows (Berlin 或更高版) 修正方法: 请将源码 FMX.Canvas.D2D.pas 复制到自 ...

  8. 利用 StartLoadingStatus 和 FinishLoadingStatus 读取数据特别是大数据时增加渐隐渐显等待特效 - Ehlib学习(三)

    代码很简单: DBGrideh.StartLoadingStatus(' Loading ... '); Sleep(500); DBGrideh.FinishLoadingStatus; 做下变动: ...

  9. ArcGIS应用——使用Python为图斑连续编号及扩展应用

    为图斑连续编号 在GIS应用中,为图斑连续编号(编号递增)是一项常见的需求,利用ArcGIS,可以方便的实现. Python脚本如下: rec=0 def autoIncrement(): globa ...

  10. Spring Boot - Restful API

    基本用法 @GetMapping与@PostMapping不指定参数时就是指直接使用到controller一级的url就行 @GetMapping与@PathVariable对应,前者{}中的字符串和 ...