自己实现除法 太太太恶心了。。。。
就是用位移代替了乘法,然后不断减去位移得到的乘数*除数
主要是测试数据还有一个Java.MinValue的问题 = = 取了abs之后它居然还是负数。。。我日
就是用位移代替了乘法,然后不断减去位移得到的乘数*除数
主要是测试数据还有一个Java.MinValue的问题 = = 取了abs之后它居然还是负数。。。我日
public class Solution { public int divide(int dividend, int divisor) { // Start typing your Java solution below // DO NOT write main() function int positive = 1; if (dividend >0 && divisor < 0 || dividend <0 && divisor > 0 ) positive = -1; if (divisor == Integer.MIN_VALUE) { if (dividend == Integer.MIN_VALUE) { return 1; } else { return 0; } } if (dividend == Integer.MIN_VALUE) { if (positive == -1) { return -1 + divide(dividend + divisor, divisor); } else { return 1 - divide(dividend + divisor, divisor); } } dividend = Math.abs(dividend); divisor = Math.abs(divisor); int result = 0; while (dividend >= divisor){ int product = 1; int temp = divisor; while (dividend - temp >= temp){ product = product << 1; temp = temp << 1; } result += product; dividend -= temp; } return positive * result; } }
发表评论
-
Insert Interval
2012-11-11 01:33 542各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 ... -
Implement strStr()
2012-11-07 15:44 581唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。 ... -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
2012-11-07 00:08 1093Flatten太简单了 递归 一遍过 oh yeah = = ... -
First Missing Positive
2012-11-06 22:50 589唉 想了很久都没想出来 后来还是看了网上的答案 >_&l ... -
Edit Distance
2012-11-06 00:27 670动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一 ... -
Distinct Subsequences
2012-11-04 21:44 657动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一 ... -
Count and Say
2012-11-04 18:46 693public class Solution { ... -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
2012-11-04 17:36 826/** * Definition for binary ... -
Container With Most Water
2012-11-04 00:25 697本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超 ... -
Construct Binary Tree from Inorder and Postorder Traversal
2012-11-03 23:40 746不知道为什么错了。。。eclipse上明明是正确的啊 leet ... -
Combinations
2012-11-03 22:19 604全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,s ... -
Combination Sum I && II
2012-11-03 21:41 751还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就 ... -
climbing stairs
2012-11-03 17:18 609一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是 ... -
Binary Tree Inorder Traversal
2012-11-02 23:51 615I简单 直接递归就好 addAll函数很好用 /** ... -
Best Time to Buy and Sell Stock I & II
2012-11-02 22:05 1044啊 第一次直接过small和big测试 好爽!虽然主要是以前知 ... -
Balanced Binary Tree
2012-11-01 23:38 609/** * Definition for binary ... -
Anagrams
2012-10-31 00:33 586这题实在是没懂它的意思。。。囧啊 import java. ... -
Add Two Numbers
2012-10-30 23:03 614这题不难 直接上递归就行 /** * Definiti ... -
Add Binary
2012-10-29 00:07 599public String addBinary(Strin ... -
4Sum
2012-10-27 22:49 734本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Ju ...
相关推荐
先来看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 Text Justification Max Points on a Line Community QQ 群: 237669375 Github: https://www.github.com/soulmachine/algorithm-essentials 微博: @灵魂机器 License Book License: CC ...
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之间的...
问题 完全的 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 ...
题目来源:https://leetcode-cn.com/problems/divide-two-integers 题目 给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。 返回被除数 dividend 除以除数 divisor ...
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, ...
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 ...
第 一 章 概述 1-1 简述计算机程序设计语言的发展历程。 解: 迄今为止计算机程序设计语言的发展经历了机器语言、汇编语言、高级语言等阶段,C++语言是一种面向对象的编程语言,也属于高级语言。...
The ASCII Character Set <br>1.12 Summary 1.13 Laboratory Exercises 1.13.1 Installing the Software 1.13.2 Data Conversion ... 1.14 Questions 1.15 Programming Projects <br>Chapter Two ...