Math

7. Reverse Integer


Reverse digits of an integer.

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

数学题,需要考虑一些细节。1.对负数的处理;2.取反后溢出的处理。

class Solution {
public:
    int reverse(int x) {
        int z=0,y=abs(x);
        while(y>0){
            if(z!=0 && INT_MAX/z<10 && INT_MAX/z>-10)
                return 0;
            z=z*10+y%10;
            y=y/10;
        }
        if(x>0) return z;
        return -z;
    }
};


8. 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 specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

将字符串转换为数字,同样要考虑一些细节。1.串首空格去除;2.正负号处理;3.溢出处理。

class Solution {
public:
    int myAtoi(string str) {
        if(str==""){
            return 0;
        }
        int ret=0,i=0;
        for(;isspace(str[i]);i++);
        bool neg=false;
        if(str[i]=='-' || str[i]=='+'){
            neg=(str[i]=='-');
            i++;
        }
        for(;isdigit(str[i]);i++){
            int digit=(str[i]-'0');
            if(neg){
                if(-ret<(INT_MIN+digit)/10){
                    return INT_MIN;
                }
            }else{
                if(ret>(INT_MAX-digit)/10){
                    return INT_MAX;
                }
            }
            ret=10*ret+digit;
        }
        return neg?-ret:ret;
    }
};


9. Palindrome Number


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

要求不使用额外的空间,所以不适合将数字反转再比较。考虑每次比较数字首位和末位是否相等,再将去掉首位和末位的值赋给x,直到x为0时停止。

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0){
            return false;
        }
        int len=1;
        for(;(x/len)>=10;len*=10);
        while(x!=0){
            int left=x/len;
            int right=x%10;
            if(left!=right){
                return false;
            }
            x=(x%len)/10;
            len/=100;
        }
        return true;
    }
};


12. Integer to Roman


Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

将整数转换为罗马数字,考虑从较高位开始转换,每次从num中减去一个当前可转换的最大数字,并将其转换为罗马数字加入到结果中。

class Solution {
public:
    string intToRoman(int num){
        string result="";
        string symbol[] =   {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};    
        int value[]     =   {1000,900,500,400, 100, 90,  50, 40,  10, 9,   5,  4,   1}; 
        int i=0;
        while(num>0){
            while(num>=value[i]){
                result+=symbol[i];
                num-=value[i];
            };
            i++;
        };
        return result;
    }
};


13. Roman to Integer


Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

将罗马数字转换为整数,需要注意'IX','IV'这种两个字母组成的罗马数字,其值等于右边字母代表的值减去左边字母代表的值。

class Solution {
public:
    int romanCharToInt(char ch){
        int d=0;
        switch(ch){
            case'I':d=1;break;
            case'V':d=5;break;
            case'X':d=10;break;
            case'L':d=50;break;
            case'C':d=100;break;
            case'D':d=500;break;
            case'M':d=1000;break;
        }
        return d;
    }
    int romanToInt(string s) {
        if(s.size()<=0) return 0;
        int result=romanCharToInt(s[0]);
        for(int i=1;i<s.size();i++){
            int pre=romanCharToInt(s[i-1]);
            int cur=romanCharToInt(s[i]);
            if(pre<cur){
                result=result-pre+(cur-pre);
            }else{
                result=result+cur;
            }
        }
        return result;
    }
};


29. Divide Two Integers


Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

数学题,不使用乘除和求余,实现除法运算。需要使用二分法思路,否则会超时。每次从被除数中减去除数的2^n倍,直到被除数减为0为止,n尽量大。例如,27除以3,第一次27-3*2^3=3,第二次3-3*2^0=0。

class Solution {
public:
    int divide(int dividend, int divisor) {
        int sign=1;
        if(dividend<0) sign=-sign;
        if(divisor<0) sign=-sign;
        unsigned long c=1;
        unsigned long long temp=abs((long long)dividend);
        unsigned long long temp2=abs((long long)divisor);
        if(dividend==-2147483648 && divisor==-1) return 2147483647;
        while(temp>temp2){
            temp2=temp2<<1;
            c=c<<1;
        }
        int result=0;
        while(temp>=abs((long long) divisor)){
            if(temp>=temp2){
                temp=temp-temp2;
                result=result+c;
            }
            temp2=temp2>>1;
            c=c>>1;
        }
        return sign*result;
    }
};

50. Pow(x, n)


Implement pow(xn).

实现乘方运算。为了保证运行时间,要用到二分的思想。

class Solution {
public:
    double myPow(double x, int n) {
        bool sign=false;
        unsigned int exp=n;
        if(n<0){
            exp=-n;
            sign=true;
        }
        double result=1.0;
        while(exp){
            if(exp & 1){
                result*=x;
            }
            exp>>=1;
            x*=x;
        }
        return sign?1/result:result;
    }
};







“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值