LeetCode每日一题(2018-01-05)
2018-01-05 15:14
417 查看
Determine whether an integer is a palindrome. Do this without extra space.
判断是否为回文数。
我的代码:
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
if(x<10) return true;
int x1 = x;
int num = 1;
while (x1>=10){
x1 = x1/10;
num++;
}
int[] array = new int[num];
for(int i=num-1;i>=0;i--){
array[i] = x%10;
x = x/10;
}
for(int i=0;i<num/2;i++){
if(array[i]!=array[num-1-i])break;
if(i+1>=num/2){
return true;
}
}
return false;
}
}
判断是否为回文数。
我的代码:
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
if(x<10) return true;
int x1 = x;
int num = 1;
while (x1>=10){
x1 = x1/10;
num++;
}
int[] array = new int[num];
for(int i=num-1;i>=0;i--){
array[i] = x%10;
x = x/10;
}
for(int i=0;i<num/2;i++){
if(array[i]!=array[num-1-i])break;
if(i+1>=num/2){
return true;
}
}
return false;
}
}
相关文章推荐
- 每日进步之leetcode15题C++实现代码
- leetcode:292 Nim Game-每日编程第一题
- LeetCode每日一题(2018-01-24)
- leetcode:232 Implement Queue using Stacks-每日编程第十六题
- leetcode:112 Path Sum-每日编程第二十七题
- 【leetcode每日一题】234.Palindrome Linked List
- 【leetcode每日一题】226.Invert Binary Tree
- leetcode:14 Longest Common Prefix-每日编程第四十二题
- leetcode 每日一题 283. Move Zeroes
- LeetCode每日一练 阿拉伯数字转罗马数字
- leetcode 每日一题 34. Search for a Range
- 【leetcode每日一题】NO92.Reverse Linked List II
- 每日一练之Two sum [leetcode No.1]
- LeetCode每日一练(2) Add Two Numbers C++版
- leetcode:258 Add Digits-每日编程第二题
- LeetCode每日一题(2018-01-29)
- 【leetcode每日一题】100.same treet
- 再从萌新开始-Leetcode每日题解-447. Number of Boomerangs
- leetcode 每日一题 263. Ugly Number
- 每日AC--LeetCode-Candy