Skip to content

Commit 95dee69

Browse files
committedDec 6, 2018
add 29
1 parent bf900be commit 95dee69

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ LeetCode
3131
|0026|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | c | [c++](./src/0026-Remove-Duplicates-from-Sorted-Array/0026.cpp) |[python](./src/0026-Remove-Duplicates-from-Sorted-Array/0026.py)|||Easy|
3232
|0027|[Remove Element](https://leetcode.com/problems/remove-element/) | c | [c++](./src/0027-Remove-Element/0027.cpp) |[python](./src/0027-Remove-Element/0027.py)|||Easy|
3333
|0028|[Implement strStr()](https://leetcode.com/problems/implement-strstr/) | c | [c++](./src/0028-Implement-strStr()/0028.cpp) |[python](./src/0028-Implement-strStr()/0028.py)|||Easy|
34+
|0029|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | c | [c++](./src/0029-Divide-Two-Integers/0029.cpp) |[python](./src/0029-Divide-Two-Integers/0029.py)|||Medium|
35+
|0030|[Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | c | [c++](./src/0030-Substring-with-Concatenation-of-All-Words/0030.cpp) |[python](./src/0030-Substring-with-Concatenation-of-All-Words/0030.py)|||Hard|
3436
|0031|[Next Permutation](https://leetcode.com/problems/next-permutation/) | c | [c++](./src/0031-Next-Permutation/0031.cpp) |[python](./src/0031-Next-Permutation/0031.py)|||Medium|
3537
|0032|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | c | [c++](./src/0032-Longest-Valid-Parentheses/0032.cpp) |[python](./src/0032-Longest-Valid-Parentheses/0032.py)|||Hard|
3638
|0033|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | c | [c++](./src/0033-Search-in-Rotated-Sorted-Array/0033.cpp) |[python](./src/0033-Search-in-Rotated-Sorted-Array/0033.py)|||Medium|

‎src/0029-Divide-Two-Integers/0029.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <cstdlib>
2+
using namespace std;
3+
4+
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
5+
class Solution
6+
{
7+
public:
8+
int divide(int dividend, int divisor)
9+
{
10+
long long res = 0;
11+
long long divd = labs(dividend), divs = labs(divisor);
12+
int pos = (dividend < 0) ^ (divisor < 0);
13+
long long mod = (long long)INT_MAX + 1;
14+
if (divd < divs) return 0;
15+
while (divs <= divd)
16+
{
17+
long long mul = 1, tmp = divs;
18+
while ((tmp << 1) <= divd)
19+
{
20+
mul <<= 1; tmp <<= 1;
21+
}
22+
res += mul; divd -= tmp;
23+
if (res == mod) break;
24+
}
25+
if (pos == 0 and res == mod) res -= 1;
26+
return pos == 0 ? res : -res;
27+
}
28+
};

‎src/0029-Divide-Two-Integers/0029.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def divide(self, dividend, divisor):
3+
"""
4+
:type dividend: int
5+
:type divisor: int
6+
:rtype: int
7+
"""
8+
pos = False if (dividend < 0) ^ (divisor < 0) else True
9+
10+
res, divd, divs = 0, abs(dividend), abs(divisor)
11+
12+
if divd < divs:
13+
return 0
14+
15+
mod = 2**31
16+
while divs <= divd:
17+
mul, tmp = 1, divs
18+
while (tmp << 1) <= divd:
19+
mul <<= 1
20+
tmp <<= 1
21+
22+
res += mul
23+
divd -= tmp
24+
25+
if res == mod:
26+
break
27+
28+
if pos and res == mod:
29+
res -= 1
30+
31+
return res if pos else -res

0 commit comments

Comments
 (0)
Please sign in to comment.