leetcode-month1-week3

来源:互联网 发布:视频相片制作软件 编辑:程序博客网 时间:2024/05/24 00:19

Valid Parentheses

package ygy.test.week3;import java.util.Stack;/** * Created by guoyao on 2017/9/15. */public class ValidParentheses {    public static void main(String[] agrs) {        System.out.println(isValid("()()(){}[]"));        System.out.println(isValid_2("()()(){}[]"));    }    /**     * 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.     */    //leetcode discussion 参考    public static boolean isValid(String s) {        Stack<Character> stack = new Stack<>();        for(int i = 0; i<s.length(); i++) {            if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{')                stack.push(s.charAt(i));            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 stack.empty();    }    //leetcode discussion 参考    public static boolean isValid_2(String s) {        Stack<Integer> p = new Stack<>();        for(int i = 0; i < s.length(); i++) {            int q = "(){}[]".indexOf(s.substring(i, i + 1));            if(q % 2 == 1) {                if(p.isEmpty() || p.pop() != q - 1) return false;            } else p.push(q);        }        return p.isEmpty();    }}

Remove Element

package ygy.test.week3;/** * Created by guoyao on 2017/9/16. */public class RemoveElement {    public static void main(String[] args) {    }    public static int removeElement(int[] nums, int val) {        if (nums == null || nums.length == 0) {            return  0 ;        }        int index = 0 ;        for ( int i = 0 ; i< nums.length ; i ++) {            if (nums[i] != val) {                nums[index]=nums[i];                index ++ ;            }        }        return index ;    }}

Remove Duplicates from Sorted Array

package ygy.test.week3;/** * Created by guoyao on 2017/9/15. */public class RemoveDuplicatesfromSortedArray {    public static void main(String[] args) {        System.out.println(removeDuplicates(new Integer[]{1,1,2,2,2,2,2,2,3,4,5,5,6}));        System.out.println(removeDuplicates_2(new int[]{1,1,2,2,2,2,2,2,3,4,5,5,6}));    }    /**     Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.     Do not allocate extra space for another array, you must do this in place with constant memory.     For example,     Given input array nums = [1,1,2],     Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.     It doesn't matter what you leave beyond the new length.     */    public static int removeDuplicates(Integer[] nums) {        int lastValue = 0   ;        int count = 0 ;        for(int i = 0 ; i < nums.length ; i ++) {            if (i == 0) {                lastValue = nums[i] ;                count ++ ;                continue;            }            if (nums[i]== lastValue) {              continue;            }            lastValue = nums[i];            count ++;        }        return  count;    }    //leet code 参考答案    public static int removeDuplicates_2(int[] nums) {        if (nums.length == 0) return 0;        int i = 0;        for (int j = 1; j < nums.length; j++) {            if (nums[j] != nums[i]) {                i++;                nums[i] = nums[j];            }        }        return i + 1;    }}

Merge Two Sorted Lists

package ygy.test.week3;/** * Created by guoyao on 2017/9/15. */public class MergeTwoSortedLists {    public static void main(String[] args) {    }    /**     * 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.     */    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        ListNode tempHeadNode=new ListNode(0);        ListNode current = tempHeadNode ;        while (l1 != null) {            int temp =  l1.val ;            while (l2 != null && temp > l2.val ) {                current.next =new ListNode(l2.val);                current = current.next ;                l2=l2.next;            }            current.next=new ListNode(temp);            current = current.next ;            l1 = l1.next ;        }        current.next = l2 ;        return  tempHeadNode.next ;    }    //leet code 参考答案    public ListNode mergeTwoLists_2(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;        }    }}class ListNode {    int val;    ListNode next;    ListNode(int x) {        val=x;    }}
原创粉丝点击