From f6ca3077c5b7aab53acfac056c475ca873e43d36 Mon Sep 17 00:00:00 2001 From: haotone Date: Sat, 20 Apr 2019 20:26:33 +0800 Subject: [PATCH 1/9] Create Solution_sortArrayByParityII.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一周代码专业 --- .../id_15/Solution_sortArrayByParityII.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Week_01/id_15/Solution_sortArrayByParityII.java diff --git a/Week_01/id_15/Solution_sortArrayByParityII.java b/Week_01/id_15/Solution_sortArrayByParityII.java new file mode 100644 index 00000000..692d8f91 --- /dev/null +++ b/Week_01/id_15/Solution_sortArrayByParityII.java @@ -0,0 +1,27 @@ +package com.haotone.week_01; + +class Solution { + public int[] sortArrayByParityII(int[] A) { + + int ou = 0; + int ji = 1; + int n = A.length; + while(ji<=n-1 && ou<=n-2){ + + + if(A[ji]%2!=0){ + ji += 2; + }else if(A[ou]%2==0){ + ou += 2; + }else if(A[ji]%2==0 && A[ou]%2!=0){ + int temp = A[ji]; + A[ji] = A[ou]; + A[ou] = temp; + ji += 2; + ou += 2; + } + } + return A; + + } +} \ No newline at end of file From 787cf440b143ee1d0b1690806c53455e19d8a2ad Mon Sep 17 00:00:00 2001 From: haotone Date: Sat, 20 Apr 2019 20:26:40 +0800 Subject: [PATCH 2/9] Create Solution_sortArrayByParity.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一周代码专业 --- Week_01/id_15/Solution_sortArrayByParity.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Week_01/id_15/Solution_sortArrayByParity.java diff --git a/Week_01/id_15/Solution_sortArrayByParity.java b/Week_01/id_15/Solution_sortArrayByParity.java new file mode 100644 index 00000000..10fa304f --- /dev/null +++ b/Week_01/id_15/Solution_sortArrayByParity.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 Date: Sat, 20 Apr 2019 20:26:44 +0800 Subject: [PATCH 3/9] Create Solution_mergeTwoLists.java --- Week_01/id_15/Solution_mergeTwoLists.java | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Week_01/id_15/Solution_mergeTwoLists.java diff --git a/Week_01/id_15/Solution_mergeTwoLists.java b/Week_01/id_15/Solution_mergeTwoLists.java new file mode 100644 index 00000000..2ee163f8 --- /dev/null +++ b/Week_01/id_15/Solution_mergeTwoLists.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 From 70afa6629122f4a700221debde2a78636dffc789 Mon Sep 17 00:00:00 2001 From: haotone Date: Sat, 20 Apr 2019 20:26:48 +0800 Subject: [PATCH 4/9] Create Solution_isValid.java --- Week_01/id_15/Solution_isValid.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Week_01/id_15/Solution_isValid.java diff --git a/Week_01/id_15/Solution_isValid.java b/Week_01/id_15/Solution_isValid.java new file mode 100644 index 00000000..9296ba4e --- /dev/null +++ b/Week_01/id_15/Solution_isValid.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 From 82adaae26a9f6448722df0c911265c4528c19aba Mon Sep 17 00:00:00 2001 From: haotone Date: Sat, 20 Apr 2019 20:26:51 +0800 Subject: [PATCH 5/9] Create Solution_deleteDuplicates.java --- Week_01/id_15/Solution_deleteDuplicates.java | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Week_01/id_15/Solution_deleteDuplicates.java diff --git a/Week_01/id_15/Solution_deleteDuplicates.java b/Week_01/id_15/Solution_deleteDuplicates.java new file mode 100644 index 00000000..c961eb2f --- /dev/null +++ b/Week_01/id_15/Solution_deleteDuplicates.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 From add976a6a8479e7d4b3edda6960b3bd94bb12328 Mon Sep 17 00:00:00 2001 From: haotone Date: Sat, 20 Apr 2019 20:51:58 +0800 Subject: [PATCH 6/9] Delete Solution_deleteDuplicates.java --- Week_01/id_15/Solution_deleteDuplicates.java | 31 -------------------- 1 file changed, 31 deletions(-) delete mode 100644 Week_01/id_15/Solution_deleteDuplicates.java diff --git a/Week_01/id_15/Solution_deleteDuplicates.java b/Week_01/id_15/Solution_deleteDuplicates.java deleted file mode 100644 index c961eb2f..00000000 --- a/Week_01/id_15/Solution_deleteDuplicates.java +++ /dev/null @@ -1,31 +0,0 @@ -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 From 8f7187a2dbc820dcf487672009d5867d7c6424e2 Mon Sep 17 00:00:00 2001 From: haotone Date: Sat, 20 Apr 2019 20:52:14 +0800 Subject: [PATCH 7/9] Create LeetCode_83_deleteDuplicates_15.java --- .../LeetCode_83_deleteDuplicates_15.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Week_01/id_15/LeetCode_83_deleteDuplicates_15.java 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 From b8e812fa98dd37cae8c39e0ba341ac0a37081fd7 Mon Sep 17 00:00:00 2001 From: haotone Date: Sat, 20 Apr 2019 20:56:02 +0800 Subject: [PATCH 8/9] week01 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一周代码作业 --- .../id_15/{Solution_isValid.java => LeetCode_20_isValid_15.java} | 0 ...ution_mergeTwoLists.java => LeetCode_21_mergeTwoLists_15.java} | 0 ...tArrayByParity.java => LeetCode_905_sortArrayByParity_15.java} | 0 ...ayByParityII.java => LeetCode_922_sortArrayByParityII_15.java} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename Week_01/id_15/{Solution_isValid.java => LeetCode_20_isValid_15.java} (100%) rename Week_01/id_15/{Solution_mergeTwoLists.java => LeetCode_21_mergeTwoLists_15.java} (100%) rename Week_01/id_15/{Solution_sortArrayByParity.java => LeetCode_905_sortArrayByParity_15.java} (100%) rename Week_01/id_15/{Solution_sortArrayByParityII.java => LeetCode_922_sortArrayByParityII_15.java} (100%) diff --git a/Week_01/id_15/Solution_isValid.java b/Week_01/id_15/LeetCode_20_isValid_15.java similarity index 100% rename from Week_01/id_15/Solution_isValid.java rename to Week_01/id_15/LeetCode_20_isValid_15.java diff --git a/Week_01/id_15/Solution_mergeTwoLists.java b/Week_01/id_15/LeetCode_21_mergeTwoLists_15.java similarity index 100% rename from Week_01/id_15/Solution_mergeTwoLists.java rename to Week_01/id_15/LeetCode_21_mergeTwoLists_15.java diff --git a/Week_01/id_15/Solution_sortArrayByParity.java b/Week_01/id_15/LeetCode_905_sortArrayByParity_15.java similarity index 100% rename from Week_01/id_15/Solution_sortArrayByParity.java rename to Week_01/id_15/LeetCode_905_sortArrayByParity_15.java diff --git a/Week_01/id_15/Solution_sortArrayByParityII.java b/Week_01/id_15/LeetCode_922_sortArrayByParityII_15.java similarity index 100% rename from Week_01/id_15/Solution_sortArrayByParityII.java rename to Week_01/id_15/LeetCode_922_sortArrayByParityII_15.java From af7553cab9a12e5936d2a01f670fb96a36dcbc92 Mon Sep 17 00:00:00 2001 From: haotone Date: Sun, 28 Apr 2019 21:19:17 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第二周作业 --- Week_02/id_15/LeetCode_242_isAnagram_15.java | 28 ++++++++++ .../id_15/LeetCode_783_minDiffInBST_15.java | 54 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 Week_02/id_15/LeetCode_242_isAnagram_15.java create mode 100644 Week_02/id_15/LeetCode_783_minDiffInBST_15.java diff --git a/Week_02/id_15/LeetCode_242_isAnagram_15.java b/Week_02/id_15/LeetCode_242_isAnagram_15.java new file mode 100644 index 00000000..20fc4658 --- /dev/null +++ b/Week_02/id_15/LeetCode_242_isAnagram_15.java @@ -0,0 +1,28 @@ +package com.haotone.week_01; + +import java.util.HashMap; + +class Solution { + public boolean isAnagram(String s, String t) { + char[] sarr = s.toCharArray(); + char[] tarr = t.toCharArray(); + HashMap 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