File tree 14 files changed +432
-95
lines changed
14 files changed +432
-95
lines changed Original file line number Diff line number Diff line change @@ -177,13 +177,12 @@ function isPalindrome(x: number): boolean {
177
177
``` rust
178
178
impl Solution {
179
179
pub fn is_palindrome (mut x : i32 ) -> bool {
180
- if x < 0 || (x % 10 == 0 && x ! = 0 ) {
180
+ if x < 0 || (x != 0 && x % 10 = = 0 ) {
181
181
return false ;
182
182
}
183
183
let mut y = 0 ;
184
184
while x > y {
185
- y *= 10 ;
186
- y += x % 10 ;
185
+ y = y * 10 + x % 10 ;
187
186
x /= 10 ;
188
187
}
189
188
x == y || x == y / 10
@@ -213,17 +212,13 @@ var isPalindrome = function (x) {
213
212
#### C#
214
213
215
214
``` cs
216
- public class Solution
217
- {
218
- public bool IsPalindrome (int x )
219
- {
220
- if (x < 0 || (x > 0 && x % 10 == 0 ))
221
- {
215
+ public class Solution {
216
+ public bool IsPalindrome (int x ) {
217
+ if (x < 0 || (x > 0 && x % 10 == 0 )) {
222
218
return false ;
223
219
}
224
220
int y = 0 ;
225
- for (; y < x ; x /= 10 )
226
- {
221
+ for (; y < x ; x /= 10 ) {
227
222
y = y * 10 + x % 10 ;
228
223
}
229
224
return x == y || x == y / 10 ;
@@ -236,14 +231,19 @@ public class Solution
236
231
``` php
237
232
class Solution {
238
233
/**
239
- * @param int $x
240
- * @return boolean
234
+ * @param Integer $x
235
+ * @return Boolean
241
236
*/
242
-
243
237
function isPalindrome($x) {
244
- $str = (string) $x;
245
- $str_reverse = strrev($str);
246
- return $str === $str_reverse;
238
+ if ($x < 0 || ($x && $x % 10 == 0)) {
239
+ return false;
240
+ }
241
+ $y = 0;
242
+ while ($x > $y) {
243
+ $y = $y * 10 + ($x % 10);
244
+ $x = (int) ($x / 10);
245
+ }
246
+ return $x == $y || $x == (int) ($y / 10);
247
247
}
248
248
}
249
249
```
Original file line number Diff line number Diff line change @@ -169,13 +169,12 @@ function isPalindrome(x: number): boolean {
169
169
``` rust
170
170
impl Solution {
171
171
pub fn is_palindrome (mut x : i32 ) -> bool {
172
- if x < 0 || (x % 10 == 0 && x ! = 0 ) {
172
+ if x < 0 || (x != 0 && x % 10 = = 0 ) {
173
173
return false ;
174
174
}
175
175
let mut y = 0 ;
176
176
while x > y {
177
- y *= 10 ;
178
- y += x % 10 ;
177
+ y = y * 10 + x % 10 ;
179
178
x /= 10 ;
180
179
}
181
180
x == y || x == y / 10
@@ -205,17 +204,13 @@ var isPalindrome = function (x) {
205
204
#### C#
206
205
207
206
``` cs
208
- public class Solution
209
- {
210
- public bool IsPalindrome (int x )
211
- {
212
- if (x < 0 || (x > 0 && x % 10 == 0 ))
213
- {
207
+ public class Solution {
208
+ public bool IsPalindrome (int x ) {
209
+ if (x < 0 || (x > 0 && x % 10 == 0 )) {
214
210
return false ;
215
211
}
216
212
int y = 0 ;
217
- for (; y < x ; x /= 10 )
218
- {
213
+ for (; y < x ; x /= 10 ) {
219
214
y = y * 10 + x % 10 ;
220
215
}
221
216
return x == y || x == y / 10 ;
@@ -228,14 +223,19 @@ public class Solution
228
223
``` php
229
224
class Solution {
230
225
/**
231
- * @param int $x
232
- * @return boolean
226
+ * @param Integer $x
227
+ * @return Boolean
233
228
*/
234
-
235
229
function isPalindrome($x) {
236
- $str = (string) $x;
237
- $str_reverse = strrev($str);
238
- return $str === $str_reverse;
230
+ if ($x < 0 || ($x && $x % 10 == 0)) {
231
+ return false;
232
+ }
233
+ $y = 0;
234
+ while ($x > $y) {
235
+ $y = $y * 10 + ($x % 10);
236
+ $x = (int) ($x / 10);
237
+ }
238
+ return $x == $y || $x == (int) ($y / 10);
239
239
}
240
240
}
241
241
```
Original file line number Diff line number Diff line change 1
- public class Solution
2
- {
3
- public bool IsPalindrome ( int x )
4
- {
5
- if ( x < 0 || ( x > 0 && x % 10 == 0 ) )
6
- {
1
+ public class Solution {
2
+ public bool IsPalindrome ( int x ) {
3
+ if ( x < 0 || ( x > 0 && x % 10 == 0 ) ) {
7
4
return false ;
8
5
}
9
6
int y = 0 ;
10
- for ( ; y < x ; x /= 10 )
11
- {
7
+ for ( ; y < x ; x /= 10 ) {
12
8
y = y * 10 + x % 10 ;
13
9
}
14
10
return x == y || x == y / 10 ;
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
/**
3
- * @param int $x
4
- * @return boolean
3
+ * @param Integer $x
4
+ * @return Boolean
5
5
*/
6
-
7
6
function isPalindrome ($x ) {
8
- $str = (string ) $x ;
9
- $str_reverse = strrev ($str );
10
- return $str === $str_reverse ;
7
+ if ($x < 0 || ($x && $x % 10 == 0 )) {
8
+ return false ;
9
+ }
10
+ $y = 0 ;
11
+ while ($x > $y ) {
12
+ $y = $y * 10 + ($x % 10 );
13
+ $x = (int ) ($x / 10 );
14
+ }
15
+ return $x == $y || $x == (int ) ($y / 10 );
11
16
}
12
17
}
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
- pub fn is_palindrome ( x : i32 ) -> bool {
3
- if x < 0 {
2
+ pub fn is_palindrome ( mut x : i32 ) -> bool {
3
+ if x < 0 || ( x != 0 && x % 10 == 0 ) {
4
4
return false ;
5
5
}
6
- let s = x. to_string ( ) ;
7
- let bs = s. as_bytes ( ) ;
8
- let n = bs. len ( ) ;
9
- let mut l = 0 ;
10
- let mut r = n - 1 ;
11
- while l < r {
12
- if bs[ l] != bs[ r] {
13
- return false ;
14
- }
15
- l += 1 ;
16
- r -= 1 ;
6
+ let mut y = 0 ;
7
+ while x > y {
8
+ y = y * 10 + x % 10 ;
9
+ x /= 10 ;
17
10
}
18
- true
11
+ x == y || x == y / 10
19
12
}
20
13
}
You can’t perform that action at this time.
0 commit comments