File tree Expand file tree Collapse file tree 8 files changed +204
-32
lines changed
0417.Pacific Atlantic Water Flow Expand file tree Collapse file tree 8 files changed +204
-32
lines changed Original file line number Diff line number Diff line change 3838]
3939</pre >
4040
41-
4241## 解法
4342
4443<!-- 这里可写通用的实现逻辑 -->
5049<!-- 这里可写当前语言的特殊实现逻辑 -->
5150
5251``` 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
5465```
5566
5667### ** Java**
5768
5869<!-- 这里可写当前语言的特殊实现逻辑 -->
5970
6071``` 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+ ```
61112
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+ }
62133```
63134
64135### ** ...**
Original file line number Diff line number Diff line change 3131 <li><code>1 <= n <= 10<sup>4</sup></code></li>
3232</ul >
3333
34-
3534## Solutions
3635
3736<!-- tabs:start -->
3837
3938### ** Python3**
4039
4140``` 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
4354```
4455
4556### ** Java**
4657
4758``` 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+ ```
4899
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+ }
49120```
50121
51122### ** ...**
Original file line number Diff line number Diff line change 11class Solution {
22public:
33 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);
1012 }
11-
12- return Ret;
13+ return ans;
1314 }
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 11class 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 4545
4646<p >  ; </p >
4747
48-
4948## 解法
5049
5150<!-- 这里可写通用的实现逻辑 -->
Original file line number Diff line number Diff line change 3535 <li><code>1 <= heights[i][j] <= 10<sup>5</sup></code></li>
3636</ul >
3737
38-
3938## Solutions
4039
4140<!-- tabs:start -->
You can’t perform that action at this time.
0 commit comments