Skip to content

Commit

Permalink
[Math] Add solution to Divide Two Integers
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Apr 5, 2018
1 parent c39619d commit 2b1c75e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 30 additions & 0 deletions Math/DivideTwoIntegers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Question Link: https://leetcode.com/problems/divide-two-integers/
* Primary idea: Use left shift and subtraction to get the number of every digit
* Time Complexity: O(logn), Space Complexity: O(1)
*
*/

class DivideTwoIntegers {
func divide(_ dividend: Int, _ divisor: Int) -> Int {
if divisor == 0 {
return Int.max
}

let isNegative = (dividend < 0) != (divisor < 0)
var dividend = abs(dividend), divisor = abs(divisor), count = 0

while dividend >= divisor {
var shift = 0

while dividend >= (divisor << shift) {
shift += 1
}

dividend -= divisor << (shift - 1)
count += 1 << (shift - 1)
}

return isNegative ? -count : count
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 228 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 229 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).


## Array
Expand Down Expand Up @@ -227,6 +227,7 @@
[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| [Swift](./Math/AddTwoNumbers.swift)| Medium| O(n)| O(1)|
[Add Digits](https://leetcode.com/problems/add-digits/)| [Swift](./Math/AddDigits.swift)| Easy| O(1)| O(1)|
[Plus One](https://leetcode.com/problems/plus-one/)| [Swift](./Math/PlusOne.swift)| Easy| O(n)| O(1)|
[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)| [Swift](./Math/DivideTwoIntegers.swift)| Medium| O(logn)| O(1)|
[Number Complement](https://leetcode.com/problems/number-complement/)| [Swift](./Math/NumberComplement.swift)| Easy| O(n)| O(1)|
[Hamming Distance](https://leetcode.com/problems/hamming-distance/)| [Swift](./Math/HammingDistance.swift)| Easy| O(n)| O(1)|
[Integer Break](https://leetcode.com/problems/integer-break/)| [Swift](./Math/IntegerBreak.swift)| Medium| O(logn)| O(1)|
Expand Down Expand Up @@ -751,7 +752,7 @@
| [Swift](./Stack/LongestValidParentheses.swift) | 32 | [Longest Valid Parentheses](https://oj.leetcode.com/problems/longest-valid-parentheses/) | Hard |
| [Swift](./Array/NextPermutation.swift) | 31 | [Next Permutation](https://oj.leetcode.com/problems/next-permutation/) | Medium |
| | 30 | [Substring with Concatenation of All Words](https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/) | Hard |
| | 29 | [Divide Two Integers](https://oj.leetcode.com/problems/divide-two-integers/) | Medium |
| [Swift](./Math/DivideTwoIntegers.swift) | 29 | [Divide Two Integers](https://oj.leetcode.com/problems/divide-two-integers/) | Medium |
| [Swift](./String/StrStr.swift) | 28 | [Implement strStr()](https://oj.leetcode.com/problems/implement-strstr/) | Easy |
| [Swfit](./Array/RemoveElement.swift) | 27 | [Remove Element](https://oj.leetcode.com/problems/remove-element/) | Easy |
| [Swift](./Array/RemoveDuplicatesFromSortedArray.swift) | 26 | [Remove Duplicates from Sorted Array](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/) | Easy |
Expand Down

0 comments on commit 2b1c75e

Please sign in to comment.