新博文地址:[leetcode]Divide Two Integers
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
不使用 乘法、除法、取余来计算除法,这道题在很多书中都有提及,比如面试金典中就有一道题要求只用加法来计算:减法、乘法、除法。其中它对除法的算法思想是,用迭代一个一个加被除数,直到》dividend,但是这无疑是个坑,比如当divideng = Integer.MAX_VALUE;divisor =1;必定会超时。
这道题的题干就给人隐约一种位运算的感觉,事实也的确如此
第一次我尝试用2分法,每一次将divisor * 2,迟早newDivisor * 2会大于dividend,
那么dividend就在[newDivisor ,2 * newDivisor]之间,然后继续从newDivisor开始逐个+ divisor,直到dividend,但是还是超时了.....
今早上突然想到这种2分法可以持续下去的。
接下来用2进制,简单讲一下:
比如dividend 用2进制的表示是1010100,divisor是11,我们一直将divisor * 2,当divisor达到1100000时,超过了dividend,再将其/2,即110000,记录叠加次数。
然后得到二者之差——100100,继续迭代刚才的做法,得到divisor为11000,得到差1100,。
继续刚才的迭代,得到divisor为1100,差为0(差<divisor)则结束。
public int divide(int dividend, int divisor) { long absDividend = Math.abs((long) dividend); long absDivisor = Math.abs((long) divisor); long count = 0, c = 1; while (absDividend >= absDivisor) { absDivisor <<= 1; c <<= 1; if (absDivisor >= absDividend) { if (absDivisor > absDividend) { absDivisor >>= 1; c >>= 1; absDividend -= absDivisor; } count += c; c = 1; if(absDividend == absDivisor){ break; } absDivisor = Math.abs((long) divisor); } } return (int) ((dividend < 0 ^ divisor < 0) ? -count : count); }
要注意溢出情况的考虑,因此将int类型都向上转型为long
相关推荐
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return ...
Leetcode two sum java 解法
先来看LeetCode-29上的Divide Two Integers题目要求: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 1 2 3 就是说不用乘法,除法,求模运算...
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
Two-Sum leetcode两数之和代码 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组...
Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating Characters LeetCode 13 Roman to...
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java AC版本
vs code LeetCode 插件
大佬的leetcode刷题笔记(c++版本)
leetcode 两分球-2 问题1 () 问题2 () 问题3 ()
leetcode 两分球-1 问题1 () 问题2 () 问题3 ()
leetcode中文版
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
Leetcode\TwoSum\TwoSum.cs 问题: 业绩报告: 反转整数 代码: Leetcode\ReverseInteger\ReverseInteger.cs 问题: 业绩报告: 回文数 代码: Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中...
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip