7_Reverse Integer
7.Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321Example 2:
Input: -123
Output: -321Example 3:
Input: 120
Output: 21Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
没有想出来, 仿照(或者照搬?)http://www.cnblogs.com/grandyang/p/4125588.html来写
注意点:
反转溢出问题, 解决方法: 使用比该数字范围大的类型定义返回值
版本一: 拆分每个位的思想是我的, 其他细节参考上述博客
class Solution {
public:
int reverse(int x) {
long long res = 0;
vector<int> bit;
while (0 != x) {
bit.push_back(x % 10);
x /= 10;
}
for (int i = 0; i < bit.size(); i++) {
res =res * 10 + bit[i];
}
return (res < INT_MIN || res > INT_MAX) ? 0 : res;
}
};
版本二: 半个自己写的, 考虑符号, 虽然多余了
class Solution {
public:
int reverse(int x) {
long long ret = 0;
// int positive = 1;
long long positive = 1;
if (x < 0) {
positive = -1;
x *= positive;
}
while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
}
ret *= positive;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
class Solution {
public:
int reverse(int x) {
long long int ret = 0;
while(0 != x) {
ret = ret*10 + x%10;
x /= 10;
}
//return (ret > INT_MIN || ret < INT_MAX)? ret : 0;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
7_Reverse Integer的更多相关文章
- 9_Palindrome Number
9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- 077 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 02 类和对象
077 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 02 类和对象 本文知识点:类和对象 说明:因为时间紧张,本人写博客过程中只是对知识点 ...
- nessus 本地扫描(一)
第一次使用nessus ,so 适合小白看看 1.新建扫描.配置策略:起个名字,description是详细记录,类似于说明:targets是要访问的主机ip地址或者网段,必填项 选择好之后sav ...
- CentOS 7 系统的安装
1.进入安装界面 2.选择"Install CentOS 7" 3.进入欢迎界面,默认语言为"English",点击"Continue" 进 ...
- OAth 2.0 的白话讲解
一.OAuth2.0是什么,主要做什么用的? 官方注解 简单说,OAuth 就是一种授权机制.数据的所有者告诉系统,同意授权第三方应用进入系统,获取这些数据.系统从而产生一个短期的进入令牌(token ...
- day43 Pyhton 并发编程06
一.内容回顾 线程 锁 为什么有了GIL之后还需要锁 多个线程同时操作全局变量还需要锁 当出现'非原子性操作',例如+= -= *= /= l.append(l) 原子性操作 a += 1 a= a ...
- JS中实现Trim(),TrimStart(),TrimEnd() 的方法
//去除字符串头尾空格或指定字符 String.prototype.Trim = function (c) { if (c == null || c == "") { var st ...
- Git软件操作过程
一.下载 Git 二.下载Git小乌龟-TortoiseGit 三.汉化-去官网下载,官网地址 https://tortoisegit.org/download/
- C++11——chrono库开发高精度计!我们可能学的不是同一门语言~
一.前言 在我们写程序过程中,有时候需要测试我们的程序语句执行时间的耗时,当前也是有很多的库提供我们去使用,一直没有良好的跨平台的库可以提供出来:而且一般这种代码也是由我们程序员自己调用系统的库来进行 ...
- 置Hugo的代码高亮
+++ date="2020-10-17" title="设置Hugo的代码高亮" tags=["hugo"] categories=[&q ...
- php闭包作为函数参数
<?php function test(Closure $call) { $a = 63; $b = 22; echo "hello"; echo $call($a,$b); ...