File tree 6 files changed +130
-4
lines changed
0100-0199/0122.Best Time to Buy and Sell Stock II
0200-0299/0242.Valid Anagram
6 files changed +130
-4
lines changed Original file line number Diff line number Diff line change @@ -225,20 +225,20 @@ public class Solution {
225
225
226
226
动态规划:
227
227
228
- ``` cs
229
228
``` cs
230
229
public class Solution {
231
230
public int MaxProfit (int [] prices ) {
232
231
int f1 = - prices [0 ], f2 = 0 ;
233
- for (int i = 1 ; i < prices .Length ; ++ i ) {
232
+ for (int i = 1 ; i < prices .Length ; ++ i )
233
+ {
234
234
f1 = Math .Max (f1 , f2 - prices [i ]);
235
235
f2 = Math .Max (f2 , f1 + prices [i ]);
236
236
}
237
237
return f2 ;
238
238
}
239
239
}
240
240
```
241
- ```
241
+
242
242
243
243
### ** ...**
244
244
Original file line number Diff line number Diff line change @@ -205,7 +205,8 @@ Dynamic Programming:
205
205
public class Solution {
206
206
public int MaxProfit (int [] prices ) {
207
207
int f1 = - prices [0 ], f2 = 0 ;
208
- for (int i = 1 ; i < prices .Length ; ++ i ) {
208
+ for (int i = 1 ; i < prices .Length ; ++ i )
209
+ {
209
210
f1 = Math .Max (f1 , f2 - prices [i ]);
210
211
f2 = Math .Max (f2 , f1 + prices [i ]);
211
212
}
Original file line number Diff line number Diff line change @@ -80,6 +80,51 @@ class Solution {
80
80
}
81
81
```
82
82
83
+ ### ** C++**
84
+
85
+ ``` cpp
86
+ class Solution {
87
+ public:
88
+ bool isAnagram(string s, string t) {
89
+ if (s.size() != t.size()) {
90
+ return false;
91
+ }
92
+ vector<int > chars(26, 0);
93
+ for (int i = 0, n = s.size(); i < n; ++i) {
94
+ ++chars[ s[ i] - 'a'] ;
95
+ --chars[ t[ i] - 'a'] ;
96
+ }
97
+ for (int i = 0; i < 26; ++i) {
98
+ if (chars[ i] != 0) {
99
+ return false;
100
+ }
101
+ }
102
+ return true;
103
+ }
104
+ };
105
+ ```
106
+
107
+ ### **Go**
108
+
109
+ ```go
110
+ func isAnagram(s string, t string) bool {
111
+ if len(s) != len(t) {
112
+ return false
113
+ }
114
+ var chars [26]int
115
+ for i := 0; i < len(s); i++ {
116
+ chars[s[i]-'a']++
117
+ chars[t[i]-'a']--
118
+ }
119
+ for i := 0; i < 26; i++ {
120
+ if chars[i] != 0 {
121
+ return false
122
+ }
123
+ }
124
+ return true
125
+ }
126
+ ```
127
+
83
128
### ** ...**
84
129
85
130
```
Original file line number Diff line number Diff line change @@ -72,6 +72,51 @@ class Solution {
72
72
}
73
73
```
74
74
75
+ ### ** C++**
76
+
77
+ ``` cpp
78
+ class Solution {
79
+ public:
80
+ bool isAnagram(string s, string t) {
81
+ if (s.size() != t.size()) {
82
+ return false;
83
+ }
84
+ vector<int > chars(26, 0);
85
+ for (int i = 0, n = s.size(); i < n; ++i) {
86
+ ++chars[ s[ i] - 'a'] ;
87
+ --chars[ t[ i] - 'a'] ;
88
+ }
89
+ for (int i = 0; i < 26; ++i) {
90
+ if (chars[ i] != 0) {
91
+ return false;
92
+ }
93
+ }
94
+ return true;
95
+ }
96
+ };
97
+ ```
98
+
99
+ ### **Go**
100
+
101
+ ```go
102
+ func isAnagram(s string, t string) bool {
103
+ if len(s) != len(t) {
104
+ return false
105
+ }
106
+ var chars [26]int
107
+ for i := 0; i < len(s); i++ {
108
+ chars[s[i]-'a']++
109
+ chars[t[i]-'a']--
110
+ }
111
+ for i := 0; i < 26; i++ {
112
+ if chars[i] != 0 {
113
+ return false
114
+ }
115
+ }
116
+ return true
117
+ }
118
+ ```
119
+
75
120
### ** ...**
76
121
77
122
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isAnagram (string s, string t) {
4
+ if (s.size () != t.size ()) {
5
+ return false ;
6
+ }
7
+ vector<int > chars (26 , 0 );
8
+ for (int i = 0 , n = s.size (); i < n; ++i) {
9
+ ++chars[s[i] - ' a' ];
10
+ --chars[t[i] - ' a' ];
11
+ }
12
+ for (int i = 0 ; i < 26 ; ++i) {
13
+ if (chars[i] != 0 ) {
14
+ return false ;
15
+ }
16
+ }
17
+ return true ;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ func isAnagram (s string , t string ) bool {
2
+ if len (s ) != len (t ) {
3
+ return false
4
+ }
5
+ var chars [26 ]int
6
+ for i := 0 ; i < len (s ); i ++ {
7
+ chars [s [i ]- 'a' ]++
8
+ chars [t [i ]- 'a' ]--
9
+ }
10
+ for i := 0 ; i < 26 ; i ++ {
11
+ if chars [i ] != 0 {
12
+ return false
13
+ }
14
+ }
15
+ return true
16
+ }
You can’t perform that action at this time.
0 commit comments