LeetCode7~9 Reverse Integer/String to Integer (atoi)/Palindrome Number

一:Reverse Integer

题目:

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

链接:https://leetcode.com/problems/reverse-integer/

分析:这题通过不断取余将余数存放在一个vector中,然后乘以相应的10^i次方相加即可,这里主要考虑是否overflow,因此将result设为long long int,当大于int的最大值0x7fffffff或者小于int的最小值0x800000000时,则返回0,否则返回result。

代码:

#define maxInt (int)0x7fffffff       // int中最大值==2147483647     unsigned int 最大值为4294967295
#define minInt (int)0x80000000      // int中最小值2147483648
class Solution {
public:
    int reverse(int x) {
        vector<int> v;
        while(x != 0){
            int a = x%10;
            v.push_back(a);
            x = x/10;
        }
        long long int result = 0;

        for(int i = 0; i < v.size(); i++){
            result += v[i]*pow(10.0,v.size()- i - 1);
        }
        if(result > maxInt || result < minInt) return 0;     // 通过long long int来解决溢出问题
        return result;

    }
};

二:String to Integer (atoi)

题目:Implement atoi to
convert a string to an integer.

链接:https://leetcode.com/problems/string-to-integer-atoi/

分析:这道题不难,关键是满足的测试样例比较变态,比如“     123456ab78”,也要返回123456等等

代码:

#define Max (int)0x7fffffff
#define Min (int)0x80000000
class Solution {
public:
    int atoi(string str) {
        if(str.size() <= 0) return 0;

        for(int i = 0; i < str.size(); i++){   // 去除前面的空格
            if(str[i] != ' '){
				str = str.substr(i);
				break;
			}
        }
		int c = 1;
		int len = str.size();
		if(isdigit(str[0])) {       // 如果第一个为数字 表示整型 则前面放一个+号
            str = "+" + str;
            len = len +1;
        }
        if(str[0] == '+') c = 1;
        else if(str[0] == '-')c = -1;
        else return 0;

		for(int i = 1; i < len; i++){  //去除后面字母开头的所有字符
			if(!isdigit(str[i]) || i >= 12) {   //超过了12位的不要了 否则有可能会超过long long 的范围
				str = str.substr(0, i);
				break;
			}
		}
		len = str.size();

        long long int result = 0;;

        for(int i = 1; i < len; i++){
            if(!isdigit(str[i]))return 0;
            stringstream ss;
            int temp;
            ss << str[i];
            ss >> temp;
            result += temp * pow(10.0, len-i-1.0)*c;
        }
        if(result > Max) return Max;
		if(result < Min) return Min;
        return result;
    }
};

三:Palindrome Number

题目:

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

链接:

https://leetcode.com/problems/palindrome-number/

分析:这题主要有几个限制:1不能用extra space,所以不能转换成string再求回文  2:先求reverse integer,那么溢出怎么办? 所以这题是先求出int的位数,然后分别取出最低位和最高位进行比较。

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        int d = 0;
        int y = x;
        while(y != 0){    // 先将x的位数求出来
            y = y/10;
            d++;
        }
        while(x != 0){
            int lowBit = x % 10;   // 求出x的最低位
            int highBit = x / pow(10.0, d-1.0);   // 求出x的最高位
            if(lowBit != highBit) return false;
            x = (x - lowBit - highBit * pow(10.0, d-1.0))/10;
            d = d - 2;
        }
        return true;

    }
};
时间: 2024-10-28 08:54:28

LeetCode7~9 Reverse Integer/String to Integer (atoi)/Palindrome Number的相关文章

Leetcode 题目整理-2 Reverse Integer &amp;&amp; String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. 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

【LeetCode7】Reverse Integer★

题目描述: 解题思路: 反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法. Java代码: 方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后,紧接着进行逆运算result=(newResult-tail)/10,如果出现溢出,那么逆运算后result和newResult必然不相等,反之,如果没有溢出,则逆运算后result=newResult. 1 public class LeetCode7 { 2 public static void

leetcode第八题--String to Integer (atoi)

Problem: 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 possible input cases. Notes: It is intended for this problem

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

【LeedCode】String to integer(atoi)

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 possible input cases. Notes: It is intended for this problem to be spe

[leetcode]经典算法题- String to Integer (atoi)

题目描述: 把字符串转化为整数值 原文描述: 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 possible input cases. Notes: It is intended for

【leetcode】String to Integer (atoi)

String to Integer (atoi) 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 possible input cases. Notes: It is intended f

[算法]String to Integer(atoi)

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 possible input cases. Analysis The following cases should be considere