- Notifications
You must be signed in to change notification settings - Fork 97
tasks 160- 172, without 161,163,170 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
a3ff6aa
83b71ab
6144148
dc0313a
910303a
52b5658
6d7883f
4ac7d26
4a28f8f
7e2696f
eec5520
64522f3
79efb99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package g0101_0200.s0160_intersection_of_two_linked_lists; | ||
| ||
import com_github_leetcode.ListNode; | ||
| ||
public class Solution { | ||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { | ||
ListNode node1 = headA; | ||
ListNode node2 = headB; | ||
while (!node1.toString().equals(node2.toString())) { | ||
node1 = node1 == null ? headB : node1.next; | ||
node2 = node2 == null ? headA : node2.next; | ||
} | ||
return node1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package g0101_0200.s0162_find_peak_element; | ||
| ||
public class Solution { | ||
public int findPeakElement(int[] nums) { | ||
int start = 0; | ||
int end = nums.length - 1; | ||
| ||
while (start < end) { | ||
// This is done because start and end might be big numbers, so it might exceed the | ||
// integer limit. | ||
int mid = start + ((end - start) / 2); | ||
| ||
if (nums[mid + 1] > nums[mid]) { | ||
start = mid + 1; | ||
} else { | ||
end = mid; | ||
} | ||
} | ||
| ||
return start; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package g0101_0200.s0164_maximum_gap; | ||
| ||
import java.util.Arrays; | ||
| ||
public class Solution { | ||
public int maximumGap(int[] nums) { | ||
if (nums.length < 2) { | ||
return 0; | ||
} | ||
| ||
int ret = Integer.MIN_VALUE; | ||
| ||
Arrays.sort(nums); | ||
| ||
for (int i = 0; i < nums.length - 1; i++) { | ||
if ((nums[i + 1] - nums[i]) > ret) { | ||
ret = (nums[i + 1] - nums[i]); | ||
} | ||
} | ||
| ||
return ret; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package g0101_0200.s0165_compare_version_numbers; | ||
| ||
public class Solution { | ||
private Solution() {} | ||
| ||
public static int compareVersion(String version1, String version2) { | ||
String[] ver1 = version1.split("\\.", 0); | ||
String[] ver2 = version2.split("\\.", 0); | ||
| ||
int i = 0; | ||
while (i < ver1.length && i < ver2.length) { | ||
String s1 = removeLeadingZero(ver1[i]); | ||
String s2 = removeLeadingZero(ver2[i]); | ||
if (Integer.valueOf(s1) > Integer.valueOf(s2)) { | ||
return 1; | ||
} else if (Integer.valueOf(s1) < Integer.valueOf(s2)) { | ||
return -1; | ||
} | ||
i++; | ||
} | ||
while (i < ver1.length) { | ||
String s1 = removeLeadingZero(ver1[i]); | ||
i++; | ||
if (Integer.valueOf(s1) == 0) { | ||
continue; | ||
} | ||
return (Integer.valueOf(s1) > 0) ? 1 : -1; | ||
} | ||
while (i < ver2.length) { | ||
String s2 = removeLeadingZero(ver2[i]); | ||
i++; | ||
if (Integer.valueOf(s2) == 0) { | ||
continue; | ||
} | ||
return (Integer.valueOf(s2) > 0) ? -1 : 1; | ||
} | ||
return 0; | ||
} | ||
| ||
private static String removeLeadingZero(String s) { | ||
int i = 0; | ||
while (i < s.length() && s.charAt(i) == '0') { | ||
i++; | ||
} | ||
return (i != s.length()) ? s.substring(i) : "0"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package g0101_0200.s0166_fraction_to_recurring_decimal; | ||
| ||
import java.util.HashMap; | ||
import java.util.Map; | ||
| ||
@SuppressWarnings("java:S2153") | ||
public class Solution { | ||
public String fractionToDecimal(int numerator, int denominator) { | ||
if (numerator == 0) { | ||
return "0"; | ||
} | ||
| ||
StringBuilder sb = new StringBuilder(); | ||
| ||
// negative case | ||
if (numerator > 0 && denominator < 0 || numerator < 0 && denominator > 0) { | ||
sb.append("-"); | ||
} | ||
| ||
long x = Math.abs(Long.valueOf(numerator)); | ||
long y = Math.abs(Long.valueOf(denominator)); | ||
| ||
sb.append(String.valueOf(x / y)); | ||
| ||
long remainder = x % y; | ||
if (remainder == 0) { | ||
return sb.toString(); | ||
} | ||
| ||
// decimal case | ||
sb.append("."); | ||
| ||
// store the remainder in a Hashmap because in the case of recurring decimal, the remainder | ||
// repeats as dividend. | ||
Map<Long, Integer> map = new HashMap<>(); | ||
while (remainder != 0) { | ||
if (map.containsKey(remainder)) { | ||
sb.insert(map.get(remainder), "("); | ||
sb.append(")"); | ||
break; | ||
} | ||
// store the remainder and the index of it's occurence in the String | ||
map.put(remainder, sb.length()); | ||
remainder *= 10; | ||
sb.append(String.valueOf(remainder / y)); | ||
remainder %= y; | ||
} | ||
return sb.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package g0101_0200.s0167_two_sum_ii_input_array_is_sorted; | ||
| ||
public class Solution { | ||
public int[] twoSum(int[] numbers, int target) { | ||
int[] res = new int[2]; | ||
int i = 0; | ||
int j = numbers.length - 1; | ||
| ||
while (i < j) { | ||
int sum = numbers[i] + numbers[j]; | ||
if (sum == target) { | ||
res[0] = i + 1; | ||
res[1] = j + 1; | ||
return res; | ||
} else if (sum < target) { | ||
i++; | ||
} else { | ||
j--; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package g0101_0200.s0168_excel_sheet_column_title; | ||
| ||
public class Solution { | ||
public String convertToTitle(int n) { | ||
StringBuilder sb = new StringBuilder(); | ||
while (n != 0) { | ||
int remainder = n % 26; | ||
if (remainder == 0) { | ||
remainder += 26; | ||
} | ||
if (n >= remainder) { | ||
n -= remainder; | ||
sb.append((char) (remainder + 64)); | ||
} | ||
n /= 26; | ||
} | ||
return sb.reverse().toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package g0101_0200.s0169_majority_element; | ||
| ||
public class Solution { | ||
public int majorityElement(int[] arr) { | ||
int count = 1; | ||
int majority = arr[0]; | ||
// For Potential Majority Element | ||
for (int i = 1; i < arr.length; i++) { | ||
if (arr[i] == majority) { | ||
count++; | ||
} else { | ||
if (count > 1) { | ||
count--; | ||
} else { | ||
majority = arr[i]; | ||
} | ||
} | ||
} | ||
| ||
// For Confirmation | ||
count = 0; | ||
for (int i = 0; i < arr.length; i++) { | ||
if (arr[i] == majority) { | ||
count++; | ||
} | ||
} | ||
| ||
if (count >= (arr.length / 2) + 1) { | ||
return majority; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package g0101_0200.s0171_excel_sheet_column_number; | ||
| ||
public class Solution { | ||
public int titleToNumber(String s) { | ||
int num = 0; | ||
int pow = 0; | ||
for (int i = s.length() - 1; i >= 0; i--) { | ||
num += (int) Math.pow(26, pow++) * (s.charAt(i) - 'A' + 1); | ||
} | ||
return num; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package g0101_0200.s0172_factorial_trailing_zeroes; | ||
| ||
public class Solution { | ||
private Solution() {} | ||
| ||
public static int trailingZeroes(int n) { | ||
| ||
int base = 5; | ||
int count = 0; | ||
while (n >= base) { | ||
count += n / base; | ||
base = base * 5; | ||
} | ||
return count; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package g0101_0200.s0160_intersection_of_two_linked_lists; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import com_github_leetcode.ListNode; | ||
import org.junit.Test; | ||
| ||
public class SolutionTest { | ||
@Test | ||
public void getIntersectionNode() { | ||
ListNode nodeA = | ||
new ListNode(4, new ListNode(1, new ListNode(8, new ListNode(4, new ListNode(5))))); | ||
ListNode nodeB = | ||
new ListNode( | ||
5, | ||
new ListNode( | ||
6, | ||
new ListNode( | ||
1, new ListNode(8, new ListNode(4, new ListNode(5)))))); | ||
| ||
| ||
int intersectVal = 8; | ||
int skipA = 2; | ||
int skipB = 3; | ||
| ||
for (int i = 0; i < skipB; i++) { | ||
if (i < skipA) { | ||
nodeA = nodeA.next; | ||
} | ||
nodeB = nodeB.next; | ||
} | ||
| ||
assertThat(new Solution().getIntersectionNode(nodeA, nodeB).val, equalTo(intersectVal)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package g0101_0200.s0162_find_peak_element; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.Test; | ||
| ||
public class SolutionTest { | ||
@Test | ||
public void findPeakElement() { | ||
assertThat(new Solution().findPeakElement(new int[] {1, 2, 3, 1}), equalTo(2)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package g0101_0200.s0164_maximum_gap; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.Test; | ||
| ||
public class SolutionTest { | ||
@Test | ||
public void maximumGap() { | ||
| ||
assertThat(new Solution().maximumGap(new int[] {3, 6, 9, 1}), equalTo(3)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package g0101_0200.s0165_compare_version_numbers; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.Test; | ||
| ||
public class SolutionTest { | ||
@Test | ||
public void compareVersion() { | ||
| ||
assertThat(Solution.compareVersion("1.01", "1.001"), equalTo(0)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package g0101_0200.s0166_fraction_to_recurring_decimal; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.Test; | ||
| ||
public class SolutionTest { | ||
@Test | ||
public void fractionToDecimal() { | ||
assertThat(new Solution().fractionToDecimal(1, 2), equalTo("0.5")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package g0101_0200.s0167_two_sum_ii_input_array_is_sorted; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.Test; | ||
| ||
public class SolutionTest { | ||
@Test | ||
public void twoSum() { | ||
assertThat(new Solution().twoSum(new int[] {2, 7, 11, 15}, 9), equalTo(new int[] {1, 2})); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package g0101_0200.s0168_excel_sheet_column_title; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.Test; | ||
| ||
public class SolutionTest { | ||
@Test | ||
public void convertToTitle() { | ||
assertThat(new Solution().convertToTitle(1), equalTo("A")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why toString?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above implementation, that you wrote results in nullpoinerexception in the test case, as node1 == node2 evaluates to false
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it in leetcode. There are no exceptions.