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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. 【BZOJ】【2154】Crash的数字表格

    莫比乌斯反演 PoPoQQQ讲义第4题 题解:http://www.cnblogs.com/jianglangcaijin/archive/2013/11/27/3446169.html 感觉两次sq ...

  2. Linq to EF 与Linq to Object 使用心得

    大家都知道Linq既可以用来查询数据库对象(我这里指的是Entity FrameWork里的Model对象),也可以用来查询内存中的IEnumerable对象. 两者单独查询时都不会出现什么问题,不过 ...

  3. 【leetcode】Median of Two Sorted Arrays(hard)★!!

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  4. jQuery经典面试题及答案精选[转载]

    问题:jQuery的美元符号$有什么作用? 回答:其实美元符号$只是”jQuery”的别名,它是jQuery的选择器,如下代码: $(document).ready(function(){ }); 当 ...

  5. 2013 ACM-ICPC长沙赛区全国邀请赛—Special equations

    ……但是没仔细看,直接跳过了 这题直接枚举就可以过了 ;}

  6. Android中JSON数据格式的简单使用

    源码: package com.wangzhu.demo; import java.io.BufferedReader; import java.io.IOException; import java ...

  7. 李洪强iOS开发之OC语言类的深入和分类

    OC语言类的深入和分类 一.分类 (一)分类的基本知识  概念:Category  分类是OC特有的语言,依赖于类. 分类的作用:在不改变原来的类内容的基础上,为类增加一些方法. 添加一个分类: 文件 ...

  8. Project Euler 96:Su Doku 数独

    Su Doku Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its o ...

  9. phpmyadmin导入大sql文件失败解决办法

    摘自:http://www.xunway.com/info/post/499.asp 昨天小编的一个客户在在利用phpmyadmin导入大sql文件的时候,总是提示错误,反应给小编,小编也是第一次遇到 ...

  10. [Unity菜鸟] 射线

    1. 射线用 Physics.Raycast 都可以判断,用 collider.Raycast 只在某些(不明)情况下可以 void Update() { Ray ray = Camera.main. ...