# LeetCode刷题整理（9.24）

## 20. 有效的括号

``````class Solution {
public boolean isValid(String s) {
ArrayList<Character> res = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (res.size() == 0) {
continue;
}
if (res.get(res.size() - 1).equals('(') && s.charAt(i) == ')') {
res.remove(res.size() - 1);
continue;
}
if (res.get(res.size() - 1).equals('{') && s.charAt(i) == '}') {
res.remove(res.size() - 1);
continue;
}
if (res.get(res.size() - 1).equals('[') && s.charAt(i) == ']') {
res.remove(res.size() - 1);
continue;
}
}
return res.size() <= 0;
}
}

## 21. 合并两个有序链表

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null && l2==null) return l1;
if(l1==null || l2==null) return l1==null? l2:l1;
ArrayList<Integer> res = new ArrayList<>();
ListNode l11 = l1;
ListNode l22 = l2;

while(l11!=null || l22!=null){
if(l11==null){
l22 = l22.next;
continue;
}
if(l22==null ){
l11 = l11.next;
continue;
}
if(l11!=null && l22!=null){

if(l11.val<=l22.val){
l11 = l11.next;
continue;
}
if(l11.val>l22.val){
l22 = l22.next;
continue;
}
}
}
ListNode number = l1;
while(number.next!=null){
number = number.next;
}
number.next = l2;
int i =0;
System.err.println(res.size());
while(l1!=null){
l1.val = res.get(i);
i++;
l1 = l1.next;
}

}
}

## 26. 删除排序数组中的重复项

``````class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length==0) return 0;
if(nums.length==1) return 1;
int l = 0 , r = 1;
while (r < nums.length) {
if(nums[r]!=nums[l]){
nums[l+1] = nums[r];
r++;
l++;
continue;
}
if(nums[r]==nums[l]){
r++;
}
}
return l+1;
}
}

## 27. 移除元素

``````class Solution {
public int removeElement(int[] nums, int val) {
int length = nums.length;
for (int num : nums) {
if (num == val) length--;
}
for (int i = 0; i < length; i++) {
if (nums[i] == val) {
for (int j = i; j < nums.length; j++) {
if (nums[j] != nums[i]) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
break;
}

}
}
}
return length;
}
}