Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9392afa

Browse files
author
applewjg
committed
DivideTwoIntegers
Change-Id: I5d9db5c6f2eaa7df89a439adc7ac33f26f7d22fa
1 parente085086 commit9392afa

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎DivideTwoIntegers.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Author: King, higuige@gmail.com
3+
Date: Nov 18, 2014
4+
Problem: Divide Two Integers
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/divide-two-integers/
7+
Notes:
8+
Divide two integers without using multiplication, division and mod operator.
9+
10+
Solution: Use << operator.
11+
*/
12+
publicclassSolution {
13+
publicintdivide(intdividend,intdivisor) {
14+
booleanflag =dividend <0 ^divisor <0;
15+
longDividend =Math.abs((long)dividend);
16+
longDivisor =Math.abs((long)divisor);
17+
longres =0;
18+
while (Dividend >=Divisor) {
19+
longc =Divisor;
20+
for (inti =0; (c <<i) <=Dividend; ++i) {
21+
Dividend -= (c <<i);
22+
res += (1 <<i);
23+
}
24+
}
25+
if (flag ==true)res = -res;
26+
if (res >Integer.MAX_VALUE)returnInteger.MAX_VALUE;
27+
return (int)res;
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp