【JAVA、C++】LeetCode 009 Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
解题思路一:
双指针法,逐位判断
Java代码如下:
static public boolean isPalindrome(int x) {
if (x < 0)
return false;
int temp = x,beginIndex = 0, endIndex = 0;
while (temp >= 10) {
temp /= 10;
endIndex++;
}
while (beginIndex < endIndex) {
if ((int)((x / Math.pow(10,beginIndex))%10) != (int)((x / Math.pow(10,endIndex))%10))
return false;
beginIndex++;
endIndex--;
}
return true;
}
解题思路二:
计算出回文的值,然后判断回文的值是否等于自身:
C++:
#include<algorithm>
using namespace std;
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
long longx = x, palindromex = ;
while (x) {
palindromex = palindromex * + x % ;
x /= ;
}
return longx == palindromex;
}
};
【JAVA、C++】LeetCode 009 Palindrome Number的更多相关文章
- 【JAVA、C++】LeetCode 012 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- BIEE 仪表盘的创建
1.新建仪表盘 选择共享文件夹里创建的表拖到仪表盘中 保存并运行 也可以新建更多仪表盘页 2.新建仪表盘提示 把提示和表都拖到仪表盘中 3.主从关系:第二张表的结果页面 ——>编辑视图——&g ...
- dedecms首页调用栏目内容和单页内容的方法
常用的需要调到首页来的单页内容,比如企业简介.联系我们等等内容,我们在首页可能都要进行体现.通过常规的方式,包括查阅dede官方论坛资料,都找不到比较合适的答案.今天我们就提供两种方式进行调用. 我们 ...
- Unity-Tween
1.GoKit 免费开源 AssetStore:https://www.assetstore.unity3d.com/en/#!/content/3663 下载地址:https://github.co ...
- spring 标注 详解
http://snowolf.iteye.com/blog/578452 http://snowolf.iteye.com/blog/578452 非常棒的入门读物
- C语言绘制余弦函数图象
#include"stdio.h" #include"math.h" void main() { double y; int x,m; for(y=1;y> ...
- Lamp学习笔记
1,php.ini 文件在哪里 /opt/app/php-5.3/etc/php.ini -------------------------------------------- 2014-0 ...
- 使用Fabric进行crash收集统计
主要是帮助自己记一下地址. 1 申请Crashlytics服务:http://try.crashlytics.com 2 下载Fabric客户端,帮助集成Crashlytics到自己的项目中:http ...
- Brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3624 Accepted: 1879 Descript ...
- Unity3D中定时器的使用
源地址:http://unity3d.9tech.cn/news/2014/0402/40149.html 在游戏设计过程中定时器是必不可少的工具,我们知道update方法是MonoBehavior中 ...
- 把 表拷贝到test测试数据库
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) bundle exec rake db:schema:load RAILS_ENV=test 注 ...