File tree Expand file tree Collapse file tree 5 files changed +56
-62
lines changed
solution/0000-0099/0009.Palindrome Number Expand file tree Collapse file tree 5 files changed +56
-62
lines changed Original file line number Diff line number Diff line change @@ -177,13 +177,12 @@ function isPalindrome(x: number): boolean {
177177``` rust
178178impl Solution {
179179 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 ) {
181181 return false ;
182182 }
183183 let mut y = 0 ;
184184 while x > y {
185- y *= 10 ;
186- y += x % 10 ;
185+ y = y * 10 + x % 10 ;
187186 x /= 10 ;
188187 }
189188 x == y || x == y / 10
@@ -213,17 +212,13 @@ var isPalindrome = function (x) {
213212#### C#
214213
215214``` 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 )) {
222218 return false ;
223219 }
224220 int y = 0 ;
225- for (; y < x ; x /= 10 )
226- {
221+ for (; y < x ; x /= 10 ) {
227222 y = y * 10 + x % 10 ;
228223 }
229224 return x == y || x == y / 10 ;
@@ -236,14 +231,19 @@ public class Solution
236231``` php
237232class Solution {
238233 /**
239- * @param int $x
240- * @return boolean
234+ * @param Integer $x
235+ * @return Boolean
241236 */
242-
243237 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);
247247 }
248248}
249249```
Original file line number Diff line number Diff line change @@ -169,13 +169,12 @@ function isPalindrome(x: number): boolean {
169169``` rust
170170impl Solution {
171171 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 ) {
173173 return false ;
174174 }
175175 let mut y = 0 ;
176176 while x > y {
177- y *= 10 ;
178- y += x % 10 ;
177+ y = y * 10 + x % 10 ;
179178 x /= 10 ;
180179 }
181180 x == y || x == y / 10
@@ -205,17 +204,13 @@ var isPalindrome = function (x) {
205204#### C#
206205
207206``` 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 )) {
214210 return false ;
215211 }
216212 int y = 0 ;
217- for (; y < x ; x /= 10 )
218- {
213+ for (; y < x ; x /= 10 ) {
219214 y = y * 10 + x % 10 ;
220215 }
221216 return x == y || x == y / 10 ;
@@ -228,14 +223,19 @@ public class Solution
228223``` php
229224class Solution {
230225 /**
231- * @param int $x
232- * @return boolean
226+ * @param Integer $x
227+ * @return Boolean
233228 */
234-
235229 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);
239239 }
240240}
241241```
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 ) ) {
74 return false ;
85 }
96 int y = 0 ;
10- for ( ; y < x ; x /= 10 )
11- {
7+ for ( ; y < x ; x /= 10 ) {
128 y = y * 10 + x % 10 ;
139 }
1410 return x == y || x == y / 10 ;
Original file line number Diff line number Diff line change 11class Solution {
22 /**
3- * @param int $x
4- * @return boolean
3+ * @param Integer $x
4+ * @return Boolean
55 */
6-
76 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 );
1116 }
1217}
Original file line number Diff line number Diff line change 11impl 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 ) {
44 return false ;
55 }
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 ;
1710 }
18- true
11+ x == y || x == y / 10
1912 }
2013}
You can’t perform that action at this time.
0 commit comments