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 ...
随机推荐
- 深入浅出Java并发包—锁(Lock)VS同步(synchronized)
今天我们来探讨一下Java中的锁机制.前面我们提到,在JDK1.5之前只能通过synchronized关键字来实现同步,这个前面我们已经提到是属于独占锁,性能并不高,因此JDK1.5之后开始借助JNI ...
- Openfire 服务端在Eclipse上部署
http://blog.csdn.net/chexitianxia/article/details/9371169 结合: http://blog.csdn.net/ares1201/article/ ...
- Project Euler 85 :Counting rectangles 数长方形
Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co ...
- hdu 3972 1 M possible
一般做法: 显然的超内存 #include<stdio.h> #include<algorithm> using namespace std; ],ans[]; int mai ...
- Linux中断处理体系结构分析
Linux中断处理体系结构分析(一) 异常,就是可以打断CPU正常运行流程的一些事情,比如外部中断.未定义指令.试图修改只读的数据.执行swi指令(Software Interrupt Instruc ...
- QString->string->wstring->LPCWSTR
QFileInfo info("./records.db"); std::string str = info.absoluteFilePath().toStdString(); / ...
- JavaScript DOM编程基础精华03(动态设置,层的操作,性能问题)
代码是否需要放置到onload中 //如果js代码需要操作页面上的元素,则将该代码放到onload里面. //因为当页面加载完毕之后页面上才会有相关的元素 //如果js代码中没有操作 ...
- C#实现的ReplaceFirst和ReplaceLast
原文:C#实现的ReplaceFirst和ReplaceLast ReplaceFirst: public static string ReplaceFirst(string input, strin ...
- Android Studio 初探
前言 上周由于写了一篇关于"Eclipse+ADT+Android SDK 搭建安卓开发环境" 的博文,其他博主们表示相当的不悦,都什么年代了还用Eclipse+ADT开发安卓应用 ...
- CentOS安装VSFTP及配置用户
第一步,安装vsftp # 1.以管理员(root)身份执行以下命令 yum install vsftpd # 2.设置开机启动vsftpd ftp服务 chkconfig vsftpd on # 3 ...