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. Jquery 模糊匹配ID

    [属性名称] 匹配包含给定属性的元素[att=value] 匹配包含给定属性的元素 (大小写区分)[att*=value] 模糊匹配[att!=value] 不能是这个值[att$=value] 结尾 ...

  2. iOS开发——打包ipa

    首先,保证设备证书和配置文件的正确,Xcode上登陆好自己公司的账号Apple ID 1.选中运行模拟器的位置为硬件设备 2.点击导航栏上的[Product]——[Archive]后编译后弹出如下界面 ...

  3. 禁用iOS9 App Transport Security(ATS)特性时不起作用

    iOS 9发布后,原来开发的iPad应用在iOS9下面测试时,协议使用的是HTTP,发送网络请求时,Console窗口输出: App Transport Security has blocked a ...

  4. 使用C#通过调用minitab的COM库自动化生成报表

    本文介绍通过C#调用minitab com组建自动化生成报表的方法. 首先需要在minitab中通过手动配置的方式生成报表来得到该报表的命令行,过程如下 选择菜单“编辑器”->“启用命令”启用命 ...

  5. 安全接口 interface --显示实现接口

    前言:当我们定义接口的成员的时候不需要写访问控制符,因为它是默认public的,也只能是public.当一个类要实现这个接口的时候,自然要公开其成员.一直以来我都这么做. interface Inte ...

  6. H5API——Canvas

    http://item.jd.com/11241807.html HTML5移动Web开发实战http://item.jd.com/10982275.html HTML5程序设计(第2版)http:/ ...

  7. CSS样式三

    CSS表格样式 border-collapse:表格边线合并 caption-side: 属性值: top:设置表格的标题在表格的上方(默认效果) bottom:设置表格的标题在表格的下方 样式代码: ...

  8. 通过命令行连接oracle数据库/进入sql plus

    1.直接打开SQL Plus登录,需要用户名和密码 开始->程序->Oracle->应用程序开发->sqlplus 我的电脑是“开始”->“Oracle - OraDb1 ...

  9. 菜鸟级asp.net 与ms sql server数据库打交道的简单总结

    using System.Data.SqlClient;using System.Data; 上面是必须的 下面说的都是用存储过程 首先是webconfig里面的连接字符串: <connecti ...

  10. C#让TopMost窗体弹出并置顶层但不获取当前输入焦点的终极办法

    为了使程序在弹出窗口时置顶层且不获取系统输入焦点,避免影响用户当前的操作,来电通来电弹屏软件尝试过N多种办法,例如:弹出前保存当前焦点窗口句柄,弹出时因为使用TopMost系统默认将焦点交给了弹出窗口 ...