-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path9.java
More file actions
117 lines (108 loc) · 3 KB
/
9.java
File metadata and controls
117 lines (108 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
__________________________________________________________________________________________________
6ms
class Solution {
public boolean isPalindrome(int x) {
if (x<0) {
return false;
}
// x%10 = value of digit 0
// x/10%10 value of digit 1
// x/10^n%10 value of digit n
// 123 --> 321
// abcdedcba
// abba
// revert half and
long number = (long)x;
long sum = 0;
// 121
while (x!=0) {
int digit = x%10;//1,2,1
x = x/10;// 12,1,0
sum = sum * 10 + digit; // 121
}
return number == sum;
// 3,2,1
}
}
__________________________________________________________________________________________________
7ms
class Solution {
public boolean isPalindrome(int x) {
if(x < 0) return false;
int w = x;
int y = 0;
while(x != 0) {
y = y * 10 + x % 10;
x = x / 10;
}
return y == w;
}
}
__________________________________________________________________________________________________
8ms
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
char[] str=Integer.toString(Math.abs(x)).toCharArray();
int len=str.length;
for(int i=0;i<len/2;i++){
if(str[i]!=str[len-1-i])
return false;
}
return true;
}
}
__________________________________________________________________________________________________
37196 kb
import java.util.*;
class Solution {
int [] intToArray(int n){
List<Integer> temp = new ArrayList<Integer>();
while (n!=0){
temp.add(n%10);
n = n/10;
}
int [] ans = temp.stream().mapToInt(i -> i).toArray();
return ans ;
}
public boolean isPalindrome(int x) {
if(x<0) return false;
int [] check = intToArray(x);
int size = check.length;
for (int i =0;i<size/2;i++){
if(check[i]!=check[size-i-1]){
return false;
}
}
return true;
}
}
__________________________________________________________________________________________________
38912 kb
class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
} else {
int reversedInt = reverseInteger(x);
return reversedInt == x;
}
}
private int reverseInteger(int x) {
int sign = 1;
if(x < 0){
sign = -1;
x *= -1;
}
long result = 0;
while(x != 0){
result = result * 10 + x % 10;
if(sign * result > Integer.MAX_VALUE ||
sign * result < Integer.MIN_VALUE)
return 0;
x = x / 10;
}
return (int)result * sign;
}
}
__________________________________________________________________________________________________