leetcode — divide-two-integers
/**
* Source : https://oj.leetcode.com/problems/divide-two-integers/
*
* Created by lverpeng on 2017/7/12.
*
* Divide two integers without using multiplication, division and mod operator.
*
* If it is overflow, return MAX_INT.
*/
public class DevideTwoInt {
/**
* 不能使用乘、除、求模
*
* 可以直接每次减去除数,但是效率较低,那可以使用移位,类似乘法,将除数乘以2的倍数,然后再与被除数做减法
*
* @param devident
* @param devisor
* @return
*/
public int devide (int devident, int devisor) {
long[] sub = new long[32];
int newDevident = devident < 0 ? -devident : devident;
int newDevisor = devisor < 0 ? -devisor : devisor;
int count = 0;
sub[count] = newDevisor;
int twiceNum = newDevisor;
while (newDevident > twiceNum) {
twiceNum = newDevisor << count;
sub[count] = twiceNum;
count ++;
}
count --;
int remainder = newDevident;
int result = 0;
while (remainder >= newDevisor) {
if (remainder >= sub[count]) {
remainder -= sub[count];
result = result + (1 << count);
} else {
count --;
}
}
return (devident > 0 ^ devisor > 0) ? -result : result;
}
public static void main(String[] args) {
DevideTwoInt devideTwoInt = new DevideTwoInt();
System.out.println(devideTwoInt.devide(-100, 10));
System.out.println(devideTwoInt.devide(-100, 9));
System.out.println(devideTwoInt.devide(-100, -9));
System.out.println(devideTwoInt.devide(100, 9));
System.out.println(devideTwoInt.devide(0, 9));
}
}
leetcode — divide-two-integers的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- 跑python用ThinkPad好还是MacBook好?
跑Python,那肯定是服务器操作系统最好,找个方便安装Linux的本子. 我想题主的意图应该是做Python开发吧,如果是Python开发,还要看一下开发方向,如果是网络爬虫.服务器后端编程类的,那 ...
- 关于requests库中文编码问题
转自:代码分析Python requests库中文编码问题 Python reqeusts在作为代理爬虫节点抓取不同字符集网站时遇到的一些问题总结. 简单说就是中文乱码的问题. 如果单纯的抓取微博 ...
- python小结 1
1.变量 记录状态 类型:数字,字符串,元组,列表,字典 可变不可变(内存地址不变的情况下,值能不能改变): 不可变:字符串,数字,元组 可变:列表,字典 访问顺序: 直接访问:数字 有序:字符串,列 ...
- idea连接操作数据库
场景 本文主要以DB2作为演示,其他数据库大同小异: 网上有很多推荐DB2的连接软件工具,但是因为DB2的使用场景不多,这次是在做数据资产管理的数据质量分析时使用到,在做数据交换时要在DB2中建表并同 ...
- 学习pyyaml
网上查了一圈,觉得较好的yaml教程有: YAML 语言教程 :http://www.ruanyifeng.com/blog/2016/07/yaml.html. 另外,在github的pyyaml库 ...
- mysql的一些操作命令
1.查看mysql数据库 SHOW DATABASES;(;号一定要加) 2.创建root用户密码 mysqladmin -u root password "new_password&quo ...
- drf5 版本和认证组件
开发项目是有多个版本的 随着项目的更新,版本就越来越多.不可能新的版本出了,以前旧的版本就不进行维护了 那我们就需要对版本进行控制,这个DRF框架也给我们提供了一些封装好的版本控制方法 版本控制组件 ...
- cp备份操作时如何忽略指定的目录
需求场景:进行CP拷贝备份的时候,子目录里面的某些大文件或是一些log文件是无需备份的,那么在CP操作时需要忽略掉指定的目录. 案例演示如下:备份data目录,但是不包括里面的share子目录. 先看 ...
- Hibernate 和 Mybatis 两者相比的优缺点
1.开发上手难度 hibernate的真正掌握(封装的功能和特性非常多)要比Mybatis来得难. 在真正产品级应用上要用Hibernate,不仅对开发人员的要求高,hibernate往往还不适合(多 ...
- USART of STM32
/*************************************************************************** * 文件名:USART.h * * 编写人:离 ...