diff --git a/Week_01/id_15/LeetCode_20_isValid_15.java b/Week_01/id_15/LeetCode_20_isValid_15.java new file mode 100644 index 00000000..9296ba4e --- /dev/null +++ b/Week_01/id_15/LeetCode_20_isValid_15.java @@ -0,0 +1,29 @@ +package com.haotone.week_01; + +import java.util.Stack; + +class Solution { + public boolean isValid(String s) { + + if(s == null || "".equals(s)) return true; + char[] charArray = s.toCharArray(); + Stack sta = new Stack(); + for(char a:charArray){ + + if(sta.isEmpty()){ + sta.push(a); + continue; + } + char peek = (char)sta.peek(); + if((peek=='(' && a==')') + || (peek=='[' && a==']') + || (peek=='{' && a=='}')) + sta.pop(); + else + sta.push(a); + } + if(sta.isEmpty()) return true; + else return false; + + } +} \ No newline at end of file diff --git a/Week_01/id_15/LeetCode_21_mergeTwoLists_15.java b/Week_01/id_15/LeetCode_21_mergeTwoLists_15.java new file mode 100644 index 00000000..2ee163f8 --- /dev/null +++ b/Week_01/id_15/LeetCode_21_mergeTwoLists_15.java @@ -0,0 +1,40 @@ +package com.haotone.week_01; + +/** + * 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 hummy = new ListNode(-1); + ListNode cur = hummy; + while(l1!=null || l2!=null){ + if(l1 == null){ + cur.next = l2; + cur = l2; + l2 = l2.next; + }else if(l2 == null){ + cur.next = l1; + cur = l1; + l1 = l1.next; + }else{ + if(l1.val>l2.val){ + cur.next = l2; + cur = l2; + l2 = l2.next; + }else{ + cur.next = l1; + cur = l1; + l1 = l1.next; + } + } + + } + return hummy.next; + } +} \ No newline at end of file diff --git a/Week_01/id_15/LeetCode_83_deleteDuplicates_15.java b/Week_01/id_15/LeetCode_83_deleteDuplicates_15.java new file mode 100644 index 00000000..c961eb2f --- /dev/null +++ b/Week_01/id_15/LeetCode_83_deleteDuplicates_15.java @@ -0,0 +1,31 @@ +package com.haotone.week_01; + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode deleteDuplicates(ListNode head) { + if(head == null) return null; + HashSet key = new HashSet(); + key.add(head.val); + ListNode node=head.next; + ListNode pre = head; + while(node!=null){ + if(key.contains(node.val)){ + pre.next = pre.next.next; + } + else{ + key.add(node.val); + pre = node; + } + node = node.next; + } + return head; + + } +} \ No newline at end of file diff --git a/Week_01/id_15/LeetCode_905_sortArrayByParity_15.java b/Week_01/id_15/LeetCode_905_sortArrayByParity_15.java new file mode 100644 index 00000000..10fa304f --- /dev/null +++ b/Week_01/id_15/LeetCode_905_sortArrayByParity_15.java @@ -0,0 +1,27 @@ +package com.haotone.week_01; + +class Solution { + public int[] sortArrayByParity(int[] A) { + int befor = 0; + int after = A.length-1; + while(befor dic = new HashMap(); + if(sarr.length != tarr.length) return false; + for(int i=0;i0) return false; + return true; + + } +} \ No newline at end of file diff --git a/Week_02/id_15/LeetCode_783_minDiffInBST_15.java b/Week_02/id_15/LeetCode_783_minDiffInBST_15.java new file mode 100644 index 00000000..56e89911 --- /dev/null +++ b/Week_02/id_15/LeetCode_783_minDiffInBST_15.java @@ -0,0 +1,54 @@ +package com.haotone.week_01; + +import java.util.HashMap; + +/** + * Definition for a binary tree node. + * */ +class TreeNode { + int val; + TreeNode left; + TreeNode right; + TreeNode(int x) { val = x; } + } + +class Solution { + public int minDiffInBST(TreeNode root) { + if(root == null) return Integer.MAX_VALUE; + if(root.left == null&&root.right == null) return Integer.MAX_VALUE; + if(root.left == null){ + int ri = root.right.val-root.val; + int rimin = minDiffInBST(root.right); + int rile = Integer.MAX_VALUE; + if(root.right.left != null) { + rile = root.right.left.val-root.val; + } + return Math.min(Math.min(ri, rimin),rile); + }else if(root.right == null){ + int le = root.val-root.left.val; + int lemin = minDiffInBST(root.left); + int leri = Integer.MAX_VALUE; + if(root.left.right != null) { + leri = root.val-root.left.right.val; + } + return Math.min(Math.min(le, lemin),leri); + } + int le = root.val-root.left.val; + int ri = root.right.val-root.val; + int cur = Math.min(le, ri); + int rimin = minDiffInBST(root.right); + int lemin = minDiffInBST(root.left); + int chimin = Math.min(rimin, lemin); + int leri = Integer.MAX_VALUE; + int rile = Integer.MAX_VALUE; + if(root.left.right != null) { + leri = root.val-root.left.right.val; + } + if(root.right.left != null) { + rile = root.right.left.val-root.val; + } + int aromin = Math.min(rile, leri); + return Math.min(Math.min(chimin,aromin),cur); + + } +} \ No newline at end of file