Hi!
LeetCode 第九道题: Palindrome Number,难度简单。
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true
Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
1 public class Solution {
2 public bool IsPalindrome ( int x )
3 {
4 if ( x < 0 )
5 {
6 return false ;
7 }
8
9 var rx = Reverse ( x );
10 return x == rx ;
11 }
12
13 public int Reverse ( int x )
14 {
15
16 int flag = 1 ;
17 if ( x < 0 )
18 {
19 flag = - 1 ;
20 x = - 1 * x ;
21 }
22
23 var str = x . ToString ();
24 var strL = "" ;
25 var strR = "" ;
26 var len = str . Length ;
27 for ( int i = 0 ; i < len ; i ++)
28 {
29 if ( i < len - i - 1 )
30 {
31 if (! string . IsNullOrEmpty ( strL ) || str [ len - i - 1 ] != '0' )
32 {
33 strL += str [ len - i - 1 ]. ToString ();
34 }
35
36 strR = str [ i ]. ToString () + strR ;
37 }
38 else if ( i == len - i - 1 )
39 {
40 strL += str [ i ];
41 break ;
42 }
43 else
44 {
45 break ;
46 }
47 }
48
49 str = strL + strR ;
50
51 var min = - 1 * Math . Pow ( 2 , 31 );
52 var max = Math . Pow ( 2 , 31 ) - 1 ;
53
54 double num = 0 ;
55 for ( var i = 0 ; i < str . Length ; i ++)
56 {
57 num += int . Parse ( str [ i ]. ToString ()) * Math . Pow ( 10 , str . Length - i - 1 );
58 var signNum = flag * num ;
59 if ( signNum > max || signNum < min )
60 {
61 return 0 ;
62 }
63 }
64
65 return flag * int . Parse ( num . ToString ());
66 }
67 }
Gist code
To use, see:Jektify - Doc
Música
Jekyll plugin to generate html snippets for embedding Spotify Musics. To listen to the full song, open your Spotify and start these musics.
Goodbye!
The word of the day!
Put a very powerful message.