【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 ...
随机推荐
- DNA repair问题
问题:Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inh ...
- 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2022 Solved: 1158[Submit][Sta ...
- NOI2002 洛谷 P1196 银河英雄传说
神奇的并查集问题 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩 ...
- JAVA-封装
1.什么是封装? 顾名思义,封装就是装起来,圈起来的意思,用于类与对象中来讲,就是在一个类中把对象拥有的属性和隐藏信息(条件)进行封装,不允许外部程序直接访问,而必须要通过该类提供的方法来实现对隐藏信 ...
- abstract 类 构造函数
public abstract class CommonReq { private String TransNo { get; set; } public String SubmitData { ge ...
- spark对于elasticsearch里的复杂类型支持
IP,直接在case class里用string, 可以考虑先用其它程序生成相关的mapping,然后再去用spark填充数据
- rwsr-sr-x类似权限设置
如何设置suid/guid? 如果希望设置suid,那么就将相应的权限位之前的那一位设置为4:如果希望设置guid,那么就将相应的权限位之前的那一位设置为2:如果希望两者都置位,那么将相应的权限位之前 ...
- 安装TFS2008最终版(转载)
一.安装操作系统:windows server 2003 + Sp2具体步骤: 1.安装windows server 2003时选用工作组(默认为workgroup).由于在工作组环境中部署,因此使用 ...
- angular2怎么使用第三方的库(jquery等)
网上找了很多教材都搜索不到该部分类型,自己测试了下写了该教程. 场景说明:项目需要使用bootstrap,众所周知bootstrap没有时间日期控件的,需要使用第三方控件,我对如何在angular2中 ...
- ngrok反向代理
关于ngrok ngrok 是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.ngrok 可捕获和分析所有通道上的流量,便于后期分析和重放. 为什么使用ngrok? ...