let29 Divide Two Integers

2024-03-29 18:18
文章标签 two integers divide let29

本文主要是介绍let29 Divide Two Integers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

主题思想: 不用乘除取模做除法运算, 首先肯定用加减法, 但是由于直接加减速度太慢,然后联想到,对数优化,以2次幂的方式进行加减,这样速度优化log
最后是处理各种边界问题,比如什么时候会溢出,除数为0,
还要注意到,int 取值范围负数的绝对值比最大正数大1 ,

class Solution {public int divide(int dividend, int divisor) {int sign=1;if(divisor==0) return Integer.MAX_VALUE;if(dividend>0&&divisor<0||dividend<0&&divisor>0)sign=-1;long ldive=Math.abs((long) dividend);long ldivs=Math.abs((long)divisor);if(ldive<ldivs) return 0;long ans=longdivide(ldive,ldivs);if(ans>Integer.MAX_VALUE){ans=(sign==1)?Integer.MAX_VALUE:Integer.MIN_VALUE;}else{ans=(int)sign*ans;}return (int) ans;}public long longdivide(long dividend,long divisor){if(dividend<divisor) return 0;long sum=divisor;long multiple=1;while((sum+sum)<dividend){sum+=sum;multiple+=multiple;}return multiple+longdivide(dividend-sum,divisor);}
}

这篇关于let29 Divide Two Integers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/859290

相关文章

Java多线程编程模式实战指南:Two-phase Termination模式

文章来源: http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-two-phase-termination?utm_source=infoq&utm_campaign=user_page&utm_medium=link 文章代码地址: https://github.com/Visce

[LeetCode] 583. Delete Operation for Two Strings

题:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in

Splay树(区间更新)—— POJ 3468 A Simple Problem with Integers

对应POJ 题目:点击打开链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 72765 Accepted: 22465Case Time Limit: 2000MS Description You have N integers, A1

CodeForces 425C Sereja and Two Sequences

题意: 两组数字a和b  如果a[i]等于b[j]  则可将a[i]和b[j]前所有数字删掉  这种操作花费e体力  得到1元钱  或者一次删掉所有数字  这种操作花费等于曾经删除的所有数字个数  做完后得到所有钱  问 一共s体力 可以得到多少钱 思路: dp+二分 由数据可知最多拿到300元钱  因此可以定义  dp[i][j]表示有i元钱时  b串删除到了j处时  a串删到的位

poj 1849 Two(树形dp求直径)

树的直径:一棵树中两个点间的最长距离。 Description The city consists of intersections and streets that connect them.  Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that ha

Longest Substring with At Most Two Distinct Characters

Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example,Given s = “eceba”, T is "ece" which its length is 3. 思路:同向双指针,跟Longest Substrin

Two to One——C语言提高题【7 kyu】

一、原题 链接:Training on Two to One | Codewars Take 2 strings s1 and s2 including only letters from a to z. Return a new sorted string (alphabetical ascending), the longest possible, containing distinct

LeetCode --- Median of Two Sorted Arrays

第一次在csdn上写备忘录,以前一直是在笔记本上写,主要是笔记本上可以随意写,只要自己能看懂,在网页上多少都受些限制,另外一方面也是想锻炼下写作能力,为以后的论文做基础吧!最近偶尔上leetcode练些题目,所以也就以这个为主题写一篇试试看,因为能力不足,理解或言辞上会有错误,还望访者不吝赐教,我定当万分感激。 好了,废话也说完了,现在进入正题: 题目: There are two sor

Leetcode 3272. Find the Count of Good Integers

Leetcode 3272. Find the Count of Good Integers 1. 解题思路2. 代码实现 题目链接:3272. Find the Count of Good Integers 1. 解题思路 这一题我思路上是比较暴力的,就是典型地分步骤执行: 找出所有的可能构成回文的长度为n的字符组合对于任意字符组合,判断其是否可以构成一个被k整除的回文序列考察这个字符组

2pc_two phase commit详情

文章目录 1. two phase commit protocol1.假设前提2. 算法概述3. 缺点4. 详情1. coordinator 端来看2. cohorts端 5. 正确性分析6. 简单总结 看2pc和3pc看的晕晕乎乎的,看了很多博客,感觉说的都不够细致,看起来也容易犯晕,找到了两篇英文文档(不算原文),看起来好像是清楚一些,有些时候这些协议类的东西研读,如果不是