Reverse Integer 旋转数字

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

本地注意正负号判断比较关键,实现部分可能不是最优的,按照自己的想法实现:

设ret = 1;每次对x进行取余mod,然后ret = ret*10+mod;(第一次让ret = mod),最后返回ret

代码如下:

 1 public class Solution {
 2     public int reverse(int x) {
 3         int result = 0;
 4         int flag = (x < 0) ? 1 : 0;
 5
 6         x = (x < 0) ? -x : x;
 7
 8         int first = 1;
 9         while(x!=0){
10             int mod = x%10;
11             if(first == 1){
12                 result = mod;
13                 first = 0;
14             }else{
15                 result *= 10;
16                 result += mod;
17             }
18
19             x /= 10;
20
21         }
22
23         return (flag == 1) ? -result : result;
24
25     }
26 }
时间: 2024-10-17 00:13:13

Reverse Integer 旋转数字的相关文章

LeetCode:Reverse Integer - 翻转数字

1.题目名称 Reverse Integer(翻转数字) 2.题目地址 https://leetcode.com/problems/reverse-integer/ 3.题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形成一个新数字 例如:x = 123, return 321:x = -123, return -321 4.一个有瑕疵的方法(不能AC) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

Leetcode 7 Reverse Integer 反转数字

题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 翻译就是把一个数字反过来输出. 解题思路:这道题目看起来比较简单.无非就是一个数字取个位,作为另一个高位.无非是在10的运算. 代码1: public static int reverse2(int x) { int rev = 0; boolean flag = false;//正负数 if(x < 0)

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign

LeetCode 7 Reverse Integer(反转数字)

题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 其实这道题看起来非常简单,要实现也是几行代码的事.但是有个小问题容易被忽略,就是边界问题.什么意思呢?如果我们输入的整数超出了int的表达范围,这个问题要怎么解决呢? 用比int更大的数据类型存储我们转

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

LeetCode #7 Reverse Integer (E)

[Problem] Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 [Analysis] 这题不难,关键是要注意overflow的处理,因为reverse后的数字有可能超出Integer的范围.有两种思路:一种是利用对10取模的运算,每次将最后一位加入结果并将结果乘10:一种是利用Java StringBuilder的reverse函数以及Integer的pars

LeetCode——Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer'

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

[LeetCode] Reverse Integer [8]

题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought th