[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 ...
随机推荐
- java createSQLQuery().list()返回日期格式没有时分秒的解决方法
方法一 将Oracel数据库对应表中“收单时间的字段”receive_sheet_time,由原来的Date类型改为timestamp 然后,在java程序中,由 (java.util.timesta ...
- DOS批处理命令-call命令
call命令 在批处理中调用别的批处理或者可运行程序或者 バッチ プログラムを別のバッチ プログラムから呼び出します. 语法 1.CALL [驱动盘符:][路径]文件名 [参数] 调用并执行[驱动盘符 ...
- Jquery插件的编写和使用
第七章 Jquery插件的编写和使用 插件的定义: 插件也称为扩展,是一种遵循一定规范的应用程序接口编写出来的程序. 下面是Jquery插件的编写很使用:要查看请点击:Jquery插件的编写很使 ...
- Android Studio ndk-Jni开发详细
http://www.open-open.com/lib/view/open1451917048573.html Java Native Interface (JNI)标准是java平台的一部分,它允 ...
- windows编程socket问题
今天调试了个MFC网络程序,被bug困扰了一天,终于在收工前解决了. 大致是这样的,我们需要用上位机远程控制机器车前行.上位机上的MFC app的键盘按键响应如下:当按键按下时,系统会发送一个消息给a ...
- Json文件/网址解析
// // main.m // OC8-Json文件解析 // // Created by qianfeng on 15/6/23. // Copyright (c) 2015年 qianfeng. ...
- OC6_复合类的类存管理
// // Person.h // OC6_复合类的类存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhan ...
- 抽象类[abstract]_C#
抽象类(abstract) abstract修饰符可以和类.方法.属性.索引器及事件一起使用.在类声明中使用abstract修饰符以指示某个类只能是其它类的基类.标记为抽象或包含在抽象类中的成员必须通 ...
- linux ubuntu vsftp 默认主目录
vi /etc/passwd 查看/ftp: 后面的目录就是默认目录 这是匿名用户的目录 --------------------------- 使用linux 别的用户,默认在/home/用户名 ...
- css设置网页打印样式
有三种方法 1. 为屏幕显示和打印分别准备一个css文件,如下所示: 用于屏幕显示的css: <link rel="stylesheet" href="css/n ...