Write a program, it outputs a number from 1 to n strings

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]

Conventional thinking

Title very simple multiple output n is 3 Fizz, n is a multiple of output 5 Buzz, n while the output 3 or 5 multiples FizzBuzz. That is, when the determination condition, should first determine whether n is a multiple of 15, followed by determining whether a multiple of 3 or 5 to determine what output. code show as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public List<String> fizzBuzz(int n) {
List<String> list = new ArrayList<>(n);
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) {
list.add("FizzBuzz");
} else if (i % 3 == 0) {
list.add("Fizz");
} else if (i % 5 == 0) {
list.add("Buzz");
} else {
list.add(i + "");
}
}
return list;
}
}

 

Here to say, as if LeetCode use the List is not required when the lead pack in the code, I tried no matter did to the first line import java.util.*;no problem.

Below is the time and memory consumption:

Runtime: 1 ms
Memory Usage: 37.2 MB

Special ideas

Top is by %carried out modulo operation to achieve, I saw some people think the comments section:

Generally, for a CPU operand modulo relatively inefficient, it can be avoided if a large amount of remainder operands, can improve the performance of the program.

So there will be no use %of writing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Solution {
public List<String> fizzBuzz(int n) {
List<String> ret = new ArrayList<String>(n);
for(int i=1,fizz=0,buzz=0;i<=n ;i++){
fizz++;
buzz++;
if(fizz==3 && buzz==5){
ret.add("FizzBuzz");
fizz=0;
buzz=0;
}else if(fizz==3){
ret.add("Fizz");
fizz=0;
}else if(buzz==5){
ret.add("Buzz");
buzz=0;
}else {
ret.add (String.valueOf (i));
}
} Return right; } }



 

Below is the time and memory consumption:

Runtime: 1 ms
Memory Usage: 37.3 MB

The program requires two variables in repeated self-energizing and re-assigned to 0, you can use the following programs to reduce these operations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
public List<String> fizzBuzz(int n) {

List<String> result = new ArrayList<>();

if(n < 1) return result;

for(int i = 1, fizz = 3, buzz = 5; i <= n; i++) {

String addVal = null;

if(i == fizz && i == buzz) {
addVal = "FizzBuzz";
fizz += 3;
buzz += 5;
} else if(i == fizz) {
addVal = "Fizz";
fizz += 3;
} else if(i == buzz) {
addVal ="Buzz";
buzz += 5;
} else
addVal = String.valueOf(i);

result.add(addVal);
}

return result;
}
}

 

下面是时间与内存的消耗:

Runtime: 1 ms
Memory Usage: 37.1 MB

补充:i+""String.valueOf(i)

which is better between
list.add( “” + i );
and
addStr = String.valueOf(i); list.add(addStr)?

我认为后者的写法更好,因为i+""实际上会new一个StringBuilder去拼接i"",然后再调用toString()来得到字符串。而String.valueOf(i)在底层是调用了Integer.toString(i)来得到字符串。

Guess you like

Origin www.cnblogs.com/yixiaogo/p/11204470.html