LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接
https://leetcode.com/problems/divide-two-integers/description/
2. 题目要求
给出被除数dividend和除数divisor,求出二者相除的商,余数忽略不计。
注意:不能使用乘法、除法和取余运算
3. 解题思路
陷阱一:MIN_VALUE/-1会溢出。因为Integer.MIN_VALUE = -的绝对值比Integer.MAX_VALUE大1
陷阱二:除数divisor不能等于“0”
思路一:使用一个while循环,当dividend >= divisor时,进入循环。dividend = divident - divisor,每减一次计数器res+1。循环结束后则得到二者之商。
缺点:时间复杂度为O( n ),当被除数很大、除数很小时,效率非常低
- public class DivideTwoIntegers29 {
- public static void main(String[] args) {
- System.out.println(divided(-36, -3));
- }
- public static int divide(int dividend, int divisor) {
- if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
- return Integer.MAX_VALUE;
- int res = 0;
- int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
- long dvd = Math.abs((long) dividend);
- long dvs = Math.abs((long) divisor);
- while (dvd >= dvs) {
- dvd -= dvs;
- res++;
- }
- return sign == 1 ? res : -res;
- }
- }
思路二:采用位移运算,任何一个整数可以表示成以2的幂为底的一组基的线性组合,即num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n。基于以上这个公式以及左移一位相当于乘以2,我们先让除数左移直到大于被除数之前得到一个最大的基。然后接下来我们每次尝试减去这个基,如果可以则结果增加加2^k,然后基继续右移迭代,直到基为0为止。因为这个方法的迭代次数是按2的幂直到超过结果,所以时间复杂度为O(logn)。
- public class DivideTwoIntegers29 {
- public static void main(String[] args) {
- System.out.println(divided(-36, -3));
- }
- public static int divide(int dividend, int divisor) {
- if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
- return Integer.MAX_VALUE;
- int res = 0;
- int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
- long dvd = Math.abs((long) dividend);
- long dvs = Math.abs((long) divisor);
- while (dvs <= dvd) {
- long temp = dvs, mul = 1;
- while (dvd >= temp << 1) { // temp<<1,二进制表示左移一位,等价于temp乘以2
- temp = temp << 1;
- mul = mul << 1;
- System.out.println("temp = " + temp + " " + "mul = " + mul);
- }
- dvd -= temp;
- System.out.println("dvd" + dvd);
- res += mul;
- }
- return sign == 1 ? res : -res;
- }
- }
LeetCode: 29. Divide Two Integers (Medium)的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
随机推荐
- React Native for Android on Windows 配置开发安装总结
配置开发安装总结(由于当前react-native更新较快,目前是针对2015年11月底时的reacti-native android for windows版本,有些内容可能过时) 官方的安装指导在 ...
- sysctl.conf学习和调优
转载于简书:sysctl.conf学习和调优 ,如有版本问题,请联系我 前言 记得第一次接触/etc/security/limits.conf和/etc/sysctl.conf时是因为部署Oracle ...
- 解决 Visual Studio 2017 打开项目提示项目不兼容
这应该算是VS2017的一个bug,昨天做好的.net core项目还能好好如初,今天打开就提示项目不兼容,未能加载...... 解决办法也是超级简单,但是往往越简单的办法越是想不到: 右键解决方案, ...
- String.format字符串拼接
一.String.Format1.简介 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象. 2.参数 format()方法有两种重载形式. form ...
- PAT——1045. 快速排序
著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边. 给定划分后的N个互不相同的正整数的排列,请问有多 ...
- [转]TestNG的多线程并行
前言 最近在做项目里的自动化测试工作,使用的是TestNG测试框架,主要涉及的测试类型有接口测试以及基于业务实际场景的场景化测试.由于涉及的场景大多都是大数据的作业开发及执行(如MapReduce.S ...
- Oracle session相关数据字典(一)
(一)session相关视图 (1)视图 v$session v$active_session_history dba_hist_active_session_history 如果是多节点数据库,v$ ...
- Linux -- date 日期命令
Linux -- date 日期命令 date 用法:date [选项]... [+格式] 以给定的格式显示当前时间,或是设置系统日期. 1.使用 date 命令查看当前日期或当前时间 [root@l ...
- Struts2-01
一.Struts2的介绍 讲Struts2框架之前,我们需要知道框架是什么呢?估计大多数初学者都只知道其名却不知其意,框架就是一个半成品,别人将一些功能已经写好了,我们只需要拿来用即可,像我们之前使用 ...
- 557. Reverse Words in a String III (5月25日)
解答 class Solution { public: string reverseWords(string s) { string temp,result; while(1){ if(s.find( ...