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

思考:位运算。AC的时候真的想说一句“尼玛。。“,用unsigned int 超时,莫名其妙,折腾到半夜,百度一下改为long long过了。。我也不明白到底怎么回事,毕竟不是CS出身,哎。。太晚了,明天再看。

VC不支持long long,珍爱生命,远离VC。

class Solution {
public:
int divide(int dividend, int divisor) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
assert(divisor!=0);
if(dividend==0) return 0;
bool flag=true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0)) flag=false;
long long c=dividend;
long long d=divisor;
long long a=abs(c);
long long b=abs(d);
long long ret=0;
long long count=1;
while(a!=0)
{
b=abs(d);
if(a<b)
{
a=0;
count=0;
break;
}
count=1;
while(b<a)
{
b<<=1;
count<<=1;
}
if(b!=a)
{
b>>=1;
count>>=1;
}
a-=b;
ret+=count;
}
return (flag)?ret:-ret;
}
};

  

[LeetCode]Divide Two Integer的更多相关文章

  1. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  2. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  3. LeetCode: Divide Two Integers 解题报告

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

  4. Leetcode:Divide Two Integers分析和实现

    题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...

  5. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  6. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  8. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  9. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

随机推荐

  1. asp.net字符串分割函数用法

    先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我要根据[111cn.net]分割的 ...

  2. JS正则与.net正则表达式的区别

    原字符串:长期投资人月井石问华菱钢铁(000932)公司反复强调VAMA的战略意义,请问在2015年4季度达产后对公司贡献的盈利预期 正则匹配处理后:公司反复强调VAMA的战略意义,请问在2015年4 ...

  3. Swift类型检查与转换

    继承会发生在子类和父类中,如图所示,是一系列类的继承关系类图,Person是类层次结构中的根类,Student是Person的直接子类,Worker是Person的直接子类.这个继承关系类图的具体实现 ...

  4. 暑假集训(5)第一弹——— Super Jumping! Jumping! Jumping!(hdu1087)

    题意概括:在上次与娑殚的三次博弈中,你们都取得了胜利.便向娑殚提出要求,借助他的力量,传送到一个安全的地方. 你们的愿望达成了,不过,你和小A似乎失散了. 街上人来人往的特别热闹,每一个人的脸上都洋溢 ...

  5. Linux进程调度

    原文地址: http://cchxm1978.blog.163.com/blog/static/35428253201092910491682/ 相当不错的文章,读了后收藏,多谢博主分享! ----- ...

  6. Mac OS X开发者准备工作

    迁移到Mac平台做开发后,需要有一系列的准备工作来使我们的工作更顺畅. 1. 安装Homebrew包管理器 苹果系统自带了一个包管理器,但是并不是很好用.现在,现在比较流行的是Homebrew,非常好 ...

  7. ListView 复制到剪切板

    private void 导出ToolStripMenuItem_Click(object sender, EventArgs e) { Clipboard.SetText(GetListView(l ...

  8. html5学习笔记——2016/4

    HTML5新增的结构元素: section     article     aside     header     hgroup     footer     nav     figure HTML ...

  9. 如何使用Git——(一)

    一.git与github git 是一款自由和开源的分布式版本控制系统,用于敏捷高效地处理任何或大或小的项目. github 是一个网站,给用户提供git仓库托管服务,是开源代码库以及版本控制系统.在 ...

  10. ldd查看可执行程序依赖的文件

    ldd 用于查看可执行程序依赖的so动态链接库文件 [root@localhost ld.so.conf.d]# ldd /usr/local/tengine/sbin/nginx linux-vds ...