leetcode-month1-week3

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值