Skip to content

Commit

Permalink
update algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
rbmonster committed Mar 11, 2023
1 parent 216eb32 commit 900ace1
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 334 deletions.
28 changes: 22 additions & 6 deletions src/main/java/com/learning/algorithm/ALGORITHM.md
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ public void backTracking() {
3. **排列问题中的树层去重**

```java
public class Soluction {
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
boolean[] used = new boolean[nums.length];
Expand Down Expand Up @@ -1647,11 +1647,15 @@ public class Soluction {
### 去重问题横向对比

- [组合总和 II](https://leetcode-cn.com/problems/combination-sum-ii/submissions/):
排序后树层去重`if(i>index && candidates[i]== candidates[i-1])`
排序后树层去重\
`if(i>index && candidates[i]== candidates[i-1])`
- [全排列 II](https://leetcode-cn.com/problems/permutations-ii/):
排序后树层去重```if(i>0 && nums[i] == nums[i-1] && used[i-1] == false) ```
- [子集 II](https://leetcode-cn.com/problems/subsets-ii/): 树层去重`if (i != index && nums[i] == nums[i-1])`
- [递增子序列](https://leetcode-cn.com/problems/increasing-subsequences/): 无序元素树层去重`if(used.contains(nums[i]))`
排序后树层去重\
```if(i>0 && nums[i] == nums[i-1] && used[i-1] == false) ```
- [子集 II](https://leetcode-cn.com/problems/subsets-ii/): 树层去重\
`if (i != index && nums[i] == nums[i-1])`
- [递增子序列](https://leetcode-cn.com/problems/increasing-subsequences/): 无序元素树层去重\
`if(used.contains(nums[i]))`


### 其他
Expand Down Expand Up @@ -2322,6 +2326,18 @@ public class Solution {
}
return true;
}

public static boolean isPrime(int N) {
if (N < 2) {
return false;
}
for (int i = 2; i * i <= N; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
}
```

Expand Down Expand Up @@ -2350,7 +2366,7 @@ public class Solution {

- [最大子序和](https://leetcode-cn.com/problems/maximum-subarray/)

```
```java
class Solution {
public int maxSubArray(int[] nums) {
int dp = 0, res = Integer.MIN_VALUE;
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/learning/algorithm/KMP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.learning.algorithm;

/**
* KMP算法本质上是模拟指针的回退
* 适用于重复性很高的文本 中查找重复性很高的"模式"(即字符串)
*/
public class KMP {

private String pat;

private int[][] dfa;

public KMP(String pat) {
this.pat = pat;
int M = pat.length();
// 字母表的大小R
int R = 256;
dfa = new int[R][M];
dfa[pat.charAt(0)][0] = 1;
// j = 1 忽略首字母 表示模式需要右移动一位
for (int j = 1, X = 0; j < M; j++) {
for (int c = 0; c < R; c++) {
// 复制模式失败情况下的值
dfa[c][j] = dfa[c][X];
}
// 设置匹配成功情况下的值, dfa[pat.charAt(j)][j] 总是 j+1 表示匹配上往前进一位
dfa[pat.charAt(j)][j] = j+1;
// 更新重启状态
X = dfa[pat.charAt(j)][X];
}
}


public int search(String txt) {
int i, j , N = txt.length(), M = pat.length();
for (i = 0, j = 0; i < N && j<M ; i++) {
j = dfa[txt.charAt(i)][j];
}
if (j == M) {
return i-M; // 找到匹配
}
else {
return N; // 未找到匹配
}
}


public static void main(String[] args) {
KMP kmp = new KMP("sadfjweasdfasd");
kmp.search("qwejriasdnfzxc,mvnz,xmcnvasdf");

}
}
36 changes: 0 additions & 36 deletions src/main/java/com/learning/algorithm/basic/dynamic/KMP.java

This file was deleted.

71 changes: 71 additions & 0 deletions src/main/java/com/learning/algorithm/trie/TST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.learning.algorithm.trie;

/**
* 三向单词查找树
*
*
* @param <T>
*/
public class TST<T> {

private Node<T> root; // 单词查找树 根节点

private static class Node<T> {
private T val;

char c;

private Node<T> mid, left, right;
}

public T get(String str) {
Node<T> x = get(root, str, 0);
if (x == null) {
return null;
}
return x.val;
}

private Node<T> get(Node<T> node, String str, int d) {
if (node == null) {
return null;
}
char ch = str.charAt(d);
if (ch < node.c) {
return get(node.right, str, d);
} else if (ch < node.c) {
return get(node.left, str, d);
} else {
if (d < str.length() - 1) {
return get(node.mid, str, d + 1);
} else {
return node;
}
}
}

public void put(String str, T value) {
this.root = put(root, str, 0, value);
}

private Node<T> put(Node<T> node, String str, int d, T value) {
char ch = str.charAt(d);
if (node == null) {
node = new Node<T>();
node.c = ch;
}
if (ch < node.c) {
node.left = put(node.left, str, d, value);
} else if (ch > node.c) {
node.right = put(node.right, str, d, value);
} else {
if (d < str.length() - 1) {
node.mid = put(node.mid, str, d + 1, value);
} else {
node.val = value;
}
}
return node;
}

}
5 changes: 4 additions & 1 deletion src/main/java/com/learning/algorithm/trie/TrieST.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.util.LinkedList;

/**
* 字典树
* R向字典树
* R 越大占用的空间越大
* (处理由大小为R的字母表构造的N个字符串, w为单词平均长度)
* 一棵R向单词查找树的链接总数在RN ~ RNw
* @param <T>
*/
public class TrieST<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/learning/offer/LastRemaining.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* </pre>
*
* @version v1.0
* @ClassName: LastRemaining_Soluction
* @ClassName: LastRemaining_Solution
* @Author: 86159
* @Date: 2020/3/24 20:55
*/
Expand Down
Loading

0 comments on commit 900ace1

Please sign in to comment.