From cba059fef63768c54c85ea62cf6dea5cba28b17f Mon Sep 17 00:00:00 2001 From: gongwentao <326967648@qq.com> Date: Sun, 19 May 2019 17:41:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/id_104/LeetCode_671_104.java | 49 ++++++++++ Week_02/id_104/LeetCode_692_104.java | 29 ++++++ Week_02/id_104/LeetCode_692_104_v1.java | 54 +++++++++++ Week_02/id_104/NOTE.md | 5 +- Week_03/id_104/LeetCode_104_104.java | 19 ++++ Week_03/id_104/LeetCode_997_104.java | 42 +++++++++ Week_03/id_104/NOTE.md | 4 +- Week_04/id_104/LeetCode_720_104.java | 117 ++++++++++++++++++++++++ Week_04/id_104/LeetCode_746_104.java | 13 +++ Week_04/id_104/NOTE.md | 4 +- 10 files changed, 333 insertions(+), 3 deletions(-) create mode 100644 Week_02/id_104/LeetCode_671_104.java create mode 100644 Week_02/id_104/LeetCode_692_104.java create mode 100644 Week_02/id_104/LeetCode_692_104_v1.java create mode 100644 Week_03/id_104/LeetCode_104_104.java create mode 100644 Week_03/id_104/LeetCode_997_104.java create mode 100644 Week_04/id_104/LeetCode_720_104.java create mode 100644 Week_04/id_104/LeetCode_746_104.java diff --git a/Week_02/id_104/LeetCode_671_104.java b/Week_02/id_104/LeetCode_671_104.java new file mode 100644 index 00000000..e50c5f94 --- /dev/null +++ b/Week_02/id_104/LeetCode_671_104.java @@ -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; + + } +} \ No newline at end of file diff --git a/Week_02/id_104/LeetCode_692_104.java b/Week_02/id_104/LeetCode_692_104.java new file mode 100644 index 00000000..e9966dfe --- /dev/null +++ b/Week_02/id_104/LeetCode_692_104.java @@ -0,0 +1,29 @@ +class Solution { + public List topKFrequent(String[] words, int k) { + //思路 + //1.遍历单词 统计每个单词出现的数量 + Map wordsMap=new HashMap(); + int num=0; + for(String word:words){ + wordsMap.put(word,wordsMap.getOrDefault(word,0)+1); + } + + PriorityQueue> queue=new PriorityQueue<>( + new Comparator>(){ + @Override + public int compare(Map.Entry o1,Map.Entry o2){ + if(o1.getValue()==o2.getValue()){ + return o1.getKey().compareTo(o2.getKey()); + } + return o2.getValue()-o1.getValue(); + } + } + ); + queue.addAll(wordsMap.entrySet()); + List res=new ArrayList(k); + for(int i=0;i topKFrequent(String[] words, int k) { + //思路 + //1.遍历单词 统计每个单词出现的数量 + Map wordsMap=new HashMap(); + 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 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; + } +} \ No newline at end of file diff --git a/Week_02/id_104/NOTE.md b/Week_02/id_104/NOTE.md index c684e62f..74f6582d 100644 --- a/Week_02/id_104/NOTE.md +++ b/Week_02/id_104/NOTE.md @@ -1 +1,4 @@ -# 学习笔记 \ No newline at end of file +# 学习笔记 + + 学到了一个很取巧的计算,比如返回两个值中的一个,知道其中一个会是固定值,可以通过相加的情况再减固定值的情况,拿到需要的数据,非常棒。 + diff --git a/Week_03/id_104/LeetCode_104_104.java b/Week_03/id_104/LeetCode_104_104.java new file mode 100644 index 00000000..eafe1c28 --- /dev/null +++ b/Week_03/id_104/LeetCode_104_104.java @@ -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; + } +} \ No newline at end of file diff --git a/Week_03/id_104/LeetCode_997_104.java b/Week_03/id_104/LeetCode_997_104.java new file mode 100644 index 00000000..f3400e1a --- /dev/null +++ b/Week_03/id_104/LeetCode_997_104.java @@ -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 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; + + + } +} \ No newline at end of file diff --git a/Week_03/id_104/NOTE.md b/Week_03/id_104/NOTE.md index c684e62f..602b8f73 100644 --- a/Week_03/id_104/NOTE.md +++ b/Week_03/id_104/NOTE.md @@ -1 +1,3 @@ -# 学习笔记 \ No newline at end of file +# 学习笔记 + 学习了递归还有巧妙计算法官的问题,试着解析问题,找到稳定条件去判断结果。 + \ No newline at end of file diff --git a/Week_04/id_104/LeetCode_720_104.java b/Week_04/id_104/LeetCode_720_104.java new file mode 100644 index 00000000..3e7025d4 --- /dev/null +++ b/Week_04/id_104/LeetCode_720_104.java @@ -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; + } + + + } + + + +} \ No newline at end of file diff --git a/Week_04/id_104/LeetCode_746_104.java b/Week_04/id_104/LeetCode_746_104.java new file mode 100644 index 00000000..b7c679c6 --- /dev/null +++ b/Week_04/id_104/LeetCode_746_104.java @@ -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