Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,16 @@
package g0101_0200.s0160_intersection_of_two_linked_lists;

import com_github_leetcode.ListNode;

@SuppressWarnings("java:S2583")
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode node1 = headA;
ListNode node2 = headB;
while (node1 != node2) {
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,55 @@
package g0101_0200.s0165_compare_version_numbers;

public 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 + (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 + (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);
}
}
}
}
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,14 @@
package g0101_0200.s0172_factorial_trailing_zeroes;

public class Solution {

public 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,28 @@
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 intersectionListNode = new ListNode(8, new ListNode(4, new ListNode(5)));
ListNode nodeA = new ListNode(4, new ListNode(1, intersectionListNode));
ListNode nodeB = new ListNode(5, new ListNode(6, new ListNode(1, intersectionListNode)));

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(new 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}));
}
}
Loading