LintCode刷题小记491
题目:
判断一个正整数是不是回文数。
回文数的定义是,将这个数反转之后,得到的数仍然是同一个数。
样例:
11, 121, 1, 12321 这些是回文数。
23, 32, 1232 这些不是回文数。
分析:
回文数就是反转后和自身一样,可利用java中StringBuffer中reverse()这一函数进行操作。
下面给出代码:
public class Solution {
/**
* @param num a positive number
* @return true if it's a palindrome or false
*/
public boolean palindromeNumber(int num) {
String str = Integer.toString(num); //将int型转为String型
StringBuffer sb = new StringBuffer(str); //构造StringBuffer对象
boolean x = false;
if(sb.reverse().toString().equals(str)){ //sb对象反转变为String对象再与原字符串比较
x = true;
}
else {
x = false;
}
return x;
}
}
LintCode刷题小记491的更多相关文章
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- Atcoder刷题小记
1. 2019.4.27 agc016d 一道很坑的题. 首先判无解,求出异或值后排个序就可以. 然后直接让\(a_i\rightarrow b_i\)并查集维护,注意离散化和判重,答案加上联通块个数 ...
- lintcode 刷题 by python 部分链表题总结(2)
本篇博客对最近做的链表的算法题做个简单的小结,主要描述题目和提供解题思路,具体代码见我的 github:https://github.com/MUSK1881/lintcode-by-python 3 ...
- LintCode刷题指南:字符串处理(C++,Python)
题目:两个字符串是变位词 题目难度:简单 题目描述: 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 解题思路: C++:引入哈希的思维,这道题就 ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
随机推荐
- C语言字符串拼接
1.使用strcat进行字符串拼接 #include <stdio.h> #include <stdlib.h> #include <string.h> int m ...
- ajax-2
serialize()输出序列化表单值的结果: 如: <html> <head> <script type="text/javascript" src ...
- rest_framwork中ApiView实现分页
from rest_framework.pagination import PageNumberPagination from .serializers import BookSerilizer fr ...
- 如何理解java采用Unicode编码
http://blog.csdn.net/gjb724332682/article/details/43229563 Java中字符仅以一种形式存在,那就是Unicode.由于java采用unicod ...
- Fundamentals of Logic
Fundamentals of Logic To make complicated mathematical relationships clear,it is convenient to use t ...
- ThinkPHP U方法
方法1: {:U('User/Booking/bookingdetails')} 方法2: {:U('User/Booking/bookingdetails')}"+"&a ...
- error while loading shared libraies :libopencv_core_so.3.4:cannot open shared object
TX2 上安装自己编译的opencv,使用时出现: error while loading shared libraies :libopencv_core_so.3.4:cannot open sha ...
- AR资料汇总学习
ARKit 从入门到精通 http://www.chinaar.com/ARKit/5210.html ARKit 框架的学习 http://blog.csdn.net/biangabiang/art ...
- win7/win8/win10 系统
WIN7/WIN8/WIN10 系统安装 http://www.windows7en.com/Win7/18572.html
- SPOJ - FREQ2 莫队 / n^1.5logn爆炸
题意:给定\(a[1...n]\)和\(Q\)次询问,每次统计\([L,R]\)范围内出现频率最高的数的次数 想法没啥好说的,分别统计该数出现的次数和次数出现的次数,然后莫队暴力 注意本题时间卡的很紧 ...