Skip to content

Commit 8eb7ce4

Browse files
committed
feat: add 69. Sqrt
1 parent 38c6fa9 commit 8eb7ce4

File tree

4 files changed

+233
-4
lines changed

4 files changed

+233
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
with a focus on <strong> readability </strong> consistent style, and adherence to modern standards across diverse topics. This repository is complemented by in-depth Medium blogs, offering <strong>comprehensive explanations</strong> for each solution</span>
1212
<br>
1313
<br>
14-
<img src="https://img.shields.io/badge/Solved-86/3422%20=%202.15%25-blue.svg?style=flat-square" />
14+
<img src="https://img.shields.io/badge/Solved-89/3555%20=%202.15%25-blue.svg?style=flat-square" />
1515
<br/>
16-
<img src="https://img.shields.io/badge/Easy-48/850-5CB85C.svg?style=flat-square"/>
17-
<img src="https://img.shields.io/badge/Medium-34/1783-F0AD4E.svg?style=flat-square"/>
18-
<img src="https://img.shields.io/badge/Hard-4/779-D9534F.svg?style=flat-square"/>
16+
<img src="https://img.shields.io/badge/Easy-49/877-5CB85C.svg?style=flat-square"/>
17+
<img src="https://img.shields.io/badge/Medium-36/1843-F0AD4E.svg?style=flat-square"/>
18+
<img src="https://img.shields.io/badge/Hard-4/835-D9534F.svg?style=flat-square"/>
1919
<br/>
2020
</p>
2121

@@ -44,6 +44,7 @@ Medium: https://medium.com/@nphausg
4444
| [0020. ValidParentheses](https://leetcode.com/problems/valid-parentheses) | [Solution](src/com/nphausg/leetcode/easy/ValidParentheses.java) | - | - |
4545
| [0026. Remove Duplicates](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | [Solution](src/com/nphausg/leetcode/easy/RemoveDuplicates.java) | - | - |
4646
| [0027. Remove Element](https://leetcode.com/problems/remove-element) | [Solution](src/com/nphausg/leetcode/easy/RemoveElement.java) | - | - |
47+
| [0069. SqrtX](https://leetcode.com/problems/sqrtx) | [Solution](src/com/nphausg/leetcode/easy/Sqrt.java) | - | - |
4748
| [0104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree) | [Solution](src/com/nphausg/leetcode/easy/MaximumDepthOfBinaryTree.java) | - | - |
4849
| [0111. Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree) | [Solution](src/com/nphausg/leetcode/easy/MinimumDepthOfBinaryTree.java) | - | - |
4950
| [0125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | [Solution](src/com/nphausg/leetcode/easy/ValidPalindrome.java) | [Solution](src/com/nphausg/leetcode/easy/ValidPalindromeKt.kt) | - |
@@ -95,6 +96,7 @@ Medium: https://medium.com/@nphausg
9596
| [0107. Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii) | [Solution](src/com/nphausg/leetcode/medium/BinaryTreeLevelOrderTraversalII.java) | - | - |
9697
| [0122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [Solution](src/com/nphausg/leetcode/medium/BestTimeToBuyAndSellStockII.java) | - | [Medium](https://nphausg.medium.com/leetcode-122-best-time-to-buy-and-sell-stock-ii-all-solutions-explained-366666e80c47) |
9798
| [0137. Single Number II](https://leetcode.com/problems/single-number-ii) | [Solution](src/com/nphausg/leetcode/medium/SingleNumberII.java) | - | - |
99+
| [0146. LRU Cache](https://leetcode.com/problems/lru-cache) | [Solution](src/com/nphausg/leetcode/medium/LruCacheLinkedHashMap.java) | - | - |
98100
| [0198. House Robber](https://leetcode.com/problems/house-robber) | [Solution](src/com/nphausg/leetcode/medium/HouseRobber.java) | - | - |
99101
| [0208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) | [Solution](src/com/nphausg/leetcode/medium/TriePrefixTree.java) | [Solution](src/com/nphausg/leetcode/medium/TriePrefixTreeKt.kt) | [Medium](https://medium.com/@nphausg/leetcode-208-implement-trie-prefix-tree-in-java-and-kotlin-7f6c493d8c2a) |
100102
| [0219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii) | [Solution](src/com/nphausg/leetcode/easy/ContainsDuplicate2.java) | - | [Medium](https://nphausg.medium.com/leetcode-contains-duplicate-ii-fb18e71189fb) |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.nphausg.leetcode.easy;
2+
3+
import com.nphausg.leetcode.config.BaseTest;
4+
import org.junit.experimental.runners.Enclosed;
5+
import org.junit.runner.RunWith;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* <a href="https://leetcode.com/problems/sqrtx/">69. Sqrt</a>
11+
* idea: r * r <= x < (r + 1) * (r + 1)
12+
*/
13+
@RunWith(Enclosed.class)
14+
public class Sqrt {
15+
16+
public static int mySqrt(int x) {
17+
if (x < 0) return 0;
18+
if (x < 2) return x;
19+
long r = x;
20+
while (r * r > x) {
21+
r = (r + x / r) / 2;
22+
}
23+
return (int) r;
24+
}
25+
26+
public static int mySqrt1(int x) {
27+
if (x < 0) return 0;
28+
if (x < 2) return x;
29+
for (int i = 1; i <= x / 2; i++) {
30+
if (i <= x / i && (i + 1) > x / (i + 1)) {
31+
return i;
32+
}
33+
}
34+
return 1;
35+
}
36+
37+
public static class TestCase extends BaseTest {
38+
39+
@org.junit.Test
40+
public void testCases() {
41+
assertEquals(0, mySqrt(-1));
42+
assertEquals(0, mySqrt(0));
43+
assertEquals(1, mySqrt(1));
44+
assertEquals(2, mySqrt(4));
45+
assertEquals(2, mySqrt(8));
46+
assertEquals(46339, mySqrt(2147395599));
47+
}
48+
}
49+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.nphausg.leetcode.easy
2+
3+
/**
4+
* <a href="https://leetcode.com/problems/sqrtx/">69. Sqrt</a>
5+
*/
6+
class SqrtKt {
7+
}

src/com/nphausg/leetcode/utils/sqrt_method_comparison.ipynb

Lines changed: 171 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)