Skip to content
Next Next commit
tasks 160- 172, without 161,163,170
  • Loading branch information
IvanMenov committed Nov 18, 2021
commit a3ff6aa7c0889f537af7f516f9a3838e411a2610
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 pA = headA;
ListNode pB = headB;
while (pA != null || pB != null) {
if (pA != null && pB != null && pA.equals(pB)) {
return pA;
}
pA = pA == null ? headB : pA.next;
pB = pB == null ? headA : pB.next;
}
return null;
}
}
21 changes: 21 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,21 @@
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;
}
}
22 changes: 22 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,22 @@
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,44 @@
package g0101_0200.s0165_compare_version_numbers;

public class 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,48 @@
package g0101_0200.s0166_fraction_to_recurring_decimal;

import java.util.HashMap;
import java.util.Map;

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,26 @@
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,12 @@
package g0101_0200.s0168_excel_sheet_column_title;

public class Solution {
public String convertToTitle(int columnNumber) {
String str = "";
while (columnNumber > 0) {
str = String.valueOf((char) (--columnNumber % 26 + 'A')) + str;
columnNumber /= 26;
}
return str;
}
}
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 += Math.pow(26,pow++) * (int)(s.charAt(i)-'A'+1);
}
return num;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g0101_0200.s0172_factorial_trailing_zeroes;

public class 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,30 @@
package g0101_0200.s0160_intersection_of_two_linked_lists;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

import com_github_leetcode.ListNode;

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));

}
}
12 changes: 12 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,12 @@
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,15 @@
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,12 @@
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,12 @@
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,12 @@
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"));
}
}
12 changes: 12 additions & 0 deletions src/test/java/g0101_0200/s0169_majority_element/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package g0101_0200.s0169_majority_element;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;

public class SolutionTest {
@Test
public void majorityElement() {
assertThat(new Solution().majorityElement(new int[] {3,2,3}), equalTo(3));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package g0101_0200.s0171_excel_sheet_column_number;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;

public class SolutionTest {
@Test
public void titleToNumber() {
assertThat(new Solution().titleToNumber("A"), equalTo(1));
}
}
Loading