File tree Expand file tree Collapse file tree 15 files changed +111
-53
lines changed
1805.Number of Different Integers in a String
1812.Determine Color of a Chessboard Square
2491.Divide Players Into Teams of Equal Skill
2493.Divide Nodes Into the Maximum Number of Groups Expand file tree Collapse file tree 15 files changed +111
-53
lines changed Original file line number Diff line number Diff line change 54
54
55
55
** 方法一:双指针 + 模拟**
56
56
57
- 遍历字符串,找到每个整数的起始位置和结束位置,截取出这一个子串,将其存入哈希表 ` s ` 中。
57
+ 遍历字符串 ` word ` ,找到每个整数的起始位置和结束位置,截取出这一个子串,将其存入哈希表 $s$ 中。
58
58
59
- 最后返回哈希表 ` s ` 的大小即可。
59
+ 遍历结束,返回哈希表 $s$ 的大小即可。
60
60
61
- 注意,这里要去掉整数的前导零,并且不能直接将字符串转为整数,因为整数可能很大,超出 ` int ` 的范围 。
61
+ > 注意,每个子串所表示的整数可能很大,我们不能直接将其转为整数。因此,我们可以去掉每个子串的前导零之后,再存入哈希表 。
62
62
63
63
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 ` word ` 的长度。
64
64
@@ -123,13 +123,9 @@ public:
123
123
int n = word.size();
124
124
for (int i = 0; i < n; ++i) {
125
125
if (isdigit(word[ i] )) {
126
- while (i < n && word[ i] == '0') {
127
- ++i;
128
- }
126
+ while (i < n && word[ i] == '0') ++i;
129
127
int j = i;
130
- while (j < n && isdigit(word[ j] )) {
131
- ++j;
132
- }
128
+ while (j < n && isdigit(word[ j] )) ++j;
133
129
s.insert(word.substr(i, j - i));
134
130
i = j;
135
131
}
Original file line number Diff line number Diff line change @@ -104,13 +104,9 @@ public:
104
104
int n = word.size();
105
105
for (int i = 0; i < n; ++i) {
106
106
if (isdigit(word[ i] )) {
107
- while (i < n && word[ i] == '0') {
108
- ++i;
109
- }
107
+ while (i < n && word[ i] == '0') ++i;
110
108
int j = i;
111
- while (j < n && isdigit(word[ j] )) {
112
- ++j;
113
- }
109
+ while (j < n && isdigit(word[ j] )) ++j;
114
110
s.insert(word.substr(i, j - i));
115
111
i = j;
116
112
}
Original file line number Diff line number Diff line change @@ -5,13 +5,9 @@ class Solution {
5
5
int n = word.size ();
6
6
for (int i = 0 ; i < n; ++i) {
7
7
if (isdigit (word[i])) {
8
- while (i < n && word[i] == ' 0' ) {
9
- ++i;
10
- }
8
+ while (i < n && word[i] == ' 0' ) ++i;
11
9
int j = i;
12
- while (j < n && isdigit (word[j])) {
13
- ++j;
14
- }
10
+ while (j < n && isdigit (word[j])) ++j;
15
11
s.insert (word.substr (i, j - i));
16
12
i = j;
17
13
}
Original file line number Diff line number Diff line change 53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
+ ** 方法一:奇偶判断**
57
+
58
+ 根据 ` coordinates ` 获取对应的坐标 $(x, y)$,如果 $(x + y)$ 为奇数,则格子为白色,返回 ` true ` ,否则返回 ` false ` 。
59
+
60
+ 时间复杂度 $O(1)$。
61
+
56
62
<!-- tabs:start -->
57
63
58
64
### ** Python3**
@@ -81,6 +87,29 @@ class Solution {
81
87
}
82
88
```
83
89
90
+ ### ** C++**
91
+
92
+ ``` cpp
93
+ class Solution {
94
+ public:
95
+ bool squareIsWhite(string coordinates) {
96
+ int x = coordinates[ 0] - 'a' + 1;
97
+ int y = coordinates[ 1] - '0';
98
+ return ((x + y) & 1) == 1;
99
+ }
100
+ };
101
+ ```
102
+
103
+ ### **Go**
104
+
105
+ ```go
106
+ func squareIsWhite(coordinates string) bool {
107
+ x := coordinates[0] - 'a' + 1
108
+ y := coordinates[1] - '0'
109
+ return ((x + y) & 1) == 1
110
+ }
111
+ ```
112
+
84
113
### ** JavaScript**
85
114
86
115
``` js
@@ -89,8 +118,8 @@ class Solution {
89
118
* @return {boolean}
90
119
*/
91
120
var squareIsWhite = function (coordinates ) {
92
- let x = coordinates .charAt (0 ).charCodeAt () - ' a' .charCodeAt () + 1 ;
93
- let y = Number (coordinates .charAt (1 ));
121
+ const x = coordinates .charAt (0 ).charCodeAt () - ' a' .charCodeAt () + 1 ;
122
+ const y = Number (coordinates .charAt (1 ));
94
123
return ((x + y) & 1 ) == 1 ;
95
124
};
96
125
```
Original file line number Diff line number Diff line change @@ -71,6 +71,29 @@ class Solution {
71
71
}
72
72
```
73
73
74
+ ### ** C++**
75
+
76
+ ``` cpp
77
+ class Solution {
78
+ public:
79
+ bool squareIsWhite(string coordinates) {
80
+ int x = coordinates[ 0] - 'a' + 1;
81
+ int y = coordinates[ 1] - '0';
82
+ return ((x + y) & 1) == 1;
83
+ }
84
+ };
85
+ ```
86
+
87
+ ### **Go**
88
+
89
+ ```go
90
+ func squareIsWhite(coordinates string) bool {
91
+ x := coordinates[0] - 'a' + 1
92
+ y := coordinates[1] - '0'
93
+ return ((x + y) & 1) == 1
94
+ }
95
+ ```
96
+
74
97
### ** JavaScript**
75
98
76
99
``` js
@@ -79,8 +102,8 @@ class Solution {
79
102
* @return {boolean}
80
103
*/
81
104
var squareIsWhite = function (coordinates ) {
82
- let x = coordinates .charAt (0 ).charCodeAt () - ' a' .charCodeAt () + 1 ;
83
- let y = Number (coordinates .charAt (1 ));
105
+ const x = coordinates .charAt (0 ).charCodeAt () - ' a' .charCodeAt () + 1 ;
106
+ const y = Number (coordinates .charAt (1 ));
84
107
return ((x + y) & 1 ) == 1 ;
85
108
};
86
109
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool squareIsWhite (string coordinates) {
4
+ int x = coordinates[0 ] - ' a' + 1 ;
5
+ int y = coordinates[1 ] - ' 0' ;
6
+ return ((x + y) & 1 ) == 1 ;
7
+ }
8
+ };
Original file line number Diff line number Diff line change
1
+ func squareIsWhite (coordinates string ) bool {
2
+ x := coordinates [0 ] - 'a' + 1
3
+ y := coordinates [1 ] - '0'
4
+ return ((x + y ) & 1 ) == 1
5
+ }
Original file line number Diff line number Diff line change 3
3
* @return {boolean }
4
4
*/
5
5
var squareIsWhite = function ( coordinates ) {
6
- let x = coordinates . charAt ( 0 ) . charCodeAt ( ) - 'a' . charCodeAt ( ) + 1 ;
7
- let y = Number ( coordinates . charAt ( 1 ) ) ;
6
+ const x = coordinates . charAt ( 0 ) . charCodeAt ( ) - 'a' . charCodeAt ( ) + 1 ;
7
+ const y = Number ( coordinates . charAt ( 1 ) ) ;
8
8
return ( ( x + y ) & 1 ) == 1 ;
9
9
} ;
Original file line number Diff line number Diff line change @@ -207,7 +207,6 @@ impl Solution {
207
207
}
208
208
```
209
209
210
-
211
210
### ** ...**
212
211
213
212
```
Original file line number Diff line number Diff line change 1
- var isCircularSentence = function ( sentence ) {
1
+ var isCircularSentence = function ( sentence ) {
2
2
const words = sentence . split ( ' ' ) ;
3
3
const post = words [ 0 ] . charCodeAt ( 0 ) ;
4
4
let prev = words [ 0 ] . charCodeAt ( words [ 0 ] . length - 1 ) ;
@@ -11,4 +11,4 @@ var isCircularSentence = function(sentence) {
11
11
prev = cur . charCodeAt ( cur . length - 1 ) ;
12
12
}
13
13
return post === prev ;
14
- } ;
14
+ } ;
You can’t perform that action at this time.
0 commit comments