Skip to content
1 change: 1 addition & 0 deletions src/main/java/com_github_leetcode/ListNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public ListNode(int val, ListNode next) {
this.next = next;
}

@Override
public String toString() {
StringBuilder result = new StringBuilder("" + val);
ListNode current = next;
Expand Down
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())) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why toString?

Copy link
Contributor Author

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

Copy link
Owner

@javadev javadev Nov 18, 2021

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.

image

node1 = node1 == null ? headB : node1.next;
node2 = node2 == null ? headA : node2.next;
}
return node1;
}
}
22 changes: 22 additions & 0 deletions src/main/java/g0101_0200/s0162_find_peak_element/Solution.java
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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/g0101_0200/s0164_maximum_gap/Solution.java
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use solution without split.

class Solution { public int compareVersion(String version1, String version2) { // acquire first number int numA = 0; int i; for(i=0; i<version1.length(); i++){ char c = version1.charAt(i); if(c == '.'){ break; }else{ numA = numA*10 + ((int)c - 48); } } // acquire second number int numB = 0; int j; for(j=0; j<version2.length(); j++){ char c = version2.charAt(j); if(c == '.'){ break; }else{ numB = numB*10 + ((int)c - 48); } } // compare if(numA > numB){ return 1; }else if(numA < numB){ return -1; }else{ // equal String v1 = ""; String v2 = ""; if(i != version1.length()) v1 = version1.substring(i+1); if(j != version2.length()) v2 = version2.substring(j+1); // if both versions end here, they are equal if(v1.equals("") && v2.equals("")){ return 0; }else{ return compareVersion(v1, v2); } } } }
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();
}
}
34 changes: 34 additions & 0 deletions src/main/java/g0101_0200/s0169_majority_element/Solution.java
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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why static?

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))))));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the same ListNode(8) for both lists.


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));
}
}
13 changes: 13 additions & 0 deletions src/test/java/g0101_0200/s0162_find_peak_element/SolutionTest.java
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));
}
}
14 changes: 14 additions & 0 deletions src/test/java/g0101_0200/s0164_maximum_gap/SolutionTest.java
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"));
}
}
Loading