This commit is contained in:
fanxb 2021-04-30 14:58:46 +08:00
parent e356c46f66
commit 7ca8b56944
5 changed files with 147 additions and 0 deletions

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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"));
}
}

View File

@ -23,5 +23,7 @@ public class Q46 {
public static void main(String[] args) {
System.out.println(new Q46().translateNum(12258));
new Thread().getState();
}
}

View File

@ -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<String> threadLocal1 = new ThreadLocal<>();
private static final ThreadLocal<String> 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");
}
}