LeetCode: divideInteger
Title:
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
思路就是每次以两倍增长除数,知道即将大于被除数,然后用被除数减去,在循环。
遇到int整数问题一定要考虑到INT_MAX和INT_MIN。
class Solution {
public:
int divide(int dividend, int divisor) {
int final = ;
/////// overflow///////
if (divisor == || (dividend == INT_MIN && divisor == -)){
return INT_MAX;
}
int nega = ;
if ((dividend>&&divisor<) || (dividend<&&divisor>))
nega = ;
//如果为INT_MIN则求abs为越界
long long a = abs((long long)dividend);
long long b = abs((long long)divisor);
if (b > a)
return ;
long long sum = ;//后面有sum += sum防止越界
int count = ;
while (a >= b)
{
count = ; //a >= b保证了最少有一个count
sum = b;
while (sum + sum <= a){
sum += sum;
count += count;
}
a -= sum;
final += count;
}
if (nega)
final = - final;
return final;
}
};
LeetCode: divideInteger的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- SQL SERVER 之 填充因子
填充因子依附索引而存在,但创建索引,就意味着要对表进行DML时,必须处理额外的工作量(也就是对索引结构的维护)以及存储方面的IO开销. 所以创建索引时,需要考虑创建索引所带来的查询性能方面的提高,与引 ...
- MySql修改默认端口号,修改my.ini的端口号
MySql默认端口号为3306,如果安装多个或者冲突需要修改端口号,修改my.ini的端口号就可以了,文件一般情况下在安装目录下.下面是具体说明: 方法/步骤 先在服务里停止mysql的服务器,再找到 ...
- JS内存管理测试
打开调试器,切换到timer,点击左下角的record按钮开始,切换到memory视图,在文档中点击鼠标左右键,看股价走势图 function Allocate(kbs){ this.mem = ne ...
- Unity3D研究院之IOS全自动编辑framework、plist、oc代码
Unity打IOS时会先生成一个Xcode工程,如果你需要增加一些第三方的framework那么需要手动一条一条的添加,这太烦了..而且可能你还需要修改Plist文件,甚至还可能要修改unity自动生 ...
- 利用vim阅读源代码一个好用的工具
阅读源代码时常常遇到找变量,函数定义的问题.vim为我们提供了一个好用的工具,ctags. 安装 ctags. 在 libvirt的源代码根目录运行 ctags -R . vim -t virConn ...
- ubuntu下安装spark1.4.0
构建在hadoop2.6.0之上的 1.在官网下载spark-1.4.0-bin-hadoop2.6.tgz 2.解压到你想要放的文件夹里,tar zxvf spark-1.4.0-bin-hadoo ...
- hdu1151 Air Raid
http://acm.hdu.edu.cn/showproblem.php?pid=1151 增广路的变种2:DAG图的最小路径覆盖=定点数-最大匹配数 #include<iostream> ...
- sudo: /etc/sudoers 的模式为 0551,应为 0440
环境:Ubuntu 12.04.4 LTS 32bit 本想修改/etc/sudoers文件,取消sudo权限的密码.但是因为sudoers文件无‘w’(写)的权限,然后用命令加写权限的时候加错了,加 ...
- Android中JSON数据格式的简单使用
源码: package com.wangzhu.demo; import java.io.BufferedReader; import java.io.IOException; import java ...
- 李洪强iOS开发之Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...