leecode,rt_phm更新更新
This commit is contained in:
parent
0db178165f
commit
f7bc35b4bc
|
|
@ -150,7 +150,7 @@ public class T46_105_BuildTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeNode build1(int[] preorder, int[] inorder, int stop) {//只确定右边界
|
public TreeNode build1(int[] preorder, int[] inorder, int stop) {//只确定右边界
|
||||||
if (pre > preorder.length) {
|
if (pre >= preorder.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (inorder[in] == stop) {//遍历到了右边界
|
if (inorder[in] == stop) {//遍历到了右边界
|
||||||
|
|
@ -164,4 +164,27 @@ public class T46_105_BuildTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int in1 = 0;
|
||||||
|
int pre1 = 0;
|
||||||
|
|
||||||
|
public TreeNode buildTree6(int[] preorder, int[] inorder) {
|
||||||
|
return buildChild(preorder, inorder, Integer.MIN_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode buildChild(int[] preorder, int[] inorder, int stop) {
|
||||||
|
if (pre1 > preorder.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (inorder[in1] == stop) {
|
||||||
|
in1++;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
TreeNode node = new TreeNode(preorder[pre1++]);
|
||||||
|
|
||||||
|
node.left = buildChild(preorder, inorder, node.val);
|
||||||
|
node.right = buildChild(preorder, inorder, stop);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import org.junit.Test;
|
||||||
*@Description: TODO 力扣152 乘积最大子数组
|
*@Description: TODO 力扣152 乘积最大子数组
|
||||||
*@Version: 1.0
|
*@Version: 1.0
|
||||||
*/
|
*/
|
||||||
public class T57_142_MaxProduct {
|
public class T57_152_MaxProduct {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
|
|
@ -77,4 +77,20 @@ public class T57_142_MaxProduct {
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int maxProduct3(int[] nums) {
|
||||||
|
int max = nums[0];
|
||||||
|
int min = nums[0];
|
||||||
|
int resultMax = nums[0];
|
||||||
|
|
||||||
|
//每一个位置都是表示要当前值的最大值,因为要求子数组,如果不要就没办法计算了
|
||||||
|
for (int i = 1; i < nums.length; i++) {
|
||||||
|
int temp = max;
|
||||||
|
max = Math.max(Math.max(nums[i], nums[i] * max), nums[i] * min);
|
||||||
|
min = Math.min(Math.min(nums[i], nums[i] * temp), nums[i] * min);
|
||||||
|
if (resultMax < max) resultMax = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultMax;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,6 +73,7 @@ public class T64_207_CanFinish {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean valid = true;
|
boolean valid = true;
|
||||||
|
|
||||||
//本质上就是图的深度优先搜索:检测这个图是否存在环路
|
//本质上就是图的深度优先搜索:检测这个图是否存在环路
|
||||||
public boolean canFinish1(int numCourses, int[][] prerequisites) {
|
public boolean canFinish1(int numCourses, int[][] prerequisites) {
|
||||||
edge = new ArrayList<>();
|
edge = new ArrayList<>();
|
||||||
|
|
@ -115,4 +116,48 @@ public class T64_207_CanFinish {
|
||||||
visited[start] = 2;//全都检查完了,没有问题
|
visited[start] = 2;//全都检查完了,没有问题
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean canFinish2(int numCourses, int[][] prerequisites) {
|
||||||
|
|
||||||
|
//遍历prerequisites确定每个节点的子节点
|
||||||
|
edge = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < numCourses; i++) {
|
||||||
|
edge.add(new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int[] prerequisite : prerequisites) {
|
||||||
|
edge.get(prerequisite[0]).add(prerequisite[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//挨个遍历所有的节点,判断是否会形成环路
|
||||||
|
int[] visited = new int[numCourses];
|
||||||
|
for (int i = 0; i < edge.size() && valid; i++) {
|
||||||
|
if (visited[i] == 0) {
|
||||||
|
//从来没有遍历过
|
||||||
|
dfs1(i, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dfs1(int start, int[] visited) {
|
||||||
|
visited[start] = 1;//正在遍历该节点
|
||||||
|
List<Integer> children = edge.get(start);
|
||||||
|
for (Integer child : children) {
|
||||||
|
if(visited[child]==0){
|
||||||
|
//继续遍历,判断是否可以
|
||||||
|
dfs1(child,visited);
|
||||||
|
if(!valid){
|
||||||
|
return;//找到环路,不在进行遍历
|
||||||
|
}
|
||||||
|
}else if(visited[child]==1){
|
||||||
|
//找到环路,不在进行遍历
|
||||||
|
valid=false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
visited[start]=2;//遍历完成
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
package com.markilue.leecode.hot100.second;
|
||||||
|
|
||||||
|
import com.markilue.leecode.tree.TreeNode;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-05-23 10:54
|
||||||
|
*@Description: TODO 力扣208 实现Trie(前缀树)
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class T65_208_Trie {
|
||||||
|
|
||||||
|
private T65_208_Trie[] children;
|
||||||
|
boolean isEnd;
|
||||||
|
|
||||||
|
|
||||||
|
public T65_208_Trie() {
|
||||||
|
children = new T65_208_Trie[26];
|
||||||
|
isEnd = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(String word) {
|
||||||
|
//挨个遍历插入
|
||||||
|
T65_208_Trie node = this;
|
||||||
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
char value = word.charAt(i);
|
||||||
|
int index = value - 'a';
|
||||||
|
if (node.children[index] == null) {
|
||||||
|
node.children[index] = new T65_208_Trie();
|
||||||
|
}
|
||||||
|
node = node.children[index];//寻找他的下一个
|
||||||
|
}
|
||||||
|
node.isEnd = true;//遍历到了最后
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean search(String word) {
|
||||||
|
T65_208_Trie node = searchPrefix(word);
|
||||||
|
return node != null && node.isEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean startsWith(String prefix) {
|
||||||
|
T65_208_Trie node = searchPrefix(prefix);
|
||||||
|
return node != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T65_208_Trie searchPrefix(String word) {
|
||||||
|
T65_208_Trie node = this;
|
||||||
|
|
||||||
|
for (int j = 0; j < word.length(); j++) {
|
||||||
|
char c = word.charAt(j);
|
||||||
|
int index = c - 'a';
|
||||||
|
if (node.children[index] == null) {
|
||||||
|
return null;//没有找到
|
||||||
|
}
|
||||||
|
node = node.children[index];
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue