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 public class Solution {
 2     public int magicalString(int n) {
 3         if (n <= 0) return 0;
 4         if (n < 4) return 1;
 5
 6         int[] magical = new int[n];
 7         magical[0] = 1; magical[1] = magical[2] = 2;
 8
 9         int result = 1;
10         int start = 2, end = 2;
11         while (start < n) {
12             if (magical[start] == 1) {
13                 if (end + 1 < n)
14                     magical[end + 1] = magical[end] == 1 ? 2 : 1;
15
16                 end++;
17                 result += 1;
18             } else {
19                 if (end + 2 < n)
20                     magical[end + 2] = magical[end] == 1 ? 2 : 1;
21                 if (end + 1 < n)
22                     magical[end + 1] = magical[end] == 1 ? 2 : 1;
23                 end += 2;
24             }
25             start++;
26         }
27         return result;
28     }
29 }
时间: 2025-02-01 08:59:43

Magical String的相关文章

LeetCode &quot;Magical String&quot;

Fun one. A matter of string generation by given rules. I know it can be much shorter.. but i'm lazy to do that. class Solution { public: int magicalString(int n) { if(!n) return 0; if(n < 2) return 1; int ret = 1; string s = "1"; bool isOne =

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

GYM 101061 I. Playing with strings(有待更新)

I. Playing with strings time limit per test 2.0 s memory limit per test 64 MB input standard input output standard output Taboush is a 10 year-old school boy. On his birthday, his parents got him a new Alphabet blocks game. Alphabet blocks game consi

Leetcode刷题第三期Week1——模拟

题目列表来自yxc大佬的AcWing Leetcode提高班第三期 Leetcode 263 Ugly Number 注意:特别地,1是Ugly Number 没什么要注意的,三个循环搞定 class Solution { public: bool isUgly(int num) { if(num <= 0) return false; while(num % 2 == 0) num = num / 2; while(num % 3 == 0) num = num / 3; while(num

hdu4941 Magical Forest

Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci. However, the forest will m

hdu 4941 Magical Forest (map容器)

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 135    Accepted Submission(s): 69 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so