【Leetcode-easy】Palindrome Number
思路:除和求余 取得首位和末尾 比较是否相等。
public boolean isPalindrome(int x){
if(x<0){
return false;
}
int div=1;
while(x/div>=10){
div*=10;
}
while(x!=0){
int left=x/div;
int right=x%10;
if(left!=right){
return false;
}
x=(x%div)/10;//去掉最高位和最低位
div/=100;
}
return true;
}
其他思路:http://m.blog.csdn.net/blog/yike1207/44307819博文总结了几种方法(整数反转、转化为字符串处理,取高低位比较)该作者认为上述方法都用到了额外的存储空间,不用额外的存储空间一种方法是通过递归,并修改引用参数值。
bool isPalindrome(int x, int &y) {
// y为必须为引用和指针型可改变的变量
if (x < 0) return false;
if (x == 0) return true;
if (isPalindrome(x/10, y) && (x%10 == y%10)) {
// 每次执行到 x%10 == y%10 的时候 x 为前i位,y表示前(n+1-i)位
y /= 10;
return true;
} else {
return false;
}
}
bool isPalindrome(int x) {
return isPalindrome(x, x);
}
【Leetcode-easy】Palindrome Number的更多相关文章
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- C# 写 LeetCode easy #9 Palindrome Number
9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...
- 【Leetcode】【Easy】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是不是回文整数(例12321).不能使 ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
- 【LeetCode 234】Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 用快慢指针找到链表中点,反转后半部分链表,然后与前半部分进行匹配,随后 ...
- 【LeetCode #179】Largest Number 解题报告
原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...
- 【leetcode❤python】191. Number of 1 Bits
#-*- coding: UTF-8 -*- class Solution(object): def hammingWeight(self, n): if n<=0:retu ...
随机推荐
- 转:maven2创建一个eclipse工程,设置M2_REPO
from: http://tonychanhoho.iteye.com/blog/1584324 M2_REPO是一个用来定义 maven 2仓库在硬盘中的存储位置,windows默认是C:\User ...
- VC++动态链接库(DLL)编程深入浅出(一)
1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了“无库-静 ...
- mysql数据库解决中文乱码问题
安装mysql之后.假设存储中文.再读出的时候就会出现乱码问题. 如今的字符集有几百种之多,都是一些公司或者组织定义的. 我们应该使用可以容纳世界所有语言所有字符的字符集,这样就不会再出现乱码问题. ...
- 使用rabbitmq rpc 模式
服务器端 安装 ubuntu 16.04 server 安装 rabbitmq-server 设置 apt 源 curl -s https://packagecloud ...
- UNP学习笔记(第十一章 名字与地址转换)
域名系统 域名系统(Domain Name System,DNS)主要用于主机名字与IP地址之间的映射. 主机名既可以是一个简单得名字,如solaris,也可以是一个全限定域名,如solaris.un ...
- vim diff 的使用
vimdiff 是vim的比较工具可以对两个文件进行差异比较和快速合并 1. 使用vimdiff 比较两个文件 方式一 vimdiff file_left file_right 或者 vim ...
- 介绍JSON
0x00 介绍JSON 介绍JSON :http://www.json.org/json-zh.html Introducing JSON :http://www.json.org/
- MVC初了解
MVC:Model-View-Controller,将数据和显示形式分离. Model:能够看做是三层中的D层+B层,实现业务逻辑和与数据库的交互. View:看做是U层,用来显示数据. Contro ...
- 串口设置-stty--设置终端线
stty - chang and print terminal line settings SYNOPSIS stty [-F DEVICE | --file=DEVICE] [SETTING]... ...
- Jquery EasyUI弹出窗体
$("#btnCreate").click(function () { $("#modalwindow").html("<iframe widt ...