首页 > 其他分享 >【刷题笔记】29. Divide Two Integers

【刷题笔记】29. Divide Two Integers

时间:2023-08-23 12:35:51浏览次数:41  
标签:Integers return divisor Divide Two abs && dividend quotient

题目

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

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 2^31 − 1 when the division result overflows.

题目大意

给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。返回被除数 dividend 除以除数 divisor 得到的商。

说明:

  • 被除数和除数均为 32 位有符号整数。
  • 除数不为 0。
  • 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−2^31,  2^31 − 1]。本题中,如果除法结果溢出,则返回 2^31 − 1。

解题思路

  • 给出除数和被除数,要求计算除法运算以后的商。注意值的取值范围在 [−2^31, 2^31 − 1] 之中。超过范围的都按边界计算。
  • 这一题可以用二分搜索来做。要求除法运算之后的商,把商作为要搜索的目标。商的取值范围是 [0, dividend],所以从 0 到被除数之间搜索。利用二分,找到(商 + 1 ) * 除数 > 被除数并且 商 * 除数 ≤ 被除数 或者 (商+1)* 除数 ≥ 被除数并且商 * 除数 < 被除数的时候,就算找到了商,其余情况继续二分即可。最后还要注意符号和题目规定的 Int32 取值范围。
  • 二分的写法常写错的 3 点:
    1. low ≤ high (注意二分循环退出的条件是小于等于)
    2. mid = low + (high-low)>>1 (防止溢出)
    3. low = mid + 1 ; high = mid - 1 (注意更新 low 和 high 的值,如果更新不对就会死循环)

参考代码

package leetcode

import (
	"math"
)

// 解法一 递归版的二分搜索
func divide(dividend int, divisor int) int {
	sign, res := -1, 0
	// low, high := 0, abs(dividend)
	if dividend == 0 {
		return 0
	}
	if divisor == 1 {
		return dividend
	}
	if dividend == math.MinInt32 && divisor == -1 {
		return math.MaxInt32
	}
	if dividend > 0 && divisor > 0 || dividend < 0 && divisor < 0 {
		sign = 1
	}
	if dividend > math.MaxInt32 {
		dividend = math.MaxInt32
	}
	// 如果把递归改成非递归,可以改成下面这段代码
	// for low <= high {
	// 	quotient := low + (high-low)>>1
	// 	if ((quotient+1)*abs(divisor) > abs(dividend) && quotient*abs(divisor) <= abs(dividend)) || ((quotient+1)*abs(divisor) >= abs(dividend) && quotient*abs(divisor) < abs(dividend)) {
	// 		if (quotient+1)*abs(divisor) == abs(dividend) {
	// 			res = quotient + 1
	// 			break
	// 		}
	// 		res = quotient
	// 		break
	// 	}
	// 	if (quotient+1)*abs(divisor) > abs(dividend) && quotient*abs(divisor) > abs(dividend) {
	// 		high = quotient - 1
	// 	}
	// 	if (quotient+1)*abs(divisor) < abs(dividend) && quotient*abs(divisor) < abs(dividend) {
	// 		low = quotient + 1
	// 	}
	// }
	res = binarySearchQuotient(0, abs(dividend), abs(divisor), abs(dividend))
	if res > math.MaxInt32 {
		return sign * math.MaxInt32
	}
	if res < math.MinInt32 {
		return sign * math.MinInt32
	}
	return sign * res
}

func binarySearchQuotient(low, high, val, dividend int) int {
	quotient := low + (high-low)>>1
	if ((quotient+1)*val > dividend && quotient*val <= dividend) || ((quotient+1)*val >= dividend && quotient*val < dividend) {
		if (quotient+1)*val == dividend {
			return quotient + 1
		}
		return quotient
	}
	if (quotient+1)*val > dividend && quotient*val > dividend {
		return binarySearchQuotient(low, quotient-1, val, dividend)
	}
	if (quotient+1)*val < dividend && quotient*val < dividend {
		return binarySearchQuotient(quotient+1, high, val, dividend)
	}
	return 0
}

// 解法二 非递归版的二分搜索
func divide1(divided int, divisor int) int {
	if divided == math.MinInt32 && divisor == -1 {
		return math.MaxInt32
	}
	result := 0
	sign := -1
	if divided > 0 && divisor > 0 || divided < 0 && divisor < 0 {
		sign = 1
	}
	dvd, dvs := abs(divided), abs(divisor)
	for dvd >= dvs {
		temp := dvs
		m := 1
		for temp<<1 <= dvd {
			temp <<= 1
			m <<= 1
		}
		dvd -= temp
		result += m
	}
	return sign * result
}

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

标签:Integers,return,divisor,Divide,Two,abs,&&,dividend,quotient
From: https://blog.51cto.com/u_16110811/7201238

相关文章

  • leetcode-1-two sum(brute force, hash table)
    Wecanusebruteforcetogetit,usetwoforloopiandj,whichi=1:nandj=i:n.However,thetimecomplexityisO(n^2),whichisnotefficient.Usehashtable,thefirstthingisfirststoreeveryelementtotable,thendotraverseagaintolookup......
  • Leetcode 349.两个数组的交集(Intersection of two arrays)
    题目链接......
  • 学习笔记:DSTAGNN: Dynamic Spatial-Temporal Aware Graph Neural Network for Traffic
    DSTAGNN:DynamicSpatial-TemporalAwareGraphNeuralNetworkforTrafficFlowForecastingICML2022论文地址:https://proceedings.mlr.press/v162/lan22a.html代码地址:https://github.com/SYLan2019/DSTAGNN一个用于时空序列预测的交通流量预测模型。可学习的地方:提出......
  • C# 判断两个时间区间是否交叉重叠 (Determine Whether Two Date Ranges Overlap)
    给定两个日期间隔A和B,组件.start和.end和约束.start<=.end,如果:A.end>=B.startANDA.start<=B.end您可以调整>=与>和<=与<的使用,以满足您对重叠程度的要求。举例:该要求是如果StartDate=EndDate不算重合if(A.EndDate>B.StartDate&&A.StartDate<B.EndDate){......
  • [KDD 2023] All in One- Multi-Task Prompting for Graph Neural Networks
    [KDD2023]AllinOne-Multi-TaskPromptingforGraphNeuralNetworks总结提出了个多任务prompt学习框架,扩展GNN的泛化能力:统一了NLP和图学习领域的prompt格式,包括prompttoken、tokenstructure、insertingpattern构建诱导子图,将点级和边级任务改造为图级任务,统一不同......
  • SocialLGN Light graph convolution network for social recommendation
    目录概SocialLGN代码LiaoJ.,ZhouW.,LuoF.,WenJ.,GaoM.,LiX.andZengJ.SocialLGN:Lightgraphconvolutionnetworkforsocialrecommendation.InformationSciences,2022.概LightGCN+Social.方法很简单,利于理解socialrecommendation.SocialLGN......
  • Cisco CCNA——Network Design Model And Case Study
    NetworkDesignModelAndCaseStudy园区网分层结构接入层技术:子网划分、vlan划分、trunk汇聚层:trunk、vrp、链路聚合核心层:静态路由、动态路由、默认路由、ospf、vtp、acl出口层:nat转换、PPPoE、stp常见网络模型SMB中小型企业网教育行业模型(中小型网络)教育行业模型(大型网络)金融行......
  • Number of Beautiful Integers in the Range
    NumberofBeautifulIntegersintheRangeYouaregivenpositiveintegers low, high,and k.Anumberisbeautiful ifitmeetsbothofthefollowingconditions:Thecountofevendigitsinthenumberisequaltothecountofodddigits.Thenumberisdivi......
  • Docker搭建lnmp之network篇
    dockerpullnginx#拉去最新的nginx镜像一、搭建vagrant+VagrantBoxVM环境创建Vagrantfile文件vagrantinit编辑Vagrantfile文件Vagrant.configure("2")do|config|config.vm.box="centos7"#指定BOXconfig.vm.networ......
  • Leetcode 160. 链表相交(Intersection of two linked lists lcci)
    题目链接给定两个单链表的头节点headA和headB,请找出并返回两个单链表相交的起始节点.如果两个链表没有交点,返回null.图示两个链表在节点c1开始相交,题目数据保证整个链式结构中不存在环.示例1:输入:intersectVal=8,listA=[4,1,8,4,5],listB=[5,0,1,8,4,5],sk......