diff --git a/5.leetcode/src/com/fanxb/common/Q213.java b/5.leetcode/src/com/fanxb/common/Q213.java new file mode 100644 index 0000000..2fc71cf --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q213.java @@ -0,0 +1,30 @@ +package com.fanxb.common; + +import java.util.Arrays; + +/** + * 旋转链表 + * 题目地址:https://leetcode-cn.com/problems/assign-cookies/submissions/ + * 解题思路: + * 贪心算法,想不明白 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q213 { + + public int rob(int[] nums) { + int last = 0, cur = 0; + for (int num : nums) { + int temp = cur; + cur = Math.max(cur, last + num); + last = temp; + } + return cur; + } + + public static void main(String[] args) { + int[] s = {1, 2, 3, 1}; + System.out.println(new Q213().rob(s)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q27.java b/5.leetcode/src/com/fanxb/common/Q27.java new file mode 100644 index 0000000..8f1c634 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q27.java @@ -0,0 +1,30 @@ +package com.fanxb.common; + +import java.util.Arrays; + +/** + * Created with IntelliJ IDEA + * 移除元素 + * 地址:https://leetcode-cn.com/problems/remove-element/ + * 思路: + * 暴力办法,两层for循环,每找到一个相同元素就将后面的所有元素向前移动一位 + * 还有一种简便点的办法,不需要删除重复元素,只需要将重复元素替换为不重复元素即可 + * + * @author fanxb + * Date: 2020/6/11 9:56 + */ +public class Q27 { + public int removeElement(int[] nums, int val) { + int count = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] != val) { + nums[count++] = nums[i]; + } + } + return count; + } + + public static void main(String[] args) { + System.out.println(new Q27().removeElement(new int[]{0, 1, 2, 2, 3, 0, 4, 2}, 2)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q28.java b/5.leetcode/src/com/fanxb/common/Q28.java new file mode 100644 index 0000000..f7475b2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q28.java @@ -0,0 +1,79 @@ +package com.fanxb.common; + +import java.text.SimpleDateFormat; + +/** + * Created with IntelliJ IDEA + * 移除元素 + * 地址:https://leetcode-cn.com/problems/remove-element/ + * 思路: + * 暴力办法,两层for循环,每找到一个相同元素就将后面的所有元素向前移动一位 + * 还有一种简便点的办法,不需要删除重复元素,只需要将重复元素替换为不重复元素即可 + * + * @author fanxb + * Date: 2020/6/11 9:56 + */ +public class Q28 { + public int strStr(String haystack, String needle) { + int num1 = haystack.length(), num2 = needle.length(); + if (num2 == 0) { + return 0; + } + for (int i = 0; i < num1; i++) { + int count = 0; + for (count = 0; count < num2 && i + count < num1; count++) { + if (haystack.charAt(i + count) != needle.charAt(count)) { + break; + } + } + if (count == num2) { + return i; + } + } + return -1; + } + + public int kmp(String haystack, String needle) { + if (needle.length() == 0) { + return 0; + } + //计算needle的PMT(部分匹配表) + int[] pmt = new int[needle.length()]; + //从第二个字符开始计算 + for (int i = 1, j = 0; i < needle.length(); ) { + if (needle.charAt(i) == needle.charAt(j)) { + pmt[i] = j + 1; + j++; + i++; + } else { + if (j == 0) { + i++; + } else { + j = pmt[j - 1]; + } + } + } + + for (int i = 0, j = 0; i < haystack.length(); ) { + if (haystack.charAt(i) != needle.charAt(j)) { + if (j == 0) { + i++; + } else { + j = pmt[j - 1]; + } + } else { + j++; + i++; + } + if (j == needle.length()) { + return i - needle.length(); + } + } + return -1; + } + + public static void main(String[] args) { + System.out.println(new Q28().kmp("aabaaabaaac", "aabaaac")); +// System.out.println(new Q28().kmp("hello", "ll")); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q46.java b/5.leetcode/src/com/fanxb/common/Q46.java index 16fc1a7..c2279be 100644 --- a/5.leetcode/src/com/fanxb/common/Q46.java +++ b/5.leetcode/src/com/fanxb/common/Q46.java @@ -23,5 +23,7 @@ public class Q46 { public static void main(String[] args) { System.out.println(new Q46().translateNum(12258)); + new Thread().getState(); + } } diff --git a/5.leetcode/src/com/fanxb/common/Sort.java b/5.leetcode/src/com/fanxb/common/Sort.java index 02c4a21..5ec13ab 100644 --- a/5.leetcode/src/com/fanxb/common/Sort.java +++ b/5.leetcode/src/com/fanxb/common/Sort.java @@ -1,6 +1,7 @@ package com.fanxb.common; import java.util.Arrays; +import java.util.concurrent.ThreadPoolExecutor; /** * 排序 @@ -72,6 +73,9 @@ public class Sort { } } + private static final ThreadLocal threadLocal1 = new ThreadLocal<>(); + private static final ThreadLocal threadLocal2 = new ThreadLocal<>(); + public static void main(String[] args) { int[] s = {23, 49, 13, 12, 45, 393, 21, 21048, 12}; @@ -79,5 +83,7 @@ public class Sort { // quickSort(s, 0, s.length - 1); insertSort(s); System.out.println(Arrays.toString(s)); + threadLocal1.set("thread1"); + threadLocal2.set("thread2"); } }