Skip to content

Commit 1636245

Browse files
committed
Added 2 solutions
1 parent b3a6f5a commit 1636245

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* The read4 API is defined in the parent class Reader4.
2+
int read4(char[] buf); */
3+
4+
public class Solution extends Reader4 {
5+
/**
6+
* @param buf Destination buffer
7+
* @param n Maximum number of characters to read
8+
* @return The number of characters read
9+
*/
10+
public int read(char[] buf, int n) {
11+
int count = 0;
12+
int total = 0;
13+
int read = 4;
14+
while(total <= n && read == 4){
15+
char[] temp = new char[4];
16+
read = read4(temp);
17+
total += read;
18+
19+
for(int i = 0; i < read && count < n; i++) {
20+
buf[count++] = temp[i];
21+
}
22+
}
23+
24+
return count;
25+
}
26+
}

Medium/Score of Parentheses.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public static int scoreOfParentheses(String S) {
3+
Stack<String> st = new Stack<>();
4+
for (int i=0; i<S.length(); i++) {
5+
if (S.charAt(i) == '(') {
6+
st.push(String.valueOf(S.charAt(i)));
7+
}
8+
else {
9+
int num = 0;
10+
while (!st.peek().equals("(")) {
11+
num += Integer.parseInt(st.pop());
12+
}
13+
st.pop();
14+
st.push(num == 0 ? String.valueOf("1") : String.valueOf(2*num));
15+
}
16+
}
17+
18+
int ans = 0;
19+
while (!st.empty()) {
20+
ans += Integer.parseInt(st.pop());
21+
}
22+
23+
return ans;
24+
}
25+
}

0 commit comments

Comments
 (0)