Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

补交作业 第二周到第四周 学号104 #865

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Week_02/id_104/LeetCode_671_104.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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) {
//找到第一个小于此节点的值既可以
int result=getMin(root,root.val);
return result;
}


/**
** 获取第二最小值
*/
public int getMin(TreeNode tree,int val){

if(tree.val!=val){
return tree.val;
}

if(tree.left==null){
return -1;
}

if(tree.right==null){
return -1;
}

int l=getMin(tree.left,val);
int r=getMin(tree.right,val);

if(l==-1 && r==-1){
return -1;
}

if(l!=-1 && r!=-1){
return Math.min(l,r);
}

return l+r+1;

}
}
29 changes: 29 additions & 0 deletions Week_02/id_104/LeetCode_692_104.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public List<String> topKFrequent(String[] words, int k) {
//思路
//1.遍历单词 统计每个单词出现的数量
Map<String,Integer> wordsMap=new HashMap<String,Integer>();
int num=0;
for(String word:words){
wordsMap.put(word,wordsMap.getOrDefault(word,0)+1);
}

PriorityQueue<Map.Entry<String,Integer>> queue=new PriorityQueue<>(
new Comparator<Map.Entry<String,Integer>>(){
@Override
public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2){
if(o1.getValue()==o2.getValue()){
return o1.getKey().compareTo(o2.getKey());
}
return o2.getValue()-o1.getValue();
}
}
);
queue.addAll(wordsMap.entrySet());
List<String> res=new ArrayList(k);
for(int i=0;i<k;i++){
res.add(queue.poll().getKey());
}
return res;
}
}
54 changes: 54 additions & 0 deletions Week_02/id_104/LeetCode_692_104_v1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Solution {
public List<String> topKFrequent(String[] words, int k) {
//思路
//1.遍历单词 统计每个单词出现的数量
Map<String,Integer> wordsMap=new HashMap<String,Integer>();
int num=0;
for(String word:words){
if(wordsMap.containsKey(word)){
int c=wordsMap.get(word)+1;
num=num>c?num:c;
wordsMap.put(word,c);
}else{
wordsMap.put(word,1);
}
}
num=num+1;
//2.按数量进行排序
String[] wordsArray=new String[num];
// for(int i=0;i<num;i++){
// wordsArray[i]="";
// }
for(Map.Entry<String,Integer> entry:wordsMap.entrySet()){
String word=entry.getKey();
int count = entry.getValue();
if(wordsArray[count]==null){
wordsArray[count]=word;
} else{
//3.数量相同的 按名称排序
String oldWord=wordsArray[count];
//如果当前单词字母更靠前一些 则替换旧值
if(word.compareTo(oldWord)<0){
//替换
wordsArray[count]=word;
}
}
}

//4.遍历排序后的集合,取第n个
int j=0;
String result="";
for(int i=num-1;i>=0;i--){
if(!"".equals(wordsArray[i])){
j++;
}
if(j==k){
result=wordsArray[i];
break;
}
}
List resultList=new ArrayList();
resultList.add(result);
return resultList;
}
}
5 changes: 4 additions & 1 deletion Week_02/id_104/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# 学习笔记
# 学习笔记

学到了一个很取巧的计算,比如返回两个值中的一个,知道其中一个会是固定值,可以通过相加的情况再减固定值的情况,拿到需要的数据,非常棒。

19 changes: 19 additions & 0 deletions Week_03/id_104/LeetCode_104_104.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int left=maxDepth(root.left);
int right=maxDepth(root.right);
return (left>right?left:right)+1;
}
}
42 changes: 42 additions & 0 deletions Week_03/id_104/LeetCode_997_104.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {
public int findJudge(int N, int[][] trust) {
if(N==1 && trust.length==0){
return N;
}
//二维数组 人(被信任次数,信任别人次数)
int[][] peopleAry=new int[N+1][2];

//1. 遍历信任关系
for(int[] t:trust){
//2. 设计数据结构 存储每个人的信任与被信任关系

//信任别人的人
int trustOther = t[0];
//2.2 信任别人次数 ++
peopleAry[trustOther][1]++;

//被信任的人
int receiveTrust=t[1];
//2.1 被信任次数 ++
peopleAry[receiveTrust][0]++;

}
List<Integer> result=new ArrayList();

//3.遍历每个人的信任情况
for(int i=0;i<=N;i++){
int[] person=peopleAry[i];
if(person[0]==N-1 && person[1]==0){
result.add(i);
}
}

//4.判断数组长度 是不是为1 为1,则输出数组记录的人
if(result.size()==1){
return result.get(0);
}
return -1;


}
}
4 changes: 3 additions & 1 deletion Week_03/id_104/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# 学习笔记
# 学习笔记
学习了递归还有巧妙计算法官的问题,试着解析问题,找到稳定条件去判断结果。

117 changes: 117 additions & 0 deletions Week_04/id_104/LeetCode_720_104.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
class Solution {
public String longestWord(String[] words) {
Trie trie=new Trie();
for(String word:words){
trie.save(word);
}

//遍历树结构 找到树的叶子节点 求此叶子节点长度 取最大
searchLongestWord(new StringBuilder(),trie.children);
return longestWord;
}

String longestWord="";

private void searchLongestWord(StringBuilder sb,Node[] children){
if(children==null){
return;
}
for(Node node:children){
if(node!=null && node.isEnd){
sb.append(node.val);
if(sb.length()>longestWord.length()){
longestWord=sb.toString();
}
searchLongestWord(sb,node.children);
sb.deleteCharAt(sb.length()-1);
}


}
}







class Node{

public char val;
public Boolean isEnd;
public Node[] children;

public Node(char val){
this.val=val;
isEnd=false;
children=new Node[26];
}

public void setIsEnd(boolean isEnd){
this.isEnd=isEnd;
}


}


/**
*前缀树
*/
class Trie{

Node[] children;

public Trie(){
children=new Node[26];
}

public void save(String word){
if(word==null || word.length()==0){
return ;
}
insert(0,word.toCharArray(),children);
}


public void insert(int l,char[] chars,Node[] children){
int i=chars[l]-'a';
if(children[i]==null){
children[i]=new Node(chars[l]);
}
if(l==(chars.length-1)){
children[i].setIsEnd(true);
return;
}

if(children[i].children==null){
children[i].children=new Node[26];
}
insert(l+1,chars,children[i].children);

}

public boolean find(String word){
char[] chars=word.toCharArray();
Node[] childs=children;
boolean result=false;
for(char c:chars){
int index=c-'a';
if(childs[index]==null){
return result;
}
result=childs[index].isEnd;
childs=childs[index].children;

}

return result;
}


}



}
13 changes: 13 additions & 0 deletions Week_04/id_104/LeetCode_746_104.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int minCostClimbingStairs(int[] cost) {
int length=cost.length;
int[] depth=new int[length];
depth[0]=cost[0];
depth[1]=cost[1];
for(int i=2;i<cost.length;i++){
depth[i]=Math.min(depth[i-1],depth[i-2])+cost[i];
}
return Math.min(depth[length-1],depth[length-2]);

}
}
4 changes: 3 additions & 1 deletion Week_04/id_104/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# 学习笔记
# 学习笔记
学习了构建树结构,并且在树结构中找到最长单词,这个就比较厉害了。