-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29.cpp
More file actions
34 lines (33 loc) · 794 Bytes
/
29.cpp
File metadata and controls
34 lines (33 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// 29.cpp
// leetcode
//
// Created by R Z on 2017/11/15.
// Copyright © 2017年 R Z. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
if(!divisor || (dividend==INT_MIN && divisor==-1)){
return INT_MAX;
}
int res=0;
int sign = (dividend<0)^(divisor<0)?-1:1;
long long divid=labs(dividend);
long long divis=labs(divisor);
while(divid>=divis){
long long temp=divis, multi=1;
while(divid>=(temp<<1)){
multi<<=1;
temp<<=1;
}
divid-=temp;
res +=multi;
}
return sign==1?res:0-res;
}
};