首页 > 代码库 > 每日刷题总结

每日刷题总结

2017-5-5

https://leetcode.com/problems/valid-parentheses/#/description

 

题目:Given a string containing just the characters ‘(‘‘)‘‘{‘‘}‘‘[‘ and ‘]‘, determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

技术分享
 1 public class Solution {
 2     public boolean isValid(String s) {
 3         char[] result = s.toCharArray();
 4         Stack<Character> stack = new Stack<>();
 5         if (result.length % 2 == 1) return false;
 6          for (int i = 0; i < result.length; i++) {
 7             stack.push(result[i]);
 8             if (stack.peek() == ‘)‘) {
 9                 stack.pop();
10                 if( stack.empty() || stack.pop() != ‘(‘) return false;
11             } else if (stack.peek() == ‘]‘) {
12                 stack.pop();
13                 if ( stack.empty() || stack.pop() != ‘[‘) return false;
14             } else if (stack.peek() == ‘}‘) {
15                 stack.pop();
16                 if (stack.empty() || stack.pop() != ‘{‘) return false;
17             }
18         }
19         if (!stack.empty()) {
20            return false;
21         }
22         return true;
23     }
24 }
View Code

http://www.jiuzhang.com/solution/valid-parentheses/  

技术分享
 1 public class Solution {
 2     public boolean isValidParentheses(String s) {
 3         Stack<Character> stack = new Stack<Character>();
 4         for (Character c : s.toCharArray()) {
 5         if ("({[".contains(String.valueOf(c))) {
 6                 stack.push(c);
 7             } else {
 8                if (!stack.isEmpty() && is_valid(stack.peek(), c)) {
 9                    stack.pop();
10                } else {
11                    return false;
12                }
13            }
14        }
15        return stack.isEmpty();
16     }
17 
18     private boolean is_valid(char c1, char c2) {
19         return (c1 == ‘(‘ && c2 == ‘)‘) || (c1 == ‘{‘ && c2 == ‘}‘)
20             || (c1 == ‘[‘ && c2 == ‘]‘);
21     }
22 }
View Code

分析:String转char写法

String s;
    for (char c : s.toCharArray()) {

discuss 

技术分享
public boolean isValid(String s) {
    Stack<Character> stack = new Stack<Character>();
    for (char c : s.toCharArray()) {
        if (c == ‘(‘)
            stack.push(‘)‘);
        else if (c == ‘{‘)
            stack.push(‘}‘);
        else if (c == ‘[‘)
            stack.push(‘]‘);
        else if (stack.isEmpty() || stack.pop() != c)
            return false;
    }
    return stack.isEmpty();
}
View Code

这个和我的差不多

技术分享
public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        // Iterate through string until empty
        for(int i = 0; i<s.length(); i++) {
            // Push any open parentheses onto stack
            if(s.charAt(i) == ‘(‘ || s.charAt(i) == ‘[‘ || s.charAt(i) == ‘{‘)
                stack.push(s.charAt(i));
            // Check stack for corresponding closing parentheses, false if not valid
            else if(s.charAt(i) == ‘)‘ && !stack.empty() && stack.peek() == ‘(‘)
                stack.pop();
            else if(s.charAt(i) == ‘]‘ && !stack.empty() && stack.peek() == ‘[‘)
                stack.pop();
            else if(s.charAt(i) == ‘}‘ && !stack.empty() && stack.peek() == ‘{‘)
                stack.pop();
            else
                return false;
        }
        // return true if no open parentheses left in stack
        return stack.empty();
    }
}
View Code

最短时间?用switch case

技术分享
public class Solution {
    public boolean isValid(String s){
    char[] cArr=new char[s.length()];
    int head=0;
    for (char c : s.toCharArray()){
        switch(c){
            case ‘(‘:
            case ‘[‘:
            case ‘{‘: cArr[head++]=c;break;
            case ‘)‘: if(head==0||cArr[--head]!=‘(‘) return false;break;
            case ‘]‘: if(head==0||cArr[--head]!=‘[‘) return false;break;
            case ‘}‘: if(head==0||cArr[--head]!=‘{‘) return false;break;                    
        }
    
    }
    return head==0;    
}

}
View Code

 

题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

https://leetcode.com/problems/merge-two-sorted-lists/#/description

 我的超复杂解法

遇到的问题就是first node怎么设置 

技术分享
 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11         if (l1 == null && l2 == null) {
12             return null;
13         } else if (l2 == null) { //l1 != null buxing???
14             return l1;
15         } else if (l1 == null) {
16             return l2;
17         } else {
18             ListNode result; //chu shi hua?
19             ListNode tmp;
20             if (l1.val <= l2.val) {
21                 tmp = l1;
22                 result = tmp; //result = l1 wrong
23                 l1 = l1.next;
24             } else {
25                 result = l2;
26                 tmp = l2;
27                 l2 = l2.next;
28             }
29             while (l1 != null && l2 != null){ //wrong
30                 if (l1.val <= l2.val) {
31                     tmp.next = l1;
32                     tmp = tmp.next;          //wrong
33                     l1 = l1.next;
34                 } else if (l2.val < l1.val) {
35                     tmp.next = l2;
36                     tmp = tmp.next;
37                     l2 = l2.next;
38                 }
39             }
40             if (l1 == null) {
41                 tmp.next = l2;
42             } else  {
43                 tmp.next = l1;
44             }
45             return result;
46         }
47             
48     }
49 }
View Code

http://www.jiuzhang.com/solution/merge-two-sorted-lists/

使用dummy做第一个node,然后return dummy.next;

lastnode 类似我的tmp,做中间传递

技术分享
 1 public class Solution {
 2     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 3         ListNode dummy = new ListNode(0);
 4         ListNode lastNode = dummy;
 5         
 6         while (l1 != null && l2 != null) {
 7             if (l1.val < l2.val) {
 8                 lastNode.next = l1;
 9                 l1 = l1.next;
10             } else {
11                 lastNode.next = l2;
12                 l2 = l2.next;
13             }
14             lastNode = lastNode.next;
15         }
16         
17         if (l1 != null) {
18             lastNode.next = l1;
19         } else {
20             lastNode.next = l2;
21         }
22         
23         return dummy.next;
24     }
25 }
View Code

discuss

技术分享
public ListNode mergeTwoLists(ListNode l1, ListNode l2){
        if(l1 == null) return l2;
        if(l2 == null) return l1;
        if(l1.val < l2.val){
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else{
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
}
View Code

 

每日刷题总结