Determine whether an integer is a palindrome. Do this without extra space.
判断是否为回文数,比较简单,可以用两种方式做,一种是用java中的stringbuffer的一个反转函数,一种直接判断数字。后一种效率比较高。前者代码量少点。
1:
public class Solution {
public boolean isPalindrome(int x) {
StringBuffer sb = new StringBuffer();
sb.append(x);
StringBuffer sb1 = new StringBuffer(sb);
sb.reverse();
return sb.toString().equals(sb1.toString());
}
}
2:
public class Solution {
public boolean isPalindrome(int x) {
int a = x;
int b = 0;
while (a>=b)
{
if(x%10 == 0 && x != 0){
return false;
}
if (a == b) return true;
b = 10 * b + a % 10;
if (a == b) return true;
a = a / 10;
}
return false;
}
}
判断是否为回文数,比较简单,可以用两种方式做,一种是用java中的stringbuffer的一个反转函数,一种直接判断数字。后一种效率比较高。前者代码量少点。
1:
public class Solution {
public boolean isPalindrome(int x) {
StringBuffer sb = new StringBuffer();
sb.append(x);
StringBuffer sb1 = new StringBuffer(sb);
sb.reverse();
return sb.toString().equals(sb1.toString());
}
}
2:
public class Solution {
public boolean isPalindrome(int x) {
int a = x;
int b = 0;
while (a>=b)
{
if(x%10 == 0 && x != 0){
return false;
}
if (a == b) return true;
b = 10 * b + a % 10;
if (a == b) return true;
a = a / 10;
}
return false;
}
}
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 326Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 365Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 308Given a string containing just ... -
Remove Nth Node From End of List
2015-03-05 22:31 340Given a linked list, remove the ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 332Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 312Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 292Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 319Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 328Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 328Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 289Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 323Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 367Implement regular expression ma ... -
String to Integer (atoi)
2015-02-13 11:07 343Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 227Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 255The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 334Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 300You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 425[size=24px;]Longest Substring W ...
相关推荐
Determine whether an integer is a palindrome. Do this without extra space. Java AC版本
LeetCode Palindrome Number解决方案
palindrome number in c
js代码-9. Palindrome Number
9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid ...
leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 ...245 Palindrome Number 593
Palindrome Number Insert Interval Merge Intervals Minimum Window Substring Multiply Strings Substring with Concatenation of All Words Pascal's Triangle Pascal's Triangle II Spiral Matrix Spiral Matrix...
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中删除重复项 代码: Leetcode\RemoveDuplicates\RemoveDuplicates.cs 问题: 买卖股票的最佳时机 II 代码: Leetcode\MaxProfit\MaxProfit.cs ...
palindrome. 示例 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 运行时间:7 毫秒,比 Java 在线提交的 Palindrome Number 快 80.06%。 内存使用:...
Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates from Sorted List #0118 - Pascal's Triangle #0121 - Best Time to Buy and Sell ...
Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of...
Palindrome Number 简单 11 Container With Most Water 中等 12 Integer to Roman 中等 13 Roman to Integer 简单 14 Longest Common Prefix 简单 15 3Sum 中等 16 3Sum Closest 中等 17 Letter Combinations of a ...
9.Palindrome Number 10.Regular Expression Matching 11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter ...
Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 Valid Parentheses 简单 堆叠 重要 Merge ...
Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses ...
leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance ...Palindrome Number 49.4% Easy
1. Introduction 2. Array i. Remove Element ii. Remove Duplicates from Sorted Array ... Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. Find Peak Element
Palindrome Number Easy #13 Roman to Integer Easy #21 Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say ...