[LeetCode]Divide Two Integer
Divide two integers without using multiplication, division and mod operator.
思考:位运算。AC的时候真的想说一句“尼玛。。“,用unsigned int 超时,莫名其妙,折腾到半夜,百度一下改为long long过了。。我也不明白到底怎么回事,毕竟不是CS出身,哎。。太晚了,明天再看。
VC不支持long long,珍爱生命,远离VC。
class Solution {
public:
int divide(int dividend, int divisor) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
assert(divisor!=0);
if(dividend==0) return 0;
bool flag=true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0)) flag=false;
long long c=dividend;
long long d=divisor;
long long a=abs(c);
long long b=abs(d);
long long ret=0;
long long count=1;
while(a!=0)
{
b=abs(d);
if(a<b)
{
a=0;
count=0;
break;
}
count=1;
while(b<a)
{
b<<=1;
count<<=1;
}
if(b!=a)
{
b>>=1;
count>>=1;
}
a-=b;
ret+=count;
}
return (flag)?ret:-ret;
}
};
[LeetCode]Divide Two Integer的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- [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分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
随机推荐
- js cookie使用方法详解
代码如下 复制代码 <script>function getCookie(c_name){ if (document.cookie.length>0){ //先查询cookie是否为 ...
- Layout.xml中控件的ID命名方式
控件 缩写 LayoutView lv RelativeView rv TextView tv Button btn ImageButton imgBtn ImageView mgView 或则 iv ...
- Xamarin 实现android gridview 多选
参考文章:http://blog.csdn.net/zhouyuanjing/article/details/8372686 GridView初始化代码: gridViewStudent = Find ...
- ubuntu grub 引导修复
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4167644.html (1) 先使用ls命令,找到Ubuntu的安装分区: 在 grub ...
- artdialog 提示 确定或取消
dialog({ title:'提示', content:"下载需扣除" + point + "个积分<br />重复下载不扣积分,需要继续吗?", ...
- Mongodb学习使用记录
在学习使用Mongodb中,对map和reduce的定义,以及对 mapReduce() 方法的调用: >map #在直接输入map时会出现一个'map is not defined'的异常错误 ...
- unionId突然不能获取的踩坑记录
昨天(2016-2-2日),突然发现系统的一个微信接口使用不了了.后来经查发现,是在网页授权获取用户基本信息的时候,unionid获取失败导致的. 在网页授权获取用户基本信息的介绍中(http://m ...
- 做了codility网站上一题:CountBoundedSlices
在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...
- ASP.NET 学习笔记(一)ASP.NET 概览
ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web Pages(Web 页面).MVC(Mode ...
- RabbitMQ基础总结
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消 息传递指的是程序之 ...