Skip to content

Commit e387e6e

Browse files
author
FreeTymeKiyan
committed
add 58 LengthOfLastWord and update other solutions
1 parent 08741f4 commit e387e6e

File tree

14 files changed

+869
-841
lines changed

14 files changed

+869
-841
lines changed

src/main/java/com/freetymekiyan/algorithms/level/easy/AddBinary.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,55 @@
1616
*/
1717
class AddBinary {
1818

19-
/**
20-
* Math. String.
21-
* Initialize two pointers i and j at the end of a and b.
22-
* Use one integer c for the carry.
23-
* While i >= 0 or j >= 0 or c == 1:
24-
* | Add char in a or 0 to carry c. Move i.
25-
* | Add char in b or 0 to carry c. Move j.
26-
* | c % 2 is the current digit.
27-
* | Insert current digit to the front of result.
28-
* | c / 2 is the next carry.
29-
* Return result string.
30-
*/
31-
public String addBinary(String a, String b) {
32-
StringBuilder sum = new StringBuilder();
33-
int i = a.length() - 1;
34-
int j = b.length() - 1;
35-
int c = 0;
36-
while (i >= 0 || j >= 0 || c == 1) {
37-
c += (i >= 0 ? a.charAt(i--) - '0' : 0);
38-
c += (j >= 0 ? b.charAt(j--) - '0' : 0);
39-
sum.insert(0, c % 2);
40-
c >>= 1;
41-
}
42-
return sum.toString();
19+
/**
20+
* Math. String.
21+
* Initialize two pointers i and j at the end of a and b.
22+
* Use one integer c for the carry.
23+
* While i >= 0 or j >= 0 or c == 1:
24+
* | Add char in a or 0 to carry c. Move i.
25+
* | Add char in b or 0 to carry c. Move j.
26+
* | c % 2 is the current digit.
27+
* | Insert current digit to the front of result.
28+
* | c / 2 is the next carry.
29+
* Return result string.
30+
*/
31+
public String addBinary(String a, String b) {
32+
StringBuilder sum = new StringBuilder();
33+
int i = a.length() - 1;
34+
int j = b.length() - 1;
35+
int c = 0;
36+
while (i >= 0 || j >= 0 || c == 1) {
37+
c += (i >= 0 ? a.charAt(i--) - '0' : 0);
38+
c += (j >= 0 ? b.charAt(j--) - '0' : 0);
39+
sum.insert(0, c % 2);
40+
c >>= 1;
4341
}
42+
return sum.toString();
43+
}
4444

45-
/**
46-
* Math. String.
47-
* From end to start, do it digit-by-digit.
48-
* Get current digits of ab and b, add them up.
49-
* Also use an integer to store carry from the previous addition.
50-
* Store the sum to result and update carry for each round.
51-
* Stop when longest string is reached.
52-
* Remember to check carry before return, if carry is 1, it should still be inserted to the result.
53-
*/
54-
public String addBinary2(String a, String b) {
55-
int m = a.length();
56-
int n = b.length();
57-
int carry = 0;
58-
StringBuilder res = new StringBuilder();
59-
int i = 0;
60-
while (i < m || i < n) { // The longer one of ab and b
61-
int p = i < m ? a.charAt(m - 1 - i) - '0' : 0;
62-
int q = i < n ? b.charAt(n - 1 - i) - '0' : 0;
63-
int temp = p + q + carry; // Sum of current digits and previous carry
64-
carry = temp / 2; // temp can be 0, 1, 2, 3. When temp >= 2, carry = 1; otherwise, carry = 0
65-
res.insert(0, temp % 2); // When temp is odd, result is 1; otherwise, result is 0
66-
i++;
67-
}
68-
return carry == 0 ? res.toString() : "1" + res.toString(); // Don't forget the carry at last.
45+
/**
46+
* Math. String.
47+
* From end to start, do it digit-by-digit.
48+
* Get current digits of ab and b, add them up.
49+
* Also use an integer to store carry from the previous addition.
50+
* Store the sum to result and update carry for each round.
51+
* Stop when longest string is reached.
52+
* Remember to check carry before return, if carry is 1, it should still be inserted to the result.
53+
*/
54+
public String addBinary2(String a, String b) {
55+
int m = a.length();
56+
int n = b.length();
57+
int carry = 0;
58+
StringBuilder res = new StringBuilder();
59+
int i = 0;
60+
while (i < m || i < n) { // The longer one of ab and b
61+
int p = i < m ? a.charAt(m - 1 - i) - '0' : 0;
62+
int q = i < n ? b.charAt(n - 1 - i) - '0' : 0;
63+
int temp = p + q + carry; // Sum of current digits and previous carry
64+
carry = temp / 2; // temp can be 0, 1, 2, 3. When temp >= 2, carry = 1; otherwise, carry = 0
65+
res.insert(0, temp % 2); // When temp is odd, result is 1; otherwise, result is 0
66+
i++;
6967
}
68+
return carry == 0 ? res.toString() : "1" + res.toString(); // Don't forget the carry at last.
69+
}
7070
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.freetymekiyan.algorithms.level.easy;
2+
3+
/**
4+
* 58. Length of Last Word
5+
* <p>
6+
* Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last
7+
* word in the string.
8+
* <p>
9+
* If the last word does not exist, return 0.
10+
* <p>
11+
* Note: A word is defined as a character sequence consists of non-space characters only.
12+
* <p>
13+
* Example:
14+
* <p>
15+
* Input: "Hello World"
16+
* Output: 5
17+
* <p>
18+
* Related Topics: String
19+
*/
20+
public class LengthOfLastWord {
21+
22+
public int lengthOfLastWord(String s) {
23+
s = s.trim();
24+
char space = ' ';
25+
if (s.indexOf(space) == -1) return s.length(); // No space
26+
int len = s.length();
27+
// Find the starting index of the last word
28+
for (int i = len - 1; i >= 0; i--) {
29+
if (s.charAt(i) == ' ' && i != len - 1) {
30+
return len - 1 - i;
31+
}
32+
}
33+
return 0;
34+
}
35+
}

src/main/java/com/freetymekiyan/algorithms/level/easy/PlusOne.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,40 @@
1111
*/
1212
public class PlusOne {
1313

14-
public int[] plusOne(int[] digits) {
15-
for (int i = digits.length - 1; i >= 0; i--) {
16-
digits[i] = 1 + digits[i];
17-
if (digits[i] == 10) { // Carry.
18-
digits[i] = 0;
19-
} else { // No carry, just return.
20-
return digits;
21-
}
22-
}
23-
// Not returned, must have carry.
24-
int[] ans = new int[digits.length + 1];
25-
ans[0] = 1;
26-
for (int i = 0; i < digits.length; i++) {
27-
ans[i + 1] = digits[i];
28-
}
29-
return ans;
14+
public int[] plusOne(int[] digits) {
15+
for (int i = digits.length - 1; i >= 0; i--) {
16+
digits[i] = 1 + digits[i];
17+
if (digits[i] == 10) { // Carry.
18+
digits[i] = 0;
19+
} else { // No carry, just return.
20+
return digits;
21+
}
3022
}
23+
// Not returned, must have carry.
24+
int[] ans = new int[digits.length + 1];
25+
ans[0] = 1;
26+
for (int i = 0; i < digits.length; i++) {
27+
ans[i + 1] = digits[i];
28+
}
29+
return ans;
30+
}
3131

32-
public int[] plusOneB(int[] digits) {
33-
int count = digits.length;
34-
while (count > 0) {
35-
digits[count - 1] = digits[count - 1] + 1;
36-
if (digits[count - 1] > 9) {
37-
digits[count - 1] = 0;
38-
} else {
39-
return digits;
40-
}
41-
count--;
42-
}
43-
int[] result = new int[digits.length + 1];
44-
result[0] = 1;
45-
for (int i = 1; i < digits.length; i++) {
46-
result[i] = digits[i - 1];
47-
}
48-
return result;
32+
public int[] plusOneB(int[] digits) {
33+
int count = digits.length;
34+
while (count > 0) {
35+
digits[count - 1] = digits[count - 1] + 1;
36+
if (digits[count - 1] > 9) {
37+
digits[count - 1] = 0;
38+
} else {
39+
return digits;
40+
}
41+
count--;
42+
}
43+
int[] result = new int[digits.length + 1];
44+
result[0] = 1;
45+
for (int i = 1; i < digits.length; i++) {
46+
result[i] = digits[i - 1];
4947
}
48+
return result;
49+
}
5050
}

src/main/java/com/freetymekiyan/algorithms/level/hard/InsertInterval.java

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,57 +26,57 @@
2626
*/
2727
public class InsertInterval {
2828

29-
/**
30-
* Array. In-place solution.
31-
* Skip the non-overlapping intervals whose end time is before new interval's start.
32-
* For overlapped intervals that start before new interval end.
33-
* | Remove this overlapped interval from list.
34-
* | Merge it with the new interval by: 1. update start to min start times; 2. update end to max end times.
35-
* Add new interval in the position i.
36-
*/
37-
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
38-
int i = 0;
39-
// Skip the non-overlapping intervals.
40-
while (i < intervals.size() && intervals.get(i).end < newInterval.start) {
41-
i++;
42-
}
43-
// Now i == intervals.size() OR intervals.get(i).end >= newInterval.start
44-
while (i < intervals.size() && intervals.get(i).start <= newInterval.end) { // Compare current interval with merged new interval.
45-
Interval inter = intervals.remove(i); // Remove the overlapped interval.
46-
newInterval.start = Math.min(inter.start, newInterval.start);
47-
newInterval.end = Math.max(inter.end, newInterval.end);
48-
}
49-
intervals.add(i, newInterval);
50-
return intervals;
29+
/**
30+
* Array. In-place solution.
31+
* Skip the non-overlapping intervals whose end time is before new interval's start.
32+
* For overlapped intervals that start before new interval end.
33+
* | Remove this overlapped interval from list.
34+
* | Merge it with the new interval by: 1. update start to min start times; 2. update end to max end times.
35+
* Add new interval in the position i.
36+
*/
37+
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
38+
int i = 0;
39+
// Skip the non-overlapping intervals.
40+
while (i < intervals.size() && intervals.get(i).end < newInterval.start) {
41+
i++;
5142
}
43+
// Now i == intervals.size() OR intervals.get(i).end >= newInterval.start
44+
while (i < intervals.size() && intervals.get(i).start <= newInterval.end) { // Compare current interval with merged new interval.
45+
Interval inter = intervals.remove(i); // Remove the overlapped interval.
46+
newInterval.start = Math.min(inter.start, newInterval.start);
47+
newInterval.end = Math.max(inter.end, newInterval.end);
48+
}
49+
intervals.add(i, newInterval);
50+
return intervals;
51+
}
5252

53-
/**
54-
* Array. Sort. Not in place solution. O(n) Time.
55-
* Make use of intervals are sorted.
56-
* Create a result list of intervals, res.
57-
* Add new interval to res first.
58-
* Go through the given intervals.
59-
* For each interval i, there are 3 situations:
60-
* 1) i and previous interval not overlapping, in front of the new interval.
61-
* 2) No overlap, behind the new interval
62-
* 3) Overlap, merge with the new interval
63-
*/
64-
public List<Interval> insertB(List<Interval> intervals, Interval newInterval) {
65-
List<Interval> res = new LinkedList<>();
66-
res.add(newInterval);
67-
for (Interval i : intervals) {
68-
int start = res.get(res.size() - 1).start; // Last interval.
69-
int end = res.get(res.size() - 1).end;
70-
if (i.end < start) { // i ends before last interval.
71-
res.add(res.size() - 1, i); // Insert it before last interval.
72-
} else if (end < i.start) { // i starts after last interval.
73-
res.add(i); // Append it to last.
74-
} else { // i overlaps with last interval.
75-
start = Math.min(start, i.start);
76-
end = Math.max(end, i.end);
77-
res.set(res.size() - 1, new Interval(start, end));
78-
}
79-
}
80-
return res;
53+
/**
54+
* Array. Sort. Not in place solution. O(n) Time.
55+
* Make use of intervals are sorted.
56+
* Create a result list of intervals, res.
57+
* Add new interval to res first.
58+
* Go through the given intervals.
59+
* For each interval i, there are 3 situations:
60+
* 1) i and previous interval not overlapping, in front of the new interval.
61+
* 2) No overlap, behind the new interval
62+
* 3) Overlap, merge with the new interval
63+
*/
64+
public List<Interval> insertB(List<Interval> intervals, Interval newInterval) {
65+
List<Interval> res = new LinkedList<>();
66+
res.add(newInterval);
67+
for (Interval i : intervals) {
68+
int start = res.get(res.size() - 1).start; // Last interval.
69+
int end = res.get(res.size() - 1).end;
70+
if (i.end < start) { // i ends before last interval.
71+
res.add(res.size() - 1, i); // Insert it before last interval.
72+
} else if (end < i.start) { // i starts after last interval.
73+
res.add(i); // Append it to last.
74+
} else { // i overlaps with last interval.
75+
start = Math.min(start, i.start);
76+
end = Math.max(end, i.end);
77+
res.set(res.size() - 1, new Interval(start, end));
78+
}
8179
}
80+
return res;
81+
}
8282
}

0 commit comments

Comments
 (0)