File tree 8 files changed +204
-32
lines changed
0417.Pacific Atlantic Water Flow
8 files changed +204
-32
lines changed Original file line number Diff line number Diff line change 38
38
]
39
39
</pre >
40
40
41
-
42
41
## 解法
43
42
44
43
<!-- 这里可写通用的实现逻辑 -->
50
49
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
50
52
51
``` python
53
-
52
+ class Solution :
53
+ def fizzBuzz (self , n : int ) -> List[str ]:
54
+ ans = []
55
+ for i in range (1 , n + 1 ):
56
+ if i % 15 == 0 :
57
+ ans.append(' FizzBuzz' )
58
+ elif i % 3 == 0 :
59
+ ans.append(' Fizz' )
60
+ elif i % 5 == 0 :
61
+ ans.append(' Buzz' )
62
+ else :
63
+ ans.append(str (i))
64
+ return ans
54
65
```
55
66
56
67
### ** Java**
57
68
58
69
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
70
60
71
``` java
72
+ class Solution {
73
+ public List<String > fizzBuzz (int n ) {
74
+ List<String > ans = new ArrayList<> ();
75
+ for (int i = 1 ; i <= n; ++ i) {
76
+ String s = " " ;
77
+ if (i % 3 == 0 ) {
78
+ s += " Fizz" ;
79
+ }
80
+ if (i % 5 == 0 ) {
81
+ s += " Buzz" ;
82
+ }
83
+ if (s. length() == 0 ) {
84
+ s += i;
85
+ }
86
+ ans. add(s);
87
+ }
88
+ return ans;
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### ** C++**
94
+
95
+ ``` cpp
96
+ class Solution {
97
+ public:
98
+ vector<string > fizzBuzz(int n) {
99
+ vector<string > ans;
100
+ for (int i = 1; i <= n; ++i)
101
+ {
102
+ string s = "";
103
+ if (i % 3 == 0) s += "Fizz";
104
+ if (i % 5 == 0) s += "Buzz";
105
+ if (s.size() == 0) s = to_string(i);
106
+ ans.push_back(s);
107
+ }
108
+ return ans;
109
+ }
110
+ };
111
+ ```
61
112
113
+ ### **Go**
114
+
115
+ ```go
116
+ func fizzBuzz(n int) []string {
117
+ var ans []string
118
+ for i := 1; i <= n; i++ {
119
+ s := &strings.Builder{}
120
+ if i%3 == 0 {
121
+ s.WriteString("Fizz")
122
+ }
123
+ if i%5 == 0 {
124
+ s.WriteString("Buzz")
125
+ }
126
+ if s.Len() == 0 {
127
+ s.WriteString(strconv.Itoa(i))
128
+ }
129
+ ans = append(ans, s.String())
130
+ }
131
+ return ans
132
+ }
62
133
```
63
134
64
135
### ** ...**
Original file line number Diff line number Diff line change 31
31
<li><code>1 <= n <= 10<sup>4</sup></code></li>
32
32
</ul >
33
33
34
-
35
34
## Solutions
36
35
37
36
<!-- tabs:start -->
38
37
39
38
### ** Python3**
40
39
41
40
``` python
42
-
41
+ class Solution :
42
+ def fizzBuzz (self , n : int ) -> List[str ]:
43
+ ans = []
44
+ for i in range (1 , n + 1 ):
45
+ if i % 15 == 0 :
46
+ ans.append(' FizzBuzz' )
47
+ elif i % 3 == 0 :
48
+ ans.append(' Fizz' )
49
+ elif i % 5 == 0 :
50
+ ans.append(' Buzz' )
51
+ else :
52
+ ans.append(str (i))
53
+ return ans
43
54
```
44
55
45
56
### ** Java**
46
57
47
58
``` java
59
+ class Solution {
60
+ public List<String > fizzBuzz (int n ) {
61
+ List<String > ans = new ArrayList<> ();
62
+ for (int i = 1 ; i <= n; ++ i) {
63
+ String s = " " ;
64
+ if (i % 3 == 0 ) {
65
+ s += " Fizz" ;
66
+ }
67
+ if (i % 5 == 0 ) {
68
+ s += " Buzz" ;
69
+ }
70
+ if (s. length() == 0 ) {
71
+ s += i;
72
+ }
73
+ ans. add(s);
74
+ }
75
+ return ans;
76
+ }
77
+ }
78
+ ```
79
+
80
+ ### ** C++**
81
+
82
+ ``` cpp
83
+ class Solution {
84
+ public:
85
+ vector<string > fizzBuzz(int n) {
86
+ vector<string > ans;
87
+ for (int i = 1; i <= n; ++i)
88
+ {
89
+ string s = "";
90
+ if (i % 3 == 0) s += "Fizz";
91
+ if (i % 5 == 0) s += "Buzz";
92
+ if (s.size() == 0) s = to_string(i);
93
+ ans.push_back(s);
94
+ }
95
+ return ans;
96
+ }
97
+ };
98
+ ```
48
99
100
+ ### **Go**
101
+
102
+ ```go
103
+ func fizzBuzz(n int) []string {
104
+ var ans []string
105
+ for i := 1; i <= n; i++ {
106
+ s := &strings.Builder{}
107
+ if i%3 == 0 {
108
+ s.WriteString("Fizz")
109
+ }
110
+ if i%5 == 0 {
111
+ s.WriteString("Buzz")
112
+ }
113
+ if s.Len() == 0 {
114
+ s.WriteString(strconv.Itoa(i))
115
+ }
116
+ ans = append(ans, s.String())
117
+ }
118
+ return ans
119
+ }
49
120
```
50
121
51
122
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
vector<string> fizzBuzz (int n) {
4
- vector<string> Ret (n);
5
-
6
- for (int i=1 ; i<=n; i++){
7
-
8
- Ret[i-1 ] = (i%15 == 0 ) ? " FizzBuzz" : (i%3 == 0 ) ? " Fizz" : (i%5 == 0 ) ? " Buzz" : to_string (i);
9
-
4
+ vector<string> ans;
5
+ for (int i = 1 ; i <= n; ++i)
6
+ {
7
+ string s = " " ;
8
+ if (i % 3 == 0 ) s += " Fizz" ;
9
+ if (i % 5 == 0 ) s += " Buzz" ;
10
+ if (s.size () == 0 ) s = to_string (i);
11
+ ans.push_back (s);
10
12
}
11
-
12
- return Ret;
13
+ return ans;
13
14
}
14
- };
15
+ };
Original file line number Diff line number Diff line change
1
+ func fizzBuzz (n int ) []string {
2
+ var ans []string
3
+ for i := 1 ; i <= n ; i ++ {
4
+ s := & strings.Builder {}
5
+ if i % 3 == 0 {
6
+ s .WriteString ("Fizz" )
7
+ }
8
+ if i % 5 == 0 {
9
+ s .WriteString ("Buzz" )
10
+ }
11
+ if s .Len () == 0 {
12
+ s .WriteString (strconv .Itoa (i ))
13
+ }
14
+ ans = append (ans , s .String ())
15
+ }
16
+ return ans
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <String > fizzBuzz (int n ) {
3
+ List <String > ans = new ArrayList <>();
4
+ for (int i = 1 ; i <= n ; ++i ) {
5
+ String s = "" ;
6
+ if (i % 3 == 0 ) {
7
+ s += "Fizz" ;
8
+ }
9
+ if (i % 5 == 0 ) {
10
+ s += "Buzz" ;
11
+ }
12
+ if (s .length () == 0 ) {
13
+ s += i ;
14
+ }
15
+ ans .add (s );
16
+ }
17
+ return ans ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change 1
1
class Solution :
2
- def fizzBuzz (self , n ):
3
- """
4
- :type n: int
5
- :rtype: List[str]
6
- """
7
-
8
- Ret = [str (i ) for i in range (1 , n + 1 )]
9
-
10
- for i in range (3 , n + 1 ):
11
- if ( i % 15 == 0 ):
12
- Ret [i - 1 ] = "FizzBuzz"
13
- elif ( i % 3 == 0 ):
14
- Ret [i - 1 ] = "Fizz"
15
- elif ( i % 5 == 0 ):
16
- Ret [i - 1 ] = "Buzz"
17
-
18
- return Ret
2
+ def fizzBuzz (self , n : int ) -> List [str ]:
3
+ ans = []
4
+ for i in range (1 , n + 1 ):
5
+ if i % 15 == 0 :
6
+ ans .append ('FizzBuzz' )
7
+ elif i % 3 == 0 :
8
+ ans .append ('Fizz' )
9
+ elif i % 5 == 0 :
10
+ ans .append ('Buzz' )
11
+ else :
12
+ ans .append (str (i ))
13
+ return ans
Original file line number Diff line number Diff line change 45
45
46
46
<p >  ; </p >
47
47
48
-
49
48
## 解法
50
49
51
50
<!-- 这里可写通用的实现逻辑 -->
Original file line number Diff line number Diff line change 35
35
<li><code>1 <= heights[i][j] <= 10<sup>5</sup></code></li>
36
36
</ul >
37
37
38
-
39
38
## Solutions
40
39
41
40
<!-- tabs:start -->
You can’t perform that action at this time.
0 commit comments