The title information

Subject address: leetcode-cn.com/problems/fi…

Write a program that outputs a string representation of the numbers 1 through N.

  1. If n is a multiple of 3, print “Fizz”;
  2. If n is a multiple of 5, output “Buzz”;
  3. If n is both a multiple of 3 and 5, output “FizzBuzz”.

Example:

 n = 15, returns: ["1"."2"."Fizz"."4"."Buzz"."Fizz"."Seven"."8"."Fizz"."Buzz"."11"."Fizz"."13"."14"."FizzBuzz"
 ]
Copy the code

Answer key a

The solution to this problem is just to follow the meaning

 public List<String> fizzBuzz(int n) {
     List<String> res = new ArrayList();
     for (int i = 1; i <= n; i++){
         if(i%3= =0 && i%5= =0){
             res.add("FizzBuzz");
         } else if(i%3= =0){
             res.add("Fizz");
         } else if(i%5= =0){
             res.add("Buzz");
         } else{ res.add(String.valueOf(i)); }}return res;
 }
Copy the code

Answer key 2

The solution to this problem is simple, but the problem probably wants to reduce the code’s stickiness and reduce the code’s duplication and coupling, so the official solution also uses an example (assuming there are more than two conditions). Let me write it over here, so there are a couple of cases where we can just do it by hand and then enumerate it, and it’s a lot of code and all of these are if examples

 /* * assuming Three conditions * I = 3, Three * I = 5, Five * I = 7, Seven * then there are eight branches */
 if(i%3= =0 && i%5= =0 && i%7= =0){
     res.add("ThreeFiveSeven");
 } else if(i%3= =0 && i%5= =0){
     res.add("ThreeFive");
 } else if(i%3= =0 && i%7= =0){
     res.add("ThreeSeven");
 } else if(i%5= =0 && i%7= =0){
     res.add("FiveSeven");
 } else if(i%3= =0){
     res.add("Three");
 } else if(i%5= =0){
     res.add("Five");
 } else if(i%7= =0){
     res.add(Seven);
 } else {
     res.add(String.valueOf(i));
 }
Copy the code

If it’s a little bit more, it’s too many if branches. It’s actually very simple and we usually write code even if we don’t pay much attention to it and write it casually, we don’t write it like this. Here’s an example:

The sample

 /* * * STR begins with 'a' and executes method A * STR ends with 'b' and executes method b */
 if(str.charAt(0) = ='a'){
     a();
 }
 if(str.charAt(length-1) = ='b'){
     b();
 }
Copy the code

Instead of having two single ifs like this:

 if(str.charAt(0) = ='a' && str.charAt(length-1) = ='b'){
     a();
     b();
 } else if(str.charAt(0) = ='a'){
     a();
 } else if(str.charAt(length-1) = ='b'){
     b();
 }
Copy the code

So this is the same thing

 public List<String> fizzBuzz(int n) {
     List<String> res = new ArrayList();
     for (int i = 1; i <= n; i++){
         String str = "";
         if (i%3= =0) {
             str += "Fizz";
         }
         if (i%5= =0) {
             str += "Buzz";
         }
         if (str.equals("")) {
             str += String.valueOf(i);
         }
         res.add(str);
     }
     return res;
 }
Copy the code

Answer key three

As in problem number two, after we use the single if, we don’t do manual composition. The number of branches will decrease and the structure will be clearer (avoid nesting or multiple branches). The multiplier will decrease but the condition will increase if. So if you have a lot of conditions you can put a lot of ifs in a container and turn that into a loop +if

 Map<Integer,String> map = new HashMap();
 map.put(3."Fizz");
 map.put(5."Buzz");
Copy the code
 for (Integer key : map.keySet()) {
     if (num % key == 0) { str += map.get(key); }}Copy the code

So the code looks like this:

public List<String> fizzBuzz(int n) {
	Map<Integer,String> map = new HashMap();
	map.put(3."Fizz");
	map.put(5."Buzz");
	List<String> res = new ArrayList();
	for (int i = 1; i <= n; i++){
		String str = "";
		for (Integer key : map.keySet()) {
			if (i % key == 0) { str += map.get(key); }}if (str.equals("")) {
			str += String.valueOf(i);
		}
		res.add(str);
	}
	return res;
}
Copy the code

conclusion

The problem itself is simple, but the main problem is how to deal with the generic, just like in the past we learned javaEE to rewrite the generic servlet, n method names are not determined by generics, here is also the same if can not write n if, there are some improvements. So for example in problem solving 2 if both conditions are satisfied so both methods are executed, do they need to be in order? If the actual logic is required, you can manually adjust the single-if order for solution 2, but not for solution 3 because the keySet() method of hashMap is out of order, but you can use TreeMap or LinkMap.TreeMap is in ascending order by key, while LinkMap is in ascending order by put