- 浏览: 184865 次
- 性别:
- 来自: 济南
文章分类
最新评论
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
题目要求我们不用乘法,除法还有模运算来完成两个整数的除运算。如果溢出时返回最大值。
题目要求返回一个整型,当除数为Integer.MIN_VALUE,被除数为-1时就会越界,这个情况我们要单独处理。因为不能用乘法,除法和模运算,因此我们想到的就是位运算来处理,左移一位相当于乘2,右移一位相当于除2。但是运算过程中,如果左移可能会溢出,因此我们要对这种情况考虑,可以把变量声明为long。具体代码如下:
If it is overflow, return MAX_INT.
题目要求我们不用乘法,除法还有模运算来完成两个整数的除运算。如果溢出时返回最大值。
题目要求返回一个整型,当除数为Integer.MIN_VALUE,被除数为-1时就会越界,这个情况我们要单独处理。因为不能用乘法,除法和模运算,因此我们想到的就是位运算来处理,左移一位相当于乘2,右移一位相当于除2。但是运算过程中,如果左移可能会溢出,因此我们要对这种情况考虑,可以把变量声明为long。具体代码如下:
public class Solution { public int divide(int dividend, int divisor) { if(divisor == -1 && dividend == Integer.MIN_VALUE) return Integer.MAX_VALUE; long dsor = Math.abs((long) divisor); long dend = Math.abs((long) dividend); int result = 0; while(dend >= dsor) { int counter = 0; while(dend >= (dsor << counter)) { counter ++; } counter --; result += 1 << counter; dend -= dsor << counter; } if(dividend < 0 && divisor > 0 || dividend > 0 && divisor < 0) return -result; return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 668Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ...
相关推荐
c c语言_leetcode 0029_divide_two_integers.zip
js js_leetcode题解之29-divide-two-integers.js
先来看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 就是说不用乘法,除法,求模运算...
- Divide Two Integers: 实现整数除法,不能使用乘法、除法和模运算符。 - Search in Rotated Sorted Array: 在旋转过的排序数组中进行二分查找。 - Search for a Range: 给定一个按照升序排列的数组,和一个目标值...
Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档....29.Divide Two Integers 二进制累加代替除法,防止溢出 36.Valid Sudoku set去重复 2018/04/19: 038.Count and Say 递归 040.C
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 | ...
Divide Two Integers 两个整数相除,要求不能使用 *,/,以及mod操作符,返回除数,若除数有小数点,只返回整数部分,如2.789,只应返回2,此题为leetcode上的题目 Random Numbers 用计算机生成了N个1到1000之间的...
5. **除以两个整数(Divide Two Integers)** 任务是实现一个函数,模拟整数除法,返回商(不考虑浮点数部分)。这个题目需要考虑整数溢出问题,可以使用位运算和减法进行求解。例如,将除数和被除数都乘以一个足够大...
问题 完全的 017_Letter_Combinations_of_a_Phone_Number 018_4总和 019_Remove_Nth_Node_From_End_of_List 020_Valid_Parentheses ... 029_Divide_Two_Integers 030_Substring_with_Concatenation_of
HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For ...
printf("Enter two integers: "); if (scanf("%d %d", &a, &b) == 2) { // 检查是否成功读取两个整数 printf("a=%d\nb=%d\n", a, b); printf("a+b=%d\n", a + b); if (b != 0) { printf("a/b=%d\n", a / b); } ...
029_divide_two_integers*.py # 实现整除 050_pow.py # 实现乘幂 066_plus_one.py # 数列末尾值+1 069_sqrt.py # 实现开根号 136_single_number.py # 位操作:异或(xor)操作 x ^ 0 = x; x ^ x = 0 sum 001_two_sum....
The recursive method is to equally divide the integer set into two parts by the integer in the middle position. Then recursively print the first part, followed by printing the integer in the middle, ...
3. **Unsigned Two's Complement Representation**: This method allows representation of both positive and negative integers. Positive numbers and zero are represented identically to their unsigned ...
8. leetcode-29-Divide-Two-Integers.md:第29题,两数相除,涉及到整数除法和位运算。 9. leetcode-218-The-Skyline-Problem.md:第218题,天际线问题,可能涉及到二维数组处理和线段树或平衡二叉搜索树的数据结构...
Chapter 4 Defining and Using Integers Chapter 5 Defining and Using Complex Data Types Chapter 6 Using Floating-Point and Binary Coded Decimal Numbers Chapter 7 Controlling Program Flow Chapter 8 ...
numbers_str = input("Enter two non-zero integers separated by comma: ") nums = [int(n) for n in numbers_str.split(",")] result = calculate(nums[0], nums[1]) print(result) ``` 在这个函数中,我们首先将...
func divide(a, b int) (int, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil } ``` - **匿名函数**:可以在任何地方定义匿名函数,并可以将其作为值传递给其他函数。...
第 一 章 概述 1-1 简述计算机程序设计语言的发展历程。 解: 迄今为止计算机程序设计语言的发展经历了机器语言、汇编语言、高级语言等阶段,C++语言是一种面向对象的编程语言,也属于高级语言。...