481. Magical String Medium
1/**
2 * [481] Magical String
3 *
4 * A magical string s consists of only '1' and '2' and obeys the following rules:
5 *
6 * The string s is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string s itself.
7 *
8 * The first few elements of s is 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 is s itself.
9 * Given an integer n, return the number of 1's in the first n number in the magical string s.
10 *
11 * Example 1:
12 *
13 * Input: n = 6
14 * Output: 3
15 * Explanation: The first 6 elements of magical string s is "122112" and it contains three 1's, so return 3.
16 *
17 * Example 2:
18 *
19 * Input: n = 1
20 * Output: 1
21 *
22 *
23 * Constraints:
24 *
25 * 1 <= n <= 10^5
26 *
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/magical-string/
31// discuss: https://leetcode.com/problems/magical-string/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36 pub fn magical_string(n: i32) -> i32 {
37 0
38 }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_481() {
49 }
50}
51
Back
© 2025 bowen.ge All Rights Reserved.