Skip to content

Commit 741b222

Browse files
refactor 481
1 parent 16f3398 commit 741b222

File tree

1 file changed

+39
-35
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+39
-35
lines changed

src/main/java/com/fishercoder/solutions/_481.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,49 @@
3030
*/
3131
public class _481 {
3232

33-
/**credit: https://discuss.leetcode.com/topic/74917/simple-java-solution-using-one-array-and-two-pointers
34-
* Algorithm:
35-
36-
1. Create an int array a and initialize the first 3 elements with 1, 2, 2.
37-
2. Create two pointers head and tail. head points to the number which will be used to generate new numbers.
38-
tail points to the next empty position to put the new number. Then keep generating new numbers until tail >= n.
39-
3. Need to create the array 1 element more than n to avoid overflow because the last round head might points to a number 2.
40-
4. A trick to flip number back and forth between 1 and 2: num = num ^ 3*/
41-
public int magicalString(int n) {
42-
if (n <= 0) {
43-
return 0;
44-
}
45-
if (n <= 3) {
46-
return 1;
47-
}
33+
public static class Solution1 {
34+
/**
35+
* credit: https://discuss.leetcode.com/topic/74917/simple-java-solution-using-one-array-and-two-pointers
36+
* Algorithm:
37+
*
38+
* 1. Create an int array a and initialize the first 3 elements with 1, 2, 2.
39+
* 2. Create two pointers head and tail. head points to the number which will be used to generate new numbers.
40+
* tail points to the next empty position to put the new number. Then keep generating new numbers until tail >= n.
41+
* 3. Need to create the array 1 element more than n to avoid overflow because the last round head might points to a number 2.
42+
* 4. A trick to flip number back and forth between 1 and 2: num = num ^ 3
43+
*/
44+
public int magicalString(int n) {
45+
if (n <= 0) {
46+
return 0;
47+
}
48+
if (n <= 3) {
49+
return 1;
50+
}
4851

49-
int[] a = new int[n + 1];
50-
a[0] = 1;
51-
a[1] = 2;
52-
a[2] = 2;
53-
54-
int head = 2;
55-
int tail = 3;
56-
int num = 1;
57-
int result = 1;
58-
59-
while (tail < n) {
60-
for (int i = 0; i < a[head]; i++) {
61-
a[tail] = num;
62-
if (num == 1 && tail < n) {
63-
result++;
52+
int[] a = new int[n + 1];
53+
a[0] = 1;
54+
a[1] = 2;
55+
a[2] = 2;
56+
57+
int head = 2;
58+
int tail = 3;
59+
int num = 1;
60+
int result = 1;
61+
62+
while (tail < n) {
63+
for (int i = 0; i < a[head]; i++) {
64+
a[tail] = num;
65+
if (num == 1 && tail < n) {
66+
result++;
67+
}
68+
tail++;
6469
}
65-
tail++;
70+
num = num ^ 3;
71+
head++;
6672
}
67-
num = num ^ 3;
68-
head++;
69-
}
7073

71-
return result;
74+
return result;
75+
}
7276
}
7377

7478
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy