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 822d921

Browse files
Added solutions 160, 162, 164-169, 171-172.
1 parent f083a9e commit 822d921

File tree

21 files changed

+416
-0
lines changed

21 files changed

+416
-0
lines changed

‎src/main/java/com_github_leetcode/ListNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public ListNode(int val, ListNode next) {
1616
this.next = next;
1717
}
1818

19+
@Override
1920
public String toString() {
2021
StringBuilder result = new StringBuilder("" + val);
2122
ListNode current = next;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0101_0200.s0160_intersection_of_two_linked_lists;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
@SuppressWarnings("java:S2583")
6+
public class Solution {
7+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
8+
ListNode node1 = headA;
9+
ListNode node2 = headB;
10+
while (node1 != node2) {
11+
node1 = node1 == null ? headB : node1.next;
12+
node2 = node2 == null ? headA : node2.next;
13+
}
14+
return node1;
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0101_0200.s0162_find_peak_element;
2+
3+
public class Solution {
4+
public int findPeakElement(int[] nums) {
5+
int start = 0;
6+
int end = nums.length - 1;
7+
8+
while (start < end) {
9+
// This is done because start and end might be big numbers, so it might exceed the
10+
// integer limit.
11+
int mid = start + ((end - start) / 2);
12+
13+
if (nums[mid + 1] > nums[mid]) {
14+
start = mid + 1;
15+
} else {
16+
end = mid;
17+
}
18+
}
19+
20+
return start;
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0101_0200.s0164_maximum_gap;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public int maximumGap(int[] nums) {
7+
if (nums.length < 2) {
8+
return 0;
9+
}
10+
11+
int ret = Integer.MIN_VALUE;
12+
13+
Arrays.sort(nums);
14+
15+
for (int i = 0; i < nums.length - 1; i++) {
16+
if ((nums[i + 1] - nums[i]) > ret) {
17+
ret = (nums[i + 1] - nums[i]);
18+
}
19+
}
20+
21+
return ret;
22+
}
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g0101_0200.s0165_compare_version_numbers;
2+
3+
public class Solution {
4+
5+
public int compareVersion(String version1, String version2) {
6+
// acquire first number
7+
int numA = 0;
8+
int i;
9+
for (i = 0; i < version1.length(); i++) {
10+
char c = version1.charAt(i);
11+
if (c == '.') {
12+
break;
13+
} else {
14+
numA = numA * 10 + (c - 48);
15+
}
16+
}
17+
18+
// acquire second number
19+
int numB = 0;
20+
int j;
21+
for (j = 0; j < version2.length(); j++) {
22+
char c = version2.charAt(j);
23+
if (c == '.') {
24+
break;
25+
} else {
26+
numB = numB * 10 + (c - 48);
27+
}
28+
}
29+
30+
// compare
31+
if (numA > numB) {
32+
return 1;
33+
} else if (numA < numB) {
34+
return -1;
35+
} else { // equal
36+
String v1 = "";
37+
String v2 = "";
38+
39+
if (i != version1.length()) {
40+
v1 = version1.substring(i + 1);
41+
}
42+
43+
if (j != version2.length()) {
44+
v2 = version2.substring(j + 1);
45+
}
46+
47+
// if both versions end here, they are equal
48+
if (v1.equals("") && v2.equals("")) {
49+
return 0;
50+
} else {
51+
return compareVersion(v1, v2);
52+
}
53+
}
54+
}
55+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0101_0200.s0166_fraction_to_recurring_decimal;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
@SuppressWarnings("java:S2153")
7+
public class Solution {
8+
public String fractionToDecimal(int numerator, int denominator) {
9+
if (numerator == 0) {
10+
return "0";
11+
}
12+
13+
StringBuilder sb = new StringBuilder();
14+
15+
// negative case
16+
if (numerator > 0 && denominator < 0 || numerator < 0 && denominator > 0) {
17+
sb.append("-");
18+
}
19+
20+
long x = Math.abs(Long.valueOf(numerator));
21+
long y = Math.abs(Long.valueOf(denominator));
22+
23+
sb.append(String.valueOf(x / y));
24+
25+
long remainder = x % y;
26+
if (remainder == 0) {
27+
return sb.toString();
28+
}
29+
30+
// decimal case
31+
sb.append(".");
32+
33+
// store the remainder in a Hashmap because in the case of recurring decimal, the remainder
34+
// repeats as dividend.
35+
Map<Long, Integer> map = new HashMap<>();
36+
while (remainder != 0) {
37+
if (map.containsKey(remainder)) {
38+
sb.insert(map.get(remainder), "(");
39+
sb.append(")");
40+
break;
41+
}
42+
// store the remainder and the index of it's occurence in the String
43+
map.put(remainder, sb.length());
44+
remainder *= 10;
45+
sb.append(String.valueOf(remainder / y));
46+
remainder %= y;
47+
}
48+
return sb.toString();
49+
}
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0101_0200.s0167_two_sum_ii_input_array_is_sorted;
2+
3+
public class Solution {
4+
public int[] twoSum(int[] numbers, int target) {
5+
int[] res = new int[2];
6+
int i = 0;
7+
int j = numbers.length - 1;
8+
9+
while (i < j) {
10+
int sum = numbers[i] + numbers[j];
11+
if (sum == target) {
12+
res[0] = i + 1;
13+
res[1] = j + 1;
14+
return res;
15+
} else if (sum < target) {
16+
i++;
17+
} else {
18+
j--;
19+
}
20+
}
21+
return res;
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0101_0200.s0168_excel_sheet_column_title;
2+
3+
public class Solution {
4+
public String convertToTitle(int n) {
5+
StringBuilder sb = new StringBuilder();
6+
while (n != 0) {
7+
int remainder = n % 26;
8+
if (remainder == 0) {
9+
remainder += 26;
10+
}
11+
if (n >= remainder) {
12+
n -= remainder;
13+
sb.append((char) (remainder + 64));
14+
}
15+
n /= 26;
16+
}
17+
return sb.reverse().toString();
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0101_0200.s0169_majority_element;
2+
3+
public class Solution {
4+
public int majorityElement(int[] arr) {
5+
int count = 1;
6+
int majority = arr[0];
7+
// For Potential Majority Element
8+
for (int i = 1; i < arr.length; i++) {
9+
if (arr[i] == majority) {
10+
count++;
11+
} else {
12+
if (count > 1) {
13+
count--;
14+
} else {
15+
majority = arr[i];
16+
}
17+
}
18+
}
19+
20+
// For Confirmation
21+
count = 0;
22+
for (int i = 0; i < arr.length; i++) {
23+
if (arr[i] == majority) {
24+
count++;
25+
}
26+
}
27+
28+
if (count >= (arr.length / 2) + 1) {
29+
return majority;
30+
} else {
31+
return -1;
32+
}
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g0101_0200.s0171_excel_sheet_column_number;
2+
3+
public class Solution {
4+
public int titleToNumber(String s) {
5+
int num = 0;
6+
int pow = 0;
7+
for (int i = s.length() - 1; i >= 0; i--) {
8+
num += (int) Math.pow(26, pow++) * (s.charAt(i) - 'A' + 1);
9+
}
10+
return num;
11+
}
12+
}

0 commit comments

Comments
(0)

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