Skip to content

Commit c6d29bc

Browse files
authored
Added tasks 722, 724, 725, 726.
1 parent 9ed523d commit c6d29bc

File tree

12 files changed

+545
-0
lines changed

12 files changed

+545
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g0701_0800.s0722_remove_comments;
2+
3+
// #Medium #Array #String
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<String> removeComments(String[] source) {
10+
List<String> result = new ArrayList<>();
11+
StringBuilder sb = new StringBuilder();
12+
boolean multiComment = false;
13+
for (String line : source) {
14+
int n = line.length();
15+
int index = 0;
16+
while (index < n) {
17+
char ch = line.charAt(index);
18+
if (!multiComment && ch == '/') {
19+
index++;
20+
if (index >= n) {
21+
sb.append(ch);
22+
continue;
23+
}
24+
if (line.charAt(index) == '/') {
25+
break;
26+
} else if (line.charAt(index) == '*') {
27+
multiComment = true;
28+
} else {
29+
sb.append(ch).append(line.charAt(index));
30+
}
31+
} else if (multiComment && ch == '*') {
32+
index++;
33+
if (index >= n) {
34+
continue;
35+
}
36+
if (line.charAt(index) == '/') {
37+
multiComment = false;
38+
} else {
39+
index--;
40+
}
41+
} else if (!multiComment) {
42+
sb.append(ch);
43+
}
44+
45+
index++;
46+
}
47+
48+
if (sb.length() > 0 && !multiComment) {
49+
result.add(sb.toString());
50+
sb.setLength(0);
51+
}
52+
}
53+
54+
return result;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
722. Remove Comments
2+
3+
Medium
4+
5+
Given a C++ program, remove comments from it. The program source is an array of strings `source` where `source[i]` is the <code>i<sup>th</sup></code> line of the source code. This represents the result of splitting the original source code string by the newline character `'
6+
'`.
7+
8+
In C++, there are two types of comments, line comments, and block comments.
9+
10+
* The string `"//"` denotes a line comment, which represents that it and the rest of the characters to the right of it in the same line should be ignored.
11+
* The string `"/*"` denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of `"*/"` should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string `"/*/"` does not yet end the block comment, as the ending would be overlapping the beginning.
12+
13+
The first effective comment takes precedence over others.
14+
15+
* For example, if the string `"//"` occurs in a block comment, it is ignored.
16+
* Similarly, if the string `"/*"` occurs in a line or block comment, it is also ignored.
17+
18+
If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.
19+
20+
There will be no control characters, single quote, or double quote characters.
21+
22+
* For example, `source = "string s = "/* Not a comment. */";"` will not be a test case.
23+
24+
Also, nothing else such as defines or macros will interfere with the comments.
25+
26+
It is guaranteed that every open block comment will eventually be closed, so `"/*"` outside of a line or block comment always starts a new comment.
27+
28+
Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.
29+
30+
After removing the comments from the source code, return _the source code in the same format_.
31+
32+
**Example 1:**
33+
34+
**Input:** source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
35+
36+
**Output:** ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
37+
38+
**Explanation:** The line by line code is visualized as below:
39+
40+
/*Test program */
41+
int main() {
42+
// variable declaration
43+
int a, b, c;
44+
/* This is a test
45+
multiline comment
46+
for testing */
47+
a = b + c;
48+
}
49+
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
50+
The line by line output code is visualized as below:
51+
int main() {
52+
int a, b, c;
53+
a = b + c;
54+
}
55+
56+
**Example 2:**
57+
58+
**Input:** source = ["a/*comment", "line", "more_comment*/b"]
59+
60+
**Output:** ["ab"]
61+
62+
**Explanation:** The original source string is "a/*comment
63+
line
64+
more_comment*/b", where we have bolded the newline characters. After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
65+
66+
**Constraints:**
67+
68+
* `1 <= source.length <= 100`
69+
* `0 <= source[i].length <= 80`
70+
* `source[i]` consists of printable **ASCII** characters.
71+
* Every open block comment is eventually closed.
72+
* There are no single-quote or double-quote in the input.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0701_0800.s0724_find_pivot_index;
2+
3+
// #Easy #Array #Prefix_Sum
4+
5+
public class Solution {
6+
public int pivotIndex(int[] nums) {
7+
if (nums == null || nums.length == 0) {
8+
return -1;
9+
}
10+
int[] sums = new int[nums.length];
11+
sums[0] = nums[0];
12+
for (int i = 1; i < nums.length; i++) {
13+
sums[i] = sums[i - 1] + nums[i];
14+
}
15+
for (int i = 0; i < nums.length; i++) {
16+
int temp = sums[nums.length - 1] - sums[i];
17+
if (i == 0 && 0 == temp || (i > 0 && sums[i - 1] == temp)) {
18+
return i;
19+
}
20+
}
21+
return -1;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
724\. Find Pivot Index
2+
3+
Easy
4+
5+
Given an array of integers `nums`, calculate the **pivot index** of this array.
6+
7+
The **pivot index** is the index where the sum of all the numbers **strictly** to the left of the index is equal to the sum of all the numbers **strictly** to the index's right.
8+
9+
If the index is on the left edge of the array, then the left sum is `0` because there are no elements to the left. This also applies to the right edge of the array.
10+
11+
Return _the **leftmost pivot index**_. If no such index exists, return -1.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,7,3,6,5,6]
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
The pivot index is 3.
22+
23+
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
24+
25+
Right sum = nums[4] + nums[5] = 5 + 6 = 11
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,2,3]
30+
31+
**Output:** -1
32+
33+
**Explanation:**
34+
35+
There is no index that satisfies the conditions in the problem statement.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [2,1,-1]
40+
41+
**Output:** 0
42+
43+
**Explanation:**
44+
45+
The pivot index is 0.
46+
47+
Left sum = 0 (no elements to the left of index 0)
48+
49+
Right sum = nums[1] + nums[2] = 1 + -1 = 0
50+
51+
**Constraints:**
52+
53+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
54+
* `-1000 <= nums[i] <= 1000`
55+
56+
**Note:** This question is the same as 1991: [https://leetcode.com/problems/find-the-middle-index-in-array/](https://leetcode.com/problems/find-the-middle-index-in-array/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0701_0800.s0725_split_linked_list_in_parts;
2+
3+
// #Medium #Linked_List
4+
5+
import com_github_leetcode.ListNode;
6+
import java.util.Objects;
7+
8+
public class Solution {
9+
public ListNode[] splitListToParts(ListNode head, int k) {
10+
int len = getLength(head);
11+
int aveSize = len / k;
12+
int extra = len % k;
13+
ListNode[] result = new ListNode[k];
14+
for (int i = 0; i < k; i++) {
15+
result[i] = head;
16+
int aveSizeTmp = aveSize;
17+
aveSizeTmp += extra-- > 0 ? 1 : 0;
18+
int aveSizeTmp2 = aveSizeTmp;
19+
while (aveSizeTmp-- > 0) {
20+
head = Objects.requireNonNull(head).next;
21+
}
22+
if (result[i] != null) {
23+
ListNode tmp = result[i];
24+
while (tmp.next != null && aveSizeTmp2-- > 1) {
25+
tmp = tmp.next;
26+
}
27+
tmp.next = null;
28+
}
29+
}
30+
return result;
31+
}
32+
33+
private int getLength(ListNode root) {
34+
int len = 0;
35+
ListNode tmp = root;
36+
while (tmp != null) {
37+
len++;
38+
tmp = tmp.next;
39+
}
40+
return len;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
725\. Split Linked List in Parts
2+
3+
Medium
4+
5+
Given the `head` of a singly linked list and an integer `k`, split the linked list into `k` consecutive linked list parts.
6+
7+
The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.
8+
9+
The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.
10+
11+
Return _an array of the_ `k` _parts_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/06/13/split1-lc.jpg)
16+
17+
**Input:** head = [1,2,3], k = 5
18+
19+
**Output:** [[1],[2],[3],[],[]]
20+
21+
**Explanation:**
22+
23+
The first element output[0] has output[0].val = 1, output[0].next = null.
24+
25+
The last element output[4] is null, but its string representation as a ListNode is [].
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/06/13/split2-lc.jpg)
30+
31+
**Input:** head = [1,2,3,4,5,6,7,8,9,10], k = 3
32+
33+
**Output:** [[1,2,3,4],[5,6,7],[8,9,10]]
34+
35+
**Explanation:**
36+
37+
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
38+
39+
**Constraints:**
40+
41+
* The number of nodes in the list is in the range `[0, 1000]`.
42+
* `0 <= Node.val <= 1000`
43+
* `1 <= k <= 50`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package g0701_0800.s0726_number_of_atoms;
2+
3+
// #Hard #String #Hash_Table #Stack
4+
5+
import java.util.ArrayList;
6+
import java.util.Comparator;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Stack;
11+
12+
public class Solution {
13+
private boolean isLower(char c) {
14+
return c >= 97 && c <= 122;
15+
}
16+
17+
private boolean isDigit(char c) {
18+
return c >= 48 && c <= 57;
19+
}
20+
21+
public String countOfAtoms(String formula) {
22+
int product = 1;
23+
Stack<Integer> mlrStack = new Stack<>();
24+
Map<String, Integer> count = new HashMap<>();
25+
for (int i = formula.length() - 1; i >= 0; --i) {
26+
char c = formula.charAt(i);
27+
28+
if (c == '(') {
29+
product /= mlrStack.pop();
30+
continue;
31+
}
32+
33+
int rank = 1;
34+
int mlr = 0;
35+
while (isDigit(c)) {
36+
mlr += rank * (c - 48);
37+
rank *= 10;
38+
c = formula.charAt(--i);
39+
}
40+
if (mlr == 0) {
41+
++mlr;
42+
}
43+
mlrStack.push(mlr);
44+
product *= mlr;
45+
46+
if (c == ')') {
47+
continue;
48+
}
49+
50+
StringBuilder atom = new StringBuilder();
51+
while (isLower(c)) {
52+
atom.insert(0, c);
53+
c = formula.charAt(--i);
54+
}
55+
atom.insert(0, c);
56+
String name = atom.toString();
57+
58+
count.put(name, count.getOrDefault(name, 0) + product);
59+
60+
product /= mlrStack.pop();
61+
}
62+
List<String> atomList = new ArrayList<>(count.keySet());
63+
atomList.sort(Comparator.naturalOrder());
64+
StringBuilder res = new StringBuilder();
65+
for (String atom : atomList) {
66+
res.append(atom);
67+
if (count.get(atom) > 1) {
68+
res.append(count.get(atom));
69+
}
70+
}
71+
return res.toString();
72+
}
73+
}

0 commit comments

Comments
 (0)