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

Commit a718d3d

Browse files
committed
sqrt x: done
1 parent 04350de commit a718d3d

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

‎src/main/java/com/leetcode/arrays/binarysearch/PowXN.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static double myPowNaive(double x, int n) {
6363
*/
6464
public static double myPow(double x, int n) {
6565
double res = 1;
66-
long absN = Math.abs((long) n);
66+
long absN = Math.abs((long) n);// used a long so that `absN / 2` doesn't overflow
6767

6868
while (absN > 0) {
6969
if (absN % 2 == 1) res *= x; // store whatever we have calculated so far in the final result

‎src/main/java/com/leetcode/arrays/binarysearch/SearchInsertPosition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* Example 4:
2727
* Input: [1,3,5,6], 0
2828
* Output: 0
29-
*
29+
* <p>
3030
* Similar question: {@link SmallestLetterGreaterThanTarget}.
3131
*
3232
* @author rampatra
@@ -59,5 +59,6 @@ public static int searchInsert(int[] nums, int target) {
5959

6060
public static void main(String[] args) {
6161
assertEquals(2, searchInsert(new int[]{1, 2}, 3));
62+
assertEquals(1, searchInsert(new int[]{1, 3, 5, 6}, 2));
6263
}
6364
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.leetcode.arrays.binarysearch;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
/**
6+
* Level: Easy
7+
* Link: https://leetcode.com/problems/sqrtx/
8+
* Description:
9+
* Implement int sqrt(int x).
10+
*
11+
* Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
12+
*
13+
* Since the return type is an integer, the decimal digits are truncated and only the integer part
14+
* of the result is returned.
15+
*
16+
* Example 1:
17+
* Input: 4
18+
* Output: 2
19+
*
20+
* Example 2:
21+
* Input: 8
22+
* Output: 2
23+
* Explanation: The square root of 8 is 2.82842..., and since
24+
* the decimal part is truncated, 2 is returned.
25+
*
26+
* @author rampatra
27+
* @since 2019年08月19日
28+
*/
29+
public class SqrtX {
30+
31+
/**
32+
* Runtime: <a href="https://leetcode.com/submissions/detail/253282868/">1 ms</a>.
33+
*
34+
* @param x
35+
* @return
36+
*/
37+
public static int mySqrt(int x) {
38+
if (x == 0 || x == 1) {
39+
return x;
40+
}
41+
long low = 1;
42+
long high = x / 2;
43+
44+
while (low <= high) {
45+
long mid = low + (high - low) / 2;
46+
if (mid * mid == x) {
47+
return (int) mid;
48+
} else if (mid * mid < x) {
49+
low = mid + 1;
50+
} else {
51+
high = mid - 1;
52+
}
53+
}
54+
return (int) high;
55+
}
56+
57+
public static void main(String[] args) {
58+
assertEquals(2, mySqrt(8));
59+
assertEquals(3, mySqrt(9));
60+
assertEquals(46339, mySqrt(2147395599));
61+
}
62+
}

‎src/main/java/com/leetcode/strings/AnagramsInString.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,40 @@
55
import java.util.List;
66

77
/**
8-
* Level: Easy
8+
* Level: Medium
99
* Problem: https://leetcode.com/problems/find-all-anagrams-in-a-string/
10+
* Description:
11+
* Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
12+
*
13+
* Strings consists of lowercase English letters only and the length of both strings s and p will not be larger
14+
* than 20,100.
15+
*
16+
* The order of output does not matter.
17+
*
18+
* Example 1:
19+
*
20+
* Input:
21+
* s: "cbaebabacd" p: "abc"
22+
*
23+
* Output:
24+
* [0, 6]
25+
*
26+
* Explanation:
27+
* The substring with start index = 0 is "cba", which is an anagram of "abc".
28+
* The substring with start index = 6 is "bac", which is an anagram of "abc".
29+
*
30+
* Example 2:
31+
*
32+
* Input:
33+
* s: "abab" p: "ab"
34+
*
35+
* Output:
36+
* [0, 1, 2]
37+
*
38+
* Explanation:
39+
* The substring with start index = 0 is "ab", which is an anagram of "ab".
40+
* The substring with start index = 1 is "ba", which is an anagram of "ab".
41+
* The substring with start index = 2 is "ab", which is an anagram of "ab".
1042
*
1143
* @author rampatra
1244
* @since 2019年04月13日

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /