package javaLeetCode_primary;

import java.util.Scanner;
/**
* Given a 32-bit signed integer, reverse digits of an integer. <b>Example 1:
* <li>Input: 123
* <li>Output: 321 Example 2:
* <li>Input: -123
* <li>Output: -32 Example 3:
* <li>Input: 120
* <li>Output: 21
*/ /*
* Test data:
*1534236469->0
*999999991->199999999
*2147483647->0
*2147483642 ->0
*1000000000->1
*-2147483648->0
*-214748364->-463847412
*901000-> 109
*/
public class ReverseInteger_7 {
public static void main(String[] args) { @SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Please input a integer:");
int x = input.nextInt();
System.out.println(reverse_2(x));
}// end main /**
* Use simple methods to solve problems.
*/
public static int reverse_1(int x) { int[] arr = new int[10];// Stores the number of bits of a parameter
int countDigit = 0;// Calculate the length of the number
int cf = 0;// Leave the parameter state
int i = 0;
// The element that initializes array is 0.
for (i = 0; i < arr.length; i++) {
arr[i] = 0;
} // end for if (x < 0) {
cf = 1;
x *= -1;
} // end if //Get the the digit of parameter and reverse/invert it.
i = 0;
while (x > 0) {
countDigit++;
arr[i] = x % 10;
x /= 10;
i++;
} // end while // Print the integer
i = 0;
long y = 0;
for (; i < countDigit; i++) {
y = y * 10 + arr[i];
} // end for //Judge whether the integer is valid.
if(y>2147483647) {
x = 0;
}else {
x = (int)y;
}//end if if (cf == 1) {
x *= -1;
} // end if return x; }// end reverse() /**
* Answer online
* */
public static int reverse_2(int x) {
int rev = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
return rev;
}//end reverse()
}

最新文章

  1. 设置table距离顶部位置
  2. VS XML注释
  3. js 点击复制内容
  4. 访问IIS元数据库失败解决方法
  5. chown命令
  6. Android_安装GooglePlay
  7. 《OD学hadoop》第一周0626 作业二:Linux基础
  8. 2015影响因子Excel版
  9. AQTime教程(1)
  10. 解决DataGridView.DataSource重复赋值而不显示问题
  11. 【解决方法】You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE)
  12. Eight
  13. 【Excel】将IP按照IP地址(v4)增长序列排序
  14. 使用@property - 廖雪峰的官方网站
  15. python接口自动化测试二十五:执行所有用例,并生成HTML测试报告
  16. Linux TCP/IP调优-Linux内核参数注释
  17. SharePoint JavaScript API 根据文件路径删除文件
  18. Android WiFi 日志记录(ASSOC_REJECT)
  19. iOS计算两个时间的时间差
  20. python设计模式之单例模式(一)

热门文章

  1. 【hdu5100】棋盘覆盖
  2. springboot+vue前后端免费开源
  3. 12个让您震撼的Linux终端命令
  4. 【Python代码】TSNE高维数据降维可视化工具 + python实现
  5. MySQL索引及查询优化
  6. 01.drf文档及外键字段反序列化
  7. zabbix 自定义指标数据来源
  8. Java并发包4--可重入锁ReentrantLock的实现原理
  9. PAT-1080 Graduate Admission (结构体排序)
  10. PHP 面向对象的数据库操作