-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday19.java
More file actions
71 lines (62 loc) · 1.96 KB
/
Copy pathday19.java
File metadata and controls
71 lines (62 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//ques1:1614. Maximum Nesting Depth of the Parentheses
//link:https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/description/
class Solution {
public int maxDepth(String s) {
int ans = 0, count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
count++;
ans = Math.max(ans, count);
} else if (s.charAt(i) == ')') {
count--;
}
}
return ans;
}
}
//TC:O(n)
//SC:O(1)
//ques2:13. Roman to Integer
//link:https://leetcode.com/problems/roman-to-integer/description/
class Solution2 {
public int romanToInt(String s) {
int count=0;
int i=0;
for(i=0;i<s.length();i++){
if(s.charAt(i)=='M') count+=1000;
else if(s.charAt(i)=='D') count+=500;
else if(s.charAt(i)=='L') count+=50;
else if(s.charAt(i)=='V') count+=5;
else if(s.charAt(i)=='C'){
if(i<s.length()-1 && s.charAt(i+1)=='D' ){
count+=400;
i++;}
else if(i<s.length()-1 && s.charAt(i+1)=='M' ) {
count+=900;
i++;}
else count+=100;
}
else if(s.charAt(i)=='X'){
if(i<s.length()-1 && s.charAt(i+1)=='L' ) {
count+=40;
i++;}
else if(i<s.length()-1 && s.charAt(i+1)=='C' ){
count+=90;
i++;}
else count+=10;
}
else if(s.charAt(i)=='I'){
if(i<s.length()-1 && s.charAt(i+1)=='V' ){
count+=4;
i++;}
else if(i<s.length()-1 && s.charAt(i+1)=='X' ) {
count+=9;
i++;}
else count+=1;
}
}
return count;
}
}
//TC:O(n)
//SC:O(1)