码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode:Divide Two Integers

时间:2015-04-03 23:55:31      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:leetcode

class Solution {
public:
    int divide(int dividend, int divisor) {
        long long div = dividend,dis = divisor;
        div = abs(div);
        dis = abs(dis);
        long long res = 0;
        while(div>=dis)
        {
            long long temp = dis;
            int i = 0;
            while(div>=temp)
            {
                temp = temp<<1;
                i++;
            }
            res += ((long long)1<<(i-1));
            div -= (dis<<(i-1));
        }
        res = ((long long)dividend>0^(long long)divisor>0)?-res:res;
        if(res>((1<<31)-1))
            return (1<<31)-1;
        return res;
    }
};

leetcode:Divide Two Integers

标签:leetcode

原文地址:http://blog.csdn.net/majing19921103/article/details/44862875

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!