From 9a663a4fc64ff42f6786aa9f40f5dca53b4d7ed2 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 21:06:34 +0800 Subject: [PATCH 01/20] =?UTF-8?q?=E5=90=88=E5=B9=B62=E4=B8=AA=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E8=A1=A8=EF=BC=88=E6=99=AE=E9=80=9A=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E6=93=8D=E4=BD=9C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_01/020 | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Week_01/020 diff --git a/Week_01/020 b/Week_01/020 new file mode 100644 index 00000000..44fa9b9c --- /dev/null +++ b/Week_01/020 @@ -0,0 +1,52 @@ +/** + * 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) { + if(l1 == null) { + return l2; + } + if(l2 == null) { + return l1; + } + ListNode head = null; + //用于扫描l1 + ListNode p1 = l1; + //用于扫描l2 + ListNode p2 = l2; + if(l1.val < l2.val) { + head = l1; + p1 = l1.next; + } else { + head = l2; + p2 = l2.next; + } + ListNode p = head;//p指向合并列表的最新节点 + while(p1 !=null && p2 != null) { + //比较插入2个链表中元素的大小 + if(p1.val < p2.val) { + //p1插入p后面 + p.next = p1; + p1 = p1.next; + } else { + //p2插入p后面 + p.next = p2; + p2 = p2.next; + } + p = p.next; + } + + if(p1 != null) { + p.next = p1; + } else if(p2 != null){ + p.next = p2; + } + return head; + + } +} From c20366a6dc1c8710a9e5805764251cd3d42e40d2 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 21:11:45 +0800 Subject: [PATCH 02/20] Rename 020 to 020_week1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 改名 --- Week_01/{020 => 020_week1} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week_01/{020 => 020_week1} (100%) diff --git a/Week_01/020 b/Week_01/020_week1 similarity index 100% rename from Week_01/020 rename to Week_01/020_week1 From de07b5df73e7cf6d0555fe78b39b0a58b6a8881f Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 21:15:11 +0800 Subject: [PATCH 03/20] =?UTF-8?q?Rename=20020=5Fweek1=20to=20=E5=90=88?= =?UTF-8?q?=E5=B9=B62=E4=B8=AA=E6=9C=89=E5=BA=8F=E5=88=97=E8=A1=A8?= =?UTF-8?q?=EF=BC=88=E6=99=AE=E9=80=9A=E9=93=BE=E8=A1=A8=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week_01/020_week1 => "Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\345\210\227\350\241\250\357\274\210\346\231\256\351\200\232\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" (100%) diff --git a/Week_01/020_week1 "b/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\345\210\227\350\241\250\357\274\210\346\231\256\351\200\232\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" similarity index 100% rename from Week_01/020_week1 rename to "Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\345\210\227\350\241\250\357\274\210\346\231\256\351\200\232\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" From 631a78e4b51f52c68fdbf001ab1ddfc0debbbdf9 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 21:16:50 +0800 Subject: [PATCH 04/20] =?UTF-8?q?Create=20=E5=90=88=E5=B9=B62=E4=B8=AA?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E9=93=BE=E8=A1=A8=EF=BC=88=E9=80=92=E5=BD=92?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\222\345\256\236\347\216\260\357\274\211" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" diff --git "a/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" "b/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" new file mode 100644 index 00000000..41f46927 --- /dev/null +++ "b/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" @@ -0,0 +1,29 @@ +/** + * 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) { + if(l1 == null) { + return l2; + } + if(l2 == null) { + return l1; + } + ListNode head = null; + + if(l1.val < l2.val) { + head = l1; + head.next = mergeTwoLists(l1.next,l2); + } else { + head = l2; + head.next = mergeTwoLists(l1,l2.next); + } + return head; + + } +} From 488d227b556d087542fb7c4f93378f76007e4ec5 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 21:25:23 +0800 Subject: [PATCH 05/20] =?UTF-8?q?Update=20=E5=90=88=E5=B9=B62=E4=B8=AA?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E5=88=97=E8=A1=A8=EF=BC=88=E6=99=AE=E9=80=9A?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E6=93=8D=E4=BD=9C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将题目加进去 --- ...23\276\350\241\250\346\223\215\344\275\234\357\274\211" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\345\210\227\350\241\250\357\274\210\346\231\256\351\200\232\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" "b/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\345\210\227\350\241\250\357\274\210\346\231\256\351\200\232\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" index 44fa9b9c..6a520dcb 100644 --- "a/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\345\210\227\350\241\250\357\274\210\346\231\256\351\200\232\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" +++ "b/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\345\210\227\350\241\250\357\274\210\346\231\256\351\200\232\351\223\276\350\241\250\346\223\215\344\275\234\357\274\211" @@ -1,4 +1,11 @@ /** + * 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 + * + * 示例: + * + * 输入:1->2->4, 1->3->4 + * 输出:1->1->2->3->4->4 + * * Definition for singly-linked list. * public class ListNode { * int val; From f58b563eca93d97bf49430c01737c73f80bf4637 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 21:26:11 +0800 Subject: [PATCH 06/20] =?UTF-8?q?Update=20=E5=90=88=E5=B9=B62=E4=B8=AA?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E9=93=BE=E8=A1=A8=EF=BC=88=E9=80=92=E5=BD=92?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将题目加进去 --- ...00\222\345\275\222\345\256\236\347\216\260\357\274\211" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" "b/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" index 41f46927..c3533358 100644 --- "a/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" +++ "b/Week_01/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250\357\274\210\351\200\222\345\275\222\345\256\236\347\216\260\357\274\211" @@ -1,4 +1,11 @@ /** + * 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 + * + * 示例: + * + * 输入:1->2->4, 1->3->4 + * 输出:1->1->2->3->4->4 + * * Definition for singly-linked list. * public class ListNode { * int val; From 25e770ed3b86c8d5805de2d10eb76dd21dae3f04 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 21:53:18 +0800 Subject: [PATCH 07/20] =?UTF-8?q?=E6=8E=92=E5=88=97=E7=A1=AC=E5=B8=81?= =?UTF-8?q?=EF=BC=88=E7=AE=80=E5=8D=95=E5=AE=9E=E7=8E=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\222\345\210\227\347\241\254\345\270\201" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "Week_01/\346\216\222\345\210\227\347\241\254\345\270\201" diff --git "a/Week_01/\346\216\222\345\210\227\347\241\254\345\270\201" "b/Week_01/\346\216\222\345\210\227\347\241\254\345\270\201" new file mode 100644 index 00000000..3e9b5fcc --- /dev/null +++ "b/Week_01/\346\216\222\345\210\227\347\241\254\345\270\201" @@ -0,0 +1,44 @@ +/** + * 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。 + * + * 给定一个数字 n,找出可形成完整阶梯行的总行数。 + * + * n 是一个非负整数,并且在32位有符号整型的范围内。 + * + * 示例 1: + * + * n = 5 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ + * + * 因为第三行不完整,所以返回2. + * 示例 2: + * + * n = 8 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ ¤ + * ¤ ¤ + * + * 因为第四行不完整,所以返回3. +**/ + class Solution { + public int arrangeCoins(int n) { + int sum = 0; + int k = 0; + if(n == 0) + return 0; + if(n == 1) + return 1; + while(sum - n < 0){ + k++; + sum +=k; + } + return k-1; + } +} From 2502ff14651b4304bfa648d0122ec4ed8c4519b2 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sat, 20 Apr 2019 22:48:54 +0800 Subject: [PATCH 08/20] =?UTF-8?q?Create=20=E6=8E=92=E5=88=97=E7=A1=AC?= =?UTF-8?q?=E5=B8=81=EF=BC=88=E6=95=B0=E5=AD=A6=E5=85=AC=E5=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\246\345\205\254\345\274\217\357\274\211" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\346\225\260\345\255\246\345\205\254\345\274\217\357\274\211" diff --git "a/\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\346\225\260\345\255\246\345\205\254\345\274\217\357\274\211" "b/\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\346\225\260\345\255\246\345\205\254\345\274\217\357\274\211" new file mode 100644 index 00000000..d1ab98a9 --- /dev/null +++ "b/\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\346\225\260\345\255\246\345\205\254\345\274\217\357\274\211" @@ -0,0 +1,36 @@ + +/** + * 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。 + * + * 给定一个数字 n,找出可形成完整阶梯行的总行数。 + * + * n 是一个非负整数,并且在32位有符号整型的范围内。 + * + * 示例 1: + * + * n = 5 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ + * + * 因为第三行不完整,所以返回2. + * 示例 2: + * + * n = 8 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ ¤ + * ¤ ¤ + * + * 因为第四行不完整,所以返回3. +**/ +class Solution { + public int arrangeCoins(int n) { + double k = -0.5d + Math.sqrt(0.25d+2*(double)n); + return (int)Math.floor(k); + } +} From 4b024379988875b6288bf3abfb26e910ae866cad Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sun, 21 Apr 2019 08:20:53 +0800 Subject: [PATCH 09/20] =?UTF-8?q?Create=20=E6=8E=92=E5=88=97=E7=A1=AC?= =?UTF-8?q?=E5=B8=81=EF=BC=88=E9=87=87=E7=94=A8=E4=BA=8C=E5=88=86=E6=B8=90?= =?UTF-8?q?=E8=BF=9B=E6=96=B9=E5=BC=8F=E8=A7=A3=E5=86=B3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这种渐进取值的题,都可以使用二分查找的方式解答 --- ...4\217\350\247\243\345\206\263\357\274\211" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 "Week_01/\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\351\207\207\347\224\250\344\272\214\345\210\206\346\270\220\350\277\233\346\226\271\345\274\217\350\247\243\345\206\263\357\274\211" diff --git "a/Week_01/\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\351\207\207\347\224\250\344\272\214\345\210\206\346\270\220\350\277\233\346\226\271\345\274\217\350\247\243\345\206\263\357\274\211" "b/Week_01/\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\351\207\207\347\224\250\344\272\214\345\210\206\346\270\220\350\277\233\346\226\271\345\274\217\350\247\243\345\206\263\357\274\211" new file mode 100644 index 00000000..bde2108c --- /dev/null +++ "b/Week_01/\346\216\222\345\210\227\347\241\254\345\270\201\357\274\210\351\207\207\347\224\250\344\272\214\345\210\206\346\270\220\350\277\233\346\226\271\345\274\217\350\247\243\345\206\263\357\274\211" @@ -0,0 +1,19 @@ +class Solution { + public int arrangeCoins(int n) { + if(n == 1){ + return 1; + } + + int start = 0, end = n; + while(start + 1 < end){ + int mid = (end + start)>>1; + if((long)mid*(mid+1) <= (long)2*n){ + start = mid; + } + else{ + end = mid; + } + } + return start; + } +} From e06d84dc6976d2a7917644211c046fa6a9a96822 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Mon, 22 Apr 2019 17:29:24 +0800 Subject: [PATCH 10/20] Create LeetCode_21 --- Week_01/id_20/LeetCode_21 | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Week_01/id_20/LeetCode_21 diff --git a/Week_01/id_20/LeetCode_21 b/Week_01/id_20/LeetCode_21 new file mode 100644 index 00000000..6a520dcb --- /dev/null +++ b/Week_01/id_20/LeetCode_21 @@ -0,0 +1,59 @@ +/** + * 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 + * + * 示例: + * + * 输入: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) { + if(l1 == null) { + return l2; + } + if(l2 == null) { + return l1; + } + ListNode head = null; + //用于扫描l1 + ListNode p1 = l1; + //用于扫描l2 + ListNode p2 = l2; + if(l1.val < l2.val) { + head = l1; + p1 = l1.next; + } else { + head = l2; + p2 = l2.next; + } + ListNode p = head;//p指向合并列表的最新节点 + while(p1 !=null && p2 != null) { + //比较插入2个链表中元素的大小 + if(p1.val < p2.val) { + //p1插入p后面 + p.next = p1; + p1 = p1.next; + } else { + //p2插入p后面 + p.next = p2; + p2 = p2.next; + } + p = p.next; + } + + if(p1 != null) { + p.next = p1; + } else if(p2 != null){ + p.next = p2; + } + return head; + + } +} From e58a2ab90a545a073e0086ced69d94653c459b7a Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Mon, 22 Apr 2019 17:32:41 +0800 Subject: [PATCH 11/20] =?UTF-8?q?Create=20LeetCode=5F21=5F=E9=80=92?= =?UTF-8?q?=E5=BD=92=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\222\345\275\222\345\256\236\347\216\260" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "Week_01/id_20/LeetCode_21_\351\200\222\345\275\222\345\256\236\347\216\260" diff --git "a/Week_01/id_20/LeetCode_21_\351\200\222\345\275\222\345\256\236\347\216\260" "b/Week_01/id_20/LeetCode_21_\351\200\222\345\275\222\345\256\236\347\216\260" new file mode 100644 index 00000000..c3533358 --- /dev/null +++ "b/Week_01/id_20/LeetCode_21_\351\200\222\345\275\222\345\256\236\347\216\260" @@ -0,0 +1,36 @@ +/** + * 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 + * + * 示例: + * + * 输入: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) { + if(l1 == null) { + return l2; + } + if(l2 == null) { + return l1; + } + ListNode head = null; + + if(l1.val < l2.val) { + head = l1; + head.next = mergeTwoLists(l1.next,l2); + } else { + head = l2; + head.next = mergeTwoLists(l1,l2.next); + } + return head; + + } +} From 9624be66264c7d2376dcf3c72361bc3f64993707 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Mon, 22 Apr 2019 17:33:06 +0800 Subject: [PATCH 12/20] =?UTF-8?q?Rename=20LeetCode=5F21=20to=20LeetCode=5F?= =?UTF-8?q?21=5F=E9=93=BE=E8=A1=A8=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LeetCode_21_\351\223\276\350\241\250\345\256\236\347\216\260" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week_01/id_20/LeetCode_21 => "Week_01/id_20/LeetCode_21_\351\223\276\350\241\250\345\256\236\347\216\260" (100%) diff --git a/Week_01/id_20/LeetCode_21 "b/Week_01/id_20/LeetCode_21_\351\223\276\350\241\250\345\256\236\347\216\260" similarity index 100% rename from Week_01/id_20/LeetCode_21 rename to "Week_01/id_20/LeetCode_21_\351\223\276\350\241\250\345\256\236\347\216\260" From 660b6eac7824d13d24e755ef52966aa42e8efdb4 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Mon, 22 Apr 2019 17:35:57 +0800 Subject: [PATCH 13/20] =?UTF-8?q?Create=20LeetCode=5F441=5F=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\200\345\215\225\345\256\236\347\216\260" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "Week_01/id_20/LeetCode_441_\347\256\200\345\215\225\345\256\236\347\216\260" diff --git "a/Week_01/id_20/LeetCode_441_\347\256\200\345\215\225\345\256\236\347\216\260" "b/Week_01/id_20/LeetCode_441_\347\256\200\345\215\225\345\256\236\347\216\260" new file mode 100644 index 00000000..3e9b5fcc --- /dev/null +++ "b/Week_01/id_20/LeetCode_441_\347\256\200\345\215\225\345\256\236\347\216\260" @@ -0,0 +1,44 @@ +/** + * 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。 + * + * 给定一个数字 n,找出可形成完整阶梯行的总行数。 + * + * n 是一个非负整数,并且在32位有符号整型的范围内。 + * + * 示例 1: + * + * n = 5 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ + * + * 因为第三行不完整,所以返回2. + * 示例 2: + * + * n = 8 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ ¤ + * ¤ ¤ + * + * 因为第四行不完整,所以返回3. +**/ + class Solution { + public int arrangeCoins(int n) { + int sum = 0; + int k = 0; + if(n == 0) + return 0; + if(n == 1) + return 1; + while(sum - n < 0){ + k++; + sum +=k; + } + return k-1; + } +} From 2f2caaa24f7f05ae28593b73991fb524e7a3e78b Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Mon, 22 Apr 2019 17:36:53 +0800 Subject: [PATCH 14/20] =?UTF-8?q?Create=20LeetCode=5F441=5F=E6=95=B0?= =?UTF-8?q?=E5=AD=A6=E5=85=AC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\260\345\255\246\345\205\254\345\274\217" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "Week_01/id_20/LeetCode_441_\346\225\260\345\255\246\345\205\254\345\274\217" diff --git "a/Week_01/id_20/LeetCode_441_\346\225\260\345\255\246\345\205\254\345\274\217" "b/Week_01/id_20/LeetCode_441_\346\225\260\345\255\246\345\205\254\345\274\217" new file mode 100644 index 00000000..73a7906d --- /dev/null +++ "b/Week_01/id_20/LeetCode_441_\346\225\260\345\255\246\345\205\254\345\274\217" @@ -0,0 +1,35 @@ +/** + * 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。 + * + * 给定一个数字 n,找出可形成完整阶梯行的总行数。 + * + * n 是一个非负整数,并且在32位有符号整型的范围内。 + * + * 示例 1: + * + * n = 5 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ + * + * 因为第三行不完整,所以返回2. + * 示例 2: + * + * n = 8 + * + * 硬币可排列成以下几行: + * ¤ + * ¤ ¤ + * ¤ ¤ ¤ + * ¤ ¤ + * + * 因为第四行不完整,所以返回3. +**/ +class Solution { + public int arrangeCoins(int n) { + double k = -0.5d + Math.sqrt(0.25d+2*(double)n); + return (int)Math.floor(k); + } +} From eecde5b7a77ba5da2260e17ec93660c17e38295c Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Mon, 22 Apr 2019 17:38:08 +0800 Subject: [PATCH 15/20] =?UTF-8?q?Create=20LeetCode=5F441=5F=E4=BA=8C?= =?UTF-8?q?=E5=88=86=E6=B8=90=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\214\345\210\206\346\270\220\350\277\233" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 "Week_01/id_20/LeetCode_441_\344\272\214\345\210\206\346\270\220\350\277\233" diff --git "a/Week_01/id_20/LeetCode_441_\344\272\214\345\210\206\346\270\220\350\277\233" "b/Week_01/id_20/LeetCode_441_\344\272\214\345\210\206\346\270\220\350\277\233" new file mode 100644 index 00000000..bde2108c --- /dev/null +++ "b/Week_01/id_20/LeetCode_441_\344\272\214\345\210\206\346\270\220\350\277\233" @@ -0,0 +1,19 @@ +class Solution { + public int arrangeCoins(int n) { + if(n == 1){ + return 1; + } + + int start = 0, end = n; + while(start + 1 < end){ + int mid = (end + start)>>1; + if((long)mid*(mid+1) <= (long)2*n){ + start = mid; + } + else{ + end = mid; + } + } + return start; + } +} From a1e570e7ca5146a7c7da37051dc8c0376e88a53a Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sun, 19 May 2019 17:15:32 +0800 Subject: [PATCH 16/20] Create LeetCode_24_020.java --- Week_01/id_20/LeetCode_24_020.java | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Week_01/id_20/LeetCode_24_020.java diff --git a/Week_01/id_20/LeetCode_24_020.java b/Week_01/id_20/LeetCode_24_020.java new file mode 100644 index 00000000..a58a93b0 --- /dev/null +++ b/Week_01/id_20/LeetCode_24_020.java @@ -0,0 +1,35 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode swapPairs(ListNode head) { + ListNode dummy = new ListNode(0); + dummy.next = head; + ListNode p = dummy; + ListNode h = head; + + while (h != null) { + if (h != null && h.next != null) { + ListNode tmp = h.next; + //p指向两个节点中的第二个节点 + p.next = tmp; + //交换位置 + h.next = h.next.next; + tmp.next = h; + + //继续遍历 + p = p.next.next; + h = h.next; + } else { + p.next = h; + h = h.next; + } + } + return dummy.next; + } +} From 5441a08bfee72e4f4b8d7c55cac6cf161f714772 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sun, 19 May 2019 17:16:25 +0800 Subject: [PATCH 17/20] Create LeetCode_81_020.java --- Week_01/id_20/LeetCode_81_020.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Week_01/id_20/LeetCode_81_020.java diff --git a/Week_01/id_20/LeetCode_81_020.java b/Week_01/id_20/LeetCode_81_020.java new file mode 100644 index 00000000..fb5a5f0c --- /dev/null +++ b/Week_01/id_20/LeetCode_81_020.java @@ -0,0 +1,29 @@ +class Solution { + public boolean search(int[] nums, int target) { + int low = 0, high = nums.length - 1; + while (low <= high) { + int mid = low + (high - low) / 2; + if (nums[mid] == target) { + return true; + } + if (nums[mid] > nums[low]) { + if (target >= nums[low] && target <= nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } else if (nums[mid] < nums[high]) { + if (target >= nums[mid] && target <= nums[high]) { + low = mid + 1; + } else { + high = mid - 1; + } + } else if (nums[low] == nums[mid]) { + low++; + } else { + high--; + } + } + return false; + } +} From ccc3722660231557551525653a7c0244712b6f02 Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sun, 19 May 2019 17:17:41 +0800 Subject: [PATCH 18/20] =?UTF-8?q?=E5=86=99=E7=82=B9=E5=BF=83=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这道题,需要配合画图来理解 --- Week_01/id_20/LeetCode_24_020.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week_01/id_20/LeetCode_24_020.java b/Week_01/id_20/LeetCode_24_020.java index a58a93b0..208ba3bc 100644 --- a/Week_01/id_20/LeetCode_24_020.java +++ b/Week_01/id_20/LeetCode_24_020.java @@ -2,7 +2,7 @@ * Definition for singly-linked list. * public class ListNode { * int val; - * ListNode next; + * ListNode next; * ListNode(int x) { val = x; } * } */ From fe1a602938a1829bd83b968996d41b64308bb49a Mon Sep 17 00:00:00 2001 From: lcfgrn Date: Sun, 19 May 2019 22:01:14 +0800 Subject: [PATCH 19/20] Create LeetCode_692_020.java --- Week_02/id_20/LeetCode_692_020.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Week_02/id_20/LeetCode_692_020.java diff --git a/Week_02/id_20/LeetCode_692_020.java b/Week_02/id_20/LeetCode_692_020.java new file mode 100644 index 00000000..7bcc83d0 --- /dev/null +++ b/Week_02/id_20/LeetCode_692_020.java @@ -0,0 +1,27 @@ +class Solution { + public List topKFrequent(String[] words, int k) { + + Map map = new HashMap<>(); + for (String word : words) { + map.put(word, map.getOrDefault(word, 0) + 1); + } + + PriorityQueue> priorityQueue = new PriorityQueue<>( + new Comparator>() { + @Override + public int compare(Map.Entry o1, Map.Entry o2) { + if (o1.getValue().equals(o2.getValue())) { + return o1.getKey().compareTo(o2.getKey()); + } + return o2.getValue() - o1.getValue(); + } + }); + + priorityQueue.addAll(map.entrySet()); + List ret = new ArrayList<>(k); + for(int i=0; i Date: Sun, 19 May 2019 22:02:00 +0800 Subject: [PATCH 20/20] Create LeetCode_671_020.java --- Week_02/id_20/LeetCode_671_020.java | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Week_02/id_20/LeetCode_671_020.java diff --git a/Week_02/id_20/LeetCode_671_020.java b/Week_02/id_20/LeetCode_671_020.java new file mode 100644 index 00000000..78cac849 --- /dev/null +++ b/Week_02/id_20/LeetCode_671_020.java @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public int findSecondMinimumValue(TreeNode root) { + Set set = new HashSet<>(); + inOrder(root, set); + List list = new ArrayList<>(); + list.addAll(set); + if (list.size() == 1) { + return -1; + } else { + Collections.sort(list); + return list.get(1); + } + } + + private void inOrder(TreeNode root, Set set) { + if (root != null) { + inOrder(root.left, set); + set.add(root.val); + inOrder(root.right,set); + } + } +}