LeetCode初尝试-简单-2

xiaoxiao2021-02-28  17

LeetCode题库:https://leetcode-cn.com/problemset/all/

20有效的括号

给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()[]{}" 输出: true

示例 3:

输入: "(]" 输出: false class Solution { public boolean isValid(String s) { if(s==null || s.length()==0) return true; if(s.length()%2 == 1) return false; Stack<Character> st = new Stack<Character>(); char[] c = s.toCharArray(); for(int i=0; i<c.length; i++){ switch(c[i]){ case '(': case '[': case '{': st.push(c[i]); break; case ')': if(st.empty()) return false; if(st.peek()=='(') st.pop(); else if(st.peek()=='[' || st.peek()=='{') st.push(c[i]); else return false; break; case ']': if(st.empty()) return false; if(st.peek()=='[') st.pop(); else if(st.peek()=='(' || st.peek()=='{') st.push(c[i]); else return false; break; case '}': if(st.empty()) return false; if(st.peek()=='{') st.pop(); else if(st.peek()=='[' || st.peek()=='(') st.push(c[i]); else return false; break; default: return false; } } return st.empty(); } }

21合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例:

输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4

方法一:递归

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { //递归 ListNode result=null; if(l1==null || l2==null){ if(l1==null && l2==null) return null; else return result=(l1==null ? l2 : l1); } if(l1.val<l2.val){ result=l1; result.next=mergeTwoLists(l1.next, l2); }else{ result=l2; result.next=mergeTwoLists(l1, l2.next); } return result; } }

方法二:循环

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { //循环 ListNode start=new ListNode(0); ListNode temp=new ListNode(0); start.next=temp; while(l1!=null && l2!=null){ if(l1.val<l2.val){ temp.next=l1; l1=l1.next; }else{ temp.next=l2; l2=l2.next; } temp=temp.next; } if(l1!=null) temp.next=l1; if(l2!=null) temp.next=l2; return start.next.next; } }

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

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

示例 :

给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1,2。 你不需要考虑数组中超出新长度后面的元素。 class Solution { public int removeDuplicates(int[] nums) { if(nums==null || nums.length==0) return 0; int count=0; for(int i=1; i<nums.length; i++){ if(nums[i]!=nums[count]){ count++; nums[count]=nums[i]; } } return count+1; } }

27移除元素

给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

示例 :

给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。 你不需要考虑数组中超出新长度后面的元素。 class Solution { public int removeElement(int[] nums, int val) { if(nums==null || nums.length==0) return 0; int count=0; for(int i=0; i<nums.length; i++){ if(nums[i]!=val){ nums[count++]=nums[i]; } } return count; } }

28实现strStr()

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1

示例 1:

输入: haystack = "hello", needle = "ll" 输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba" 输出: -1 class Solution { public int strStr(String haystack, String needle) { int m = haystack.length(); int n = needle.length(); if(haystack==null || needle==null || n==0) return 0; if(m<n) return -1; for(int i=0; i<=m-n; i++){ boolean flag=true; for(int j=0; j<n; j++){ if(haystack.charAt(i+j)!=needle.charAt(j)){ flag=false; break; } } if(flag) return i; } return -1; } }
转载请注明原文地址: https://www.6miu.com/read-2000013.html

最新回复(0)