LeetCode第131场周赛(JAVA语言)
class Solution {
public String removeOuterParentheses(String S) {
Stack<String> s = new Stack<String>();
String str = "";
for(int i=0;i<S.length();i++) {
if(S.charAt(i)=='('&&s.isEmpty()) {
s.add("(");
}else if(S.charAt(i)=='(') {
s.add("(");
str+="(";
}else if(S.charAt(i)==')'){
String s1 = s.pop();
if(!s.isEmpty()) {
str+=")";
}
}
}
return str;
}
}
import java.math.BigInteger;
class Solution {
List<String> l1= new LinkedList<String>();
String sum = "";
public int sumRootToLeaf(TreeNode root) {
if(root==null) {
return 0;
}
dfs(root,sum,l1);
//1l 1-1-100101 =>2;
BigInteger count = BigInteger.ZERO;
for(int i=0;i<l1.size();i++) {
BigInteger b = new BigInteger(l1.get(i), 2);
count = count.add(b);
}
BigInteger j = new BigInteger("1000000007");
count = count.mod(j);
return count.intValue();
}
private void dfs(TreeNode root, String sum, List<String> l1) {
// TODO Auto-generated method stub
sum += root.val;
if(root.right==null&&root.left==null) {
l1.add(sum);
}
if(root.left!=null) {
dfs(root.left,sum,l1);
}
if(root.right!=null) {
dfs(root.right,sum,l1);
}
}
}
class Solution {
public List<Boolean> camelMatch(String[] queries, String pattern) {
char[] pt = pattern.toCharArray();
int m = pt.length;
List<Boolean> ret = new ArrayList<>();
outer:
for(String q : queries){
int p = 0;
for(char c : q.toCharArray()){
if(p < m && pt[p] == c){
p++;
}else{
if(!('a' <= c && c <= 'z')){
ret.add(false);
continue outer;
}
}
}
ret.add(p == m);
}
return ret;
}
}
class sp{
int x,y;
}
class Solution {
LinkedList<sp> l1= new LinkedList<>();
public int videoStitching(int[][] clips, int T) {
for(int i=0;i<clips.length;i++) {
sp s = new sp();
s.x = clips[i][0];
s.y = clips[i][1];
l1.add(s);
}
l1.sort(new Comparator<sp>() {
@Override
public int compare(sp o1, sp o2) {
// TODO Auto-generated method stub
if(o1.x==o2.x) {
return o2.y - o1.y;
}
return o1.x - o2.x;
}
});
/*for(int i=0;i<l1.size();i++) {
System.out.println(l1.get(i).x + " " +l1.get(i).y);
}*/
if(l1.get(0).x!=0) {
return -1;
}
int n = clips.length;
int now=0,ans=0,i=0;
while(now<T&&i<clips.length) {
int max = now;
while(i<n&&l1.get(i).x<=now) {
max = Math.max(max, l1.get(i).y);
i++;
}
if(max == now) {
break;
}
now = max;
ans++;
}
if(now<T) {
return -1;
}
return ans;
}
}
结束