【LeetCode】007 Reverse Interger

题目:LeetCode 007 Reverse Interger

题意:将一个整数的数字反转。保留正负符号。

思路:先将整数变成字符串,然后判断是否为负数,或是否含有’+’,然后从字符串末尾开始累计得到新整数即可。

但是还会有特殊情况,即正向为Int范围内,但反转之后会溢出,因此要进行特判。

代码如下:

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         int len, flag = 1, i = 0;
 5         long long ans = 0;
 6         string s = to_string(x);
 7
 8         len = s.size();
 9         len--;
10         if(s[i] == ‘-‘)
11         {
12             flag = -1;
13             i++;
14         }
15         // else if(s[i] == ‘+‘) i++;
16
17         while(len >= i)
18         {
19             ans *= 10;
20             ans += s[len--]-‘0‘;
21         }
22         ans *= flag;
23         //cout << ans << endl;
24         // 返回值根据预设的函数类型自动变,Longlong也会自动按int溢出处理
25         if(ans > INT_MAX || ans < INT_MIN) return 0;
26         return ans;
27     }
28 };
时间: 2024-10-12 16:04:20

【LeetCode】007 Reverse Interger的相关文章

【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】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

【leetcode】25. Reverse Nodes in k-Group

题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, on

【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】 #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】92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【leetcode】Evaluate Reverse Polish Notation(middle)

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", "*"] -&g