leetcode第28题--Divide Two Integers
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的更多相关文章
- LeetCode(29)Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【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 ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers
引言 数学计算的模拟类题目,往往是要求实现某种计算(比如两数相除),实现的过程中会有所限定,比如不允许乘法等等. 这类题目首先要注意计算过程中本身的特殊情况.比如求相除,则必须首先反映过来除数不能为0 ...
- 【leetcode刷题笔记】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 题解:要求不用乘除和取模运算实现两个数的除法. ...
- LeetCode 28 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 思路:1.先将被除数和除数转化为long的非负 ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- CCBValue
#ifndef __CCB_VALUE_H__ #define __CCB_VALUE_H__ #include "cocos2d.h" #include "Extens ...
- 《Effective C++》:规定44-规定45
规定44分离的不依赖参数代码templates 条款45运用成员函数模板接受全部兼容类型 Templates和泛型编程 条款44:将与參数无关的代码抽离templates Templates能够节省时 ...
- Java Date API demo
package date; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; /*2015- ...
- 构造activeMQ
一.添加下列库 而配置的路径 ws2_32.lib;Mswsock.lib;cppunit.lib;libapr-1.lib;libapriconv-1.lib;libaprutil-1.lib;li ...
- Android发展简报
Android这个词的本义是指“机器人”.同时它是Google至2007年11月5根据公布Linux台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成.号称是首个为移动终端打 ...
- css 鼠标提示样式预览表[转]
语法: cursor : auto | all-scroll | col-resize| crosshair | default | hand | move | help | no-drop | no ...
- Nyoj 一笔画问题(图论)
描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画. 输入 第一行只有一个正整数N(N&l ...
- Ubuntu Server 14.04 LTS(64bit)已安装 weblogic Server 12c(12.1.3) Zip Distribution
这里说的对Ubuntu Server 14.04 LTS(64bit)已安装weblogic Server 12c(12.1.3) Zip Distribution遇到的问题.至于Windows什么好 ...
- C语言字符串函数大全
C语言字符串函数大全 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include ...
- T4模版引擎之生成数据库实体类
在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇.T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗).而这其中我们使用的最普遍的是根据数据库生成实 ...