【leetcode】7. Reverse Integer

题目描述:

Reverse digits of an integer.

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

解题思路:

这道题比较简单,只要注意两个问题:1,输入可能有123,-123两种情况。2,可能会出现值溢出的情况,所以先用long类型处理,决定没有溢出后再转换为int

具体代码:

 1 public static int reverse(int x) {
 2         String s =""+x;
 3         char[] array=s.toCharArray();
 4         if(array[0]==‘-‘){
 5             reverse(array,1);
 6             Long num=Long.valueOf(new String(array));
 7             if(num<Integer.MIN_VALUE){
 8                 return 0;
 9             }
10             return Integer.valueOf(new String(array));
11
12         }
13         else{
14             reverse(array,0);
15             Long num=Long.valueOf(new String(array));
16             if(num>Integer.MAX_VALUE){
17                 return 0;
18             }
19             return Integer.valueOf(new String(array));
20         }
21
22    }
23    public static void reverse(char[] array,int from){
24        int to = array.length-1;
25        while(from<to){
26            char ch=array[from];
27            array[from]=array[to];
28            array[to]=ch;
29            from++;
30            to--;
31        }
32    }
时间: 2024-10-20 11:22:15

【leetcode】7. Reverse Integer的相关文章

【Leetcode】 #7 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 测试用例: 123………………321 -123………………-321 1200………………21 45134543545…………0(overflow) 2147483646………………0(虽然它小于最大值2147483647,但是调转过来之后溢出!) 最后两个陷阱是不容易发现的. int类型的最大值是2147483647

【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