【Leetcode】 #7 Reverse Integer

Reverse digits of an integer.

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

测试用例:

  • 123………………321
  • -123………………-321
  • 1200………………21
  • 45134543545…………0(overflow)
  • 2147483646………………0(虽然它小于最大值2147483647,但是调转过来之后溢出!)

最后两个陷阱是不容易发现的。

int类型的最大值是2147483647。

下面是代码:

 1 public static int reverse(int x) {
 2         boolean isNegative=false;
 3         if(x<0){
 4             isNegative=true;
 5             x*=-1;
 6         }
 7         if(x>0x7FFFFFFF)
 8             return 0;
 9         int result=0;
10         boolean bigin=false;
11         int y=0;
12         while(x>0){
13             y=x%10;
14             x/=10;
15             if(y!=0){
16                 bigin=true;
17             }
18             if(bigin){
19                 if(result>214748364||(result==214748364&&y>7))   //防止调转过来溢出
20                     return 0;
21                 result*=10;
22                 result+=y;
23             }
24         }
25         if(isNegative)
26             result*=-1;
27         return result;
28     }
时间: 2024-12-13 20:43:02

【Leetcode】 #7 Reverse Integer的相关文章

【leetcode】7. Reverse Integer

题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 这道题比较简单,只要注意两个问题:1,输入可能有123,-123两种情况.2,可能会出现值溢出的情况,所以先用long类型处理,决定没有溢出后再转换为int 具体代码: 1 public static int reverse(int x) { 2 String s =""+x; 3

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

【LeetCode】Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]

【LeetCode】String to Integer (atoi) 解题报告

这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the pos

【LeetCode】String to Integer (atoi) 解题报告 (Java)

这道题在LeetCode OJ上难道属于Easy,但是通过率却比较低,究其原因是需要考虑的情况比较低,很少有人一遍过吧. [题目] Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the poss

【LeetCode】190. Reverse Bits

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). Follow up:If this function i

【Leetcode】Evaluate Reverse Polish Notation JAVA

   一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

【LeetCode】345. Reverse Vowels of a String 解题小结

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 应该算不上有难度. class Solution { p