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

分析:题目意思很容易理解,就是不用乘除法和模运算求来做除法,很容易想到的一个方法是一直做减法,然后计数,超时。在网上找到一种解法,利用位运算,意思是任何一个整数可以表示成以2的幂为底的一组基的线性组合,即num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n。基于以上这个公式以及左移一位相当于乘以2,我们先让除数左移直到大于被除数之前得到一个最大的基n的值,说明被除数中至少包含2^n个除数,然后减去这个基数,再依次找到n-1,...,1的值。将所有的基数相加即可得到结果。具体代码如下:

class Solution {
public:
int divide(int dividend, int divisor) {
int res = ;
int flag = ;
if((dividend< && divisor>) || (dividend> && divisor<))
flag = -; long long d = abs((long long)dividend); // 为什么只有 long long才可以
long long s = abs((long long)divisor); while(s<=d)
{
int cnt = ;
long long tmp = s; // 只有long long才可以
tmp<<=;
while(tmp<=d)
{
cnt<<=;
tmp<<=;
}
res+=cnt;
d-=(tmp>>=);
}
return (int)flag*res; }
};

很巧妙,学会利用位运算。还有要用long long。tem<<=1 是tmp= tmp<<1 的意思。 &&的优先级比||的高。

leetcode第28题--Divide Two Integers的更多相关文章

  1. LeetCode(29)Divide Two Integers

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

  2. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  3. 【LeetCode算法-28/35】Implement strStr()/Search Insert Position

    LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...

  4. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  5. 【LeetCode每天一题】Divide Two Integers(两整数相除)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  6. [LeetCode] 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers

    引言 数学计算的模拟类题目,往往是要求实现某种计算(比如两数相除),实现的过程中会有所限定,比如不允许乘法等等. 这类题目首先要注意计算过程中本身的特殊情况.比如求相除,则必须首先反映过来除数不能为0 ...

  7. 【leetcode刷题笔记】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 题解:要求不用乘除和取模运算实现两个数的除法. ...

  8. LeetCode 28 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 思路:1.先将被除数和除数转化为long的非负 ...

  9. 【Leetcode】【Medium】Divide Two Integers

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

随机推荐

  1. curl 要么 file_get_contents 获得授权页面的方法的必要性

    今天,需要工作,需要使用 curl / file_get_contents 获得授权的必要性(Authorization)的页面内容.解决后写了这篇文章分享给大家. php curl 扩展,可以在se ...

  2. DOM笔记2

    <!-- 节点类型检查 if(someNode.nodeType==ElementNode){ alert("Node is an element"); } 或者 if(so ...

  3. C++ Primer笔记4_静态成员类_IO库

    1.静态成员类 static成员变量与函数 static成员变量:必须在类外初始化.(const或引用类型变量必须在构造函数初始化列表里初始化) static成员函数: 不依赖于类.相当于类里的全局函 ...

  4. C#-默认显示前列-ShinePans

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 3.1、Eclipse

    (原版的:http://www.libgdx.cn/topic/22/3-1-eclipse) 生成项目之后,如今我们来将项目导入到Eclipse中. 在将项目导入到Eclipse之前,确定你已经配置 ...

  6. 它的斗争“和loser对话”短篇故事

    今天,一个朋友发来的图片故事,尽管听说过,但见一.仍感慨颇多. 有时总是说easy,其实做起来的另一个故事. 想实现梦想,看来还是要脚踏实地,一步一步.不断努力,不断前行啊! 版权声明:本文博客原创文 ...

  7. Ajax基础知识(一)

    随便在百度谷歌上输入Ajax都会出现一大堆的搜索结果,这已经不再是什么新奇的技术了.但若从一开始就学习了ASP.Net,使用功能齐全的Visual Studio集成开发工具,或许未必能对访问一个动态网 ...

  8. linux Apache安装

    原文:linux Apache安装 1.       下载apache,http://httpd.apache.org/download.cgi  通过这个官方网站,我们可以下到最新的版本.现在版本都 ...

  9. C面试题

    1.sizeof()和strlen()使用? 答案: 1.从功能定义,strlen功能,要查找字符串的长度,sizeof功能是用来寻找指定的变量或变量类型的存储器占用 尺寸: 2.sizeof是运算符 ...

  10. css 鼠标提示样式预览表[转]

    语法: cursor : auto | all-scroll | col-resize| crosshair | default | hand | move | help | no-drop | no ...