Java实现LeetCode_0009_PalindromeNumber
package javaLeetCode_primary;
import java.util.Scanner;
import java.util.Stack;
public class PalindromeNumber_9 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Please input the integer:");
int x=input.nextInt();
System.out.println(isPalindrome_3(x));
}// end main()
/**
* Data is stored on a stack.
*/
/*
* Test data:
* 121
* 1221
* -121
* 10
* 1221
* */
public static boolean isPalindrome_1(int x) {
String str = Integer.toString(x);
char[] cha = str.toCharArray();
Stack <Character>stack = new Stack<Character>();
boolean isPalindrome = true;
for(int i=0;i<str.length()/2;i++) {
stack.push(cha[i]);
}//end for
int temp=0;
if(str.length()%2==0) {
temp= str.length()/2;
}else {
temp = str.length()/2+1;
}//end if
for(int i=temp;i<str.length();i++) {
if(stack.pop()==cha[i]) {
isPalindrome = true;
}else {
isPalindrome = false;
break;
}
}//end for
return isPalindrome;
}// end isPalindrome()
/**
* Use the Conventional thinking.
* */
public static boolean isPalindrome_2(int x) {
String str = Integer.toString(x);
char[] cha = str.toCharArray();
boolean isPalindrome = true;
int temp = str.length();
for(int i=0;i<temp/2;i++) {
if(cha[i]==cha[temp-i-1]) {
isPalindrome = true;
}else {
isPalindrome = false;
break;
}//end if
}//end for
return isPalindrome;
}// end isPalindrome()
/**
* Answer online
* Use the MathMetical method.
* */
public static boolean isPalindrome_3(int x) {
boolean isPalindrome = false;
int temp = 0;
if(x < 0 || (x % 10 == 0 && x != 0)) {
return isPalindrome;
}//end if
while(temp<x) {
temp = (temp*10)+ (x%10);
x /= 10;
}//end while()
if(temp/10==x||temp == x)
isPalindrome = true;
return isPalindrome;
}// end isPalindrome()
}
Java实现LeetCode_0009_PalindromeNumber的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- [hdu5348]图上找环,删环
http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...
- [zoj 3416/hdu 3709]数位DP
题意:求从区间[L, R]内有多少个数是平衡数,平衡数是指以10进制的某一位为中心轴,左右两边的每一位到中心轴的距离乘上数位上的值的和相等.0<=L<=R<=1e18 思路:由于任何 ...
- 使用plupload实现多文件上传,自定义参数
下载地址:点击打开链接 1.在开发中可能需要用户附件上传的功能,实现批量上传功能其实就将多个上传任务放到一个集合中,分别上传. 2,使用plupload js插件可以很轻松的实现带参数的多文件上传 3 ...
- 接口testing简介
一.基础介绍 1.什么是接口 我们常说的接口一般指2种1)API:应用程序编程接口 2)GUI:图形用户界面(接口) 这里我们主要说API——接口测试 2.接口测试的目的 测试接口的正确性和稳定性 ...
- 论文阅读:Reducing Transformer Depth On Demand With Structured Dropout
Introduction 这篇paper是做Transformer压缩的,但其实bert的核心也就是transformer,这篇paper的实验里也做了bert的压缩.作者的主要工作是提出了Layer ...
- Lowest Common Multiple Plus(hdu2028)
思考: 乘法爆咋数据.把int换成unsigned就过了,同时%d换成%u.求最大公约数和最小公倍数. #include<stdio.h> int gcd(unsigned x, unsi ...
- vue中template的三种写法
第一种(使用模板字符串)早期字符串拼接年代 <div id="app"></div> new Vue({ el: "#app", tem ...
- WordPress 获取文章内容页特色图像地址
WordPress获取特色图像地址主要需要用到两个函数get_post_thumbnail_id和wp_get_attachment_image_src.下面是分别获取小.中.大.完整.指定图片规格的 ...
- web自动化之执行js脚本
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...
- JavaScript的流程控制语句以及函数
一.流程控制 1. 作用:控制代码的执行顺序 2. 分类 2.1顺序结构:从上到下依次执行代码语句 2.2选择结构: 1. if语句 简单if结构 if(条件表达式){ 表达式成立时执行的代码段 } ...