package ygy.test.week3;
import java.util.Stack;
/**
* Created by guoyao on 2017/9/15.
*/publicclassValidParentheses {publicstaticvoidmain(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 參考publicstaticbooleanisValid(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));
elseif(s.charAt(i) == ')' && !stack.empty() && stack.peek() == '(')
stack.pop();
elseif(s.charAt(i) == ']' && !stack.empty() && stack.peek() == '[')
stack.pop();
elseif(s.charAt(i) == '}' && !stack.empty() && stack.peek() == '{')
stack.pop();
elsereturnfalse;
}
return stack.empty();
}
//leetcode discussion 參考publicstaticbooleanisValid_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) returnfalse;
} else p.push(q);
}
return p.isEmpty();
}
}
Remove Element
package ygy.test.week3;
/**
* Created by guoyao on 2017/9/16.
*/publicclassRemoveElement {publicstaticvoidmain(String[] args) {
}
publicstaticintremoveElement(int[] nums, int val) {
if (nums == null || nums.length == 0) {
return0 ;
}
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.
*/publicclassRemoveDuplicatesfromSortedArray {publicstaticvoidmain(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(newint[]{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.
*/publicstaticintremoveDuplicates(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 參考答案publicstaticintremoveDuplicates_2(int[] nums) {
if (nums.length == 0) return0;
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.
*/publicclassMergeTwoSortedLists {publicstaticvoidmain(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;
}
}