diff --git a/Week_02/id_35/LeetCode_03_35.java b/Week_02/id_35/LeetCode_03_35.java new file mode 100644 index 00000000..9187802a --- /dev/null +++ b/Week_02/id_35/LeetCode_03_35.java @@ -0,0 +1,131 @@ +package com.leecode.week02; + +import com.sun.org.apache.xpath.internal.operations.Bool; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ + * 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 + */ +public class LeetCode_03_35 { + public void printAllSubstring(String s){ + char[] chs=s.toCharArray(); + for(int i=0;i set=new HashSet<>(); + for(int i=0;i set=new HashSet<>(); + for(int i=0;i cache=new HashMap<>(); + public boolean hasRepeatingChar(String s){ + + if(cache.containsKey(s)){ + return cache.get(s); + } + + char[] chs=s.toCharArray(); + Set characters=new HashSet<>(); + for (char ch:chs) { + if(characters.contains(ch)){ + cache.put(s,true); + return true; + }else { + characters.add(ch); + } + + } + cache.put(s,false); + return false; + }*/ +} diff --git a/Week_02/id_35/LeetCode_242_35.java b/Week_02/id_35/LeetCode_242_35.java new file mode 100644 index 00000000..ff02a35a --- /dev/null +++ b/Week_02/id_35/LeetCode_242_35.java @@ -0,0 +1,73 @@ +package com.leecode.week02; + +import java.util.Arrays; + +/** + * 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 + * 输入: s = "anagram", t = "nagaram" + * 输出: true + */ +public class LeetCode_242_35 { + public static void main(String[] args) { + LeetCode_242_35 lc=new LeetCode_242_35(); + System.out.println(lc.isAnagram("anagram","nagaram")); + + System.out.println(lc.isAnagram("rat","art")); + System.out.println(lc.isAnagram("rat"," car")); + //Arrays.equals("anagram".toCharArray(),"nagaram".toCharArray()); + } + + public boolean isAnagram(String s, String t) + { + if(s==null||t==null||s.length()!=t.length()){ + return false; + } + int[] sArr=new int[128]; + int[] tArr=new int[128]; + char[] sChs=s.toCharArray(); + char[] tChs=t.toCharArray(); + + for(int i=0;i cache=new HashMap<>(); + + for (int i=0;i> list= lc.levelOrder(treeNode); + System.out.println(list); + + } + + /** + * 思考过程,我是做了多叉树按层次遍历的题目之后来做这道题目的 + * + * @param root + * @return + */ + public List> levelOrder(TreeNode root) { + + List> list=new ArrayList<>(); + if(root==null){ + return list; + } + + Queue queue=new ArrayDeque<>(); + queue.offer(root); + + while (!queue.isEmpty()){ + int queueSize=queue.size(); + List ls =new ArrayList<>(queueSize); + + for(int i=0;i> list= lc.zigzagLevelOrder(treeNode); + System.out.println(list); + + } + + /** + * 思考过程,我是做了多叉树按层次遍历的题目之后来做这道题目的 + * ,同样在上面基础上面进行改进,要从左到右,然后从右到做,只要把每层加入的时候顺序往后或者往前就好了 + * 需要注意的是,出队列是有顺序的,所以只能是在写入list中元素的下标进行控制了 + * + * @param root + * @return + */ + public List> zigzagLevelOrder(TreeNode root) { + + + List> list=new ArrayList<>(); + if(root==null){ + return list; + } + + Queue queue=new ArrayDeque<>(); + queue.offer(root); + boolean needReverse=false;//false ->从左到右 true->从右到左 + + while (!queue.isEmpty()){ + int queueSize=queue.size(); + Integer[] arr=new Integer[queueSize]; + + for(int i=0;i"+maxDept); + } + + + /** + * 递归 + * @param root + * @return + */ + public int maxDepth(TreeNode root) { + return root == null ? 0 : (1 + Math.max(maxDepth(root.left), maxDepth(root.right))); + } + + +} diff --git a/Week_03/id_35/Leetcode_107_35.java b/Week_03/id_35/Leetcode_107_35.java new file mode 100644 index 00000000..fc397789 --- /dev/null +++ b/Week_03/id_35/Leetcode_107_35.java @@ -0,0 +1,77 @@ +package com.leecode.week03; + +import com.leecode.TreeNode; + +import java.util.*; + +/** + * 107. Binary Tree Level Order Traversal II + */ + +public class Leetcode_107_35 { + + public static void main(String[] args) { + Leetcode_107_35 lc=new Leetcode_107_35(); + //int[] arr={3,2,9,-1,-1,10,-1,-1,8,-1,11,-1}; + //[3,9,20,null,null,15,7] + int[] arr={3,9,20,-1,-1,-1,15,7}; + // int[] arr={3,2}; + TreeNode treeNode=TreeNode.createBinaryV1(arr,arr.length); + + List> list= lc.levelOrderBottom(treeNode); + System.out.println(list); + + } + + /** + * 思考过程,我是做了多叉树按层次遍历的题目之后来做这道题目的 + * ,直接想法就是把结果反着输出就好了,要反着输出,就把结果压到栈上面 + * + * @param root + * @return + */ + public List> levelOrderBottom(TreeNode root) { + + List> list=new ArrayList<>(); + if(root==null){ + return list; + } + + Stack> stack=new Stack<>(); + + Queue queue=new ArrayDeque<>(); + queue.offer(root); + + while (!queue.isEmpty()){ + int queueSize=queue.size(); + List ls =new ArrayList<>(queueSize); + + for(int i=0;i ls =stack.pop(); + list.add(ls); + } + + return list; + } +} diff --git a/Week_03/id_35/Leetcode_111_35.java b/Week_03/id_35/Leetcode_111_35.java new file mode 100644 index 00000000..74c34269 --- /dev/null +++ b/Week_03/id_35/Leetcode_111_35.java @@ -0,0 +1,54 @@ +package com.leecode.week03; + +import com.geekbang.BinaryTree; +import com.leecode.TreeNode; + +import java.util.ArrayDeque; +import java.util.Queue; + +/** + * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ + * 给定一个二叉树,找出其最小深度 + */ +public class Leetcode_111_35 { + public static void main(String[] args) { + Leetcode_111_35 lc=new Leetcode_111_35(); + //int[] arr={3,2,9,-1,-1,10,-1,-1,8,-1,11,-1}; + //[3,9,20,null,null,15,7] + int[] arr={3,9,20,-1,-1,-1,15,7}; + // int[] arr={3,2}; + TreeNode treeNode=TreeNode.createBinaryV1(arr,arr.length); + int minDept= lc.minDepth(treeNode); + System.out.println("minDepth->"+minDept); + } + + + /** + * 思考过程: + * 凡是要了解深度,都是要遍历到叶子节点,直接的想法就是遍历到所有的叶子节点,找到深度最小的那波即可 + * + * @param root + * @return + */ + public int minDepth(TreeNode root) { + + if(root==null){ + return 0; + } + //当前的叶子节点的条件,左右都是null的 + if(root.left==null&&root.right==null){ + return 1; + } + int minDept=Integer.MAX_VALUE; + if(root.left!=null){ + minDept=Math.min(minDepth(root.left),minDept); + } + + if(root.right!=null){ + minDept=Math.min(minDepth(root.right),minDept); + } + + //左右节点中不为空的情况,会往下递推一层,深度+1 + return minDept+1; + } +} diff --git a/Week_03/id_35/Leetcode_429_35.java b/Week_03/id_35/Leetcode_429_35.java new file mode 100644 index 00000000..af36d198 --- /dev/null +++ b/Week_03/id_35/Leetcode_429_35.java @@ -0,0 +1,128 @@ +package com.leecode.week03; + +import com.leecode.TreeNode; + +import java.util.*; + +/** + * + * N叉树的层序遍历 + */ +public class Leetcode_429_35 { + + class Node { + public int val; + public List children; + + public Node() {} + + public Node(int _val,List _children) { + val = _val; + children = _children; + } + }; + + + public static void main(String[] args) { + Leetcode_429_35 lc=new Leetcode_429_35(); + + Node root=lc.createTree(); + + + + List> list= lc.levelOrder(root); + System.out.println(list); + + } + + + public Node createTree(){ + List _children56=new ArrayList<>(); + Node n5=new Node(5,null); + Node n6=new Node(6,null); + + _children56.add(n5); + _children56.add(n6); + + + List _children324=new ArrayList<>(); + Node n3=new Node(3,_children56); + Node n2=new Node(2,null); + + List _children78=new ArrayList<>(); + + Node n7=new Node(7,null); + Node n8=new Node(8,null); + + _children78.add(n7); + _children78.add(n8); + + Node n4=new Node(4,_children78); + + + + _children324.add(n3); + _children324.add(n2); + _children324.add(n4); + + Node root=new Node(1,_children324); + + return root; + + + + + } + + + /** + * 思考过程,按照层次遍历只是可以打印顺序,一开始是直接输出 + * 但是碰到有个问题的就是层次没注意, + * 第一把提交未通过,再寻找原因 + *后面发现每次入队列的时候队列上面的元素其实就是当前层的顺序,但是这次需要每次取元素的时候把 + * 队列里面的元素都拿出来 + * 拿出来之后队列必然是空的,接下来就把下一层元素全部放进队列里面 + * 每个元素会被访问到两次,入队列一次,出队列一次 + * @param root + * @return + */ + public List> levelOrder(Node root) { + + List> list=new ArrayList<>(); + if(root==null){ + return list; + } + + Queue queue=new ArrayDeque<>(); + queue.offer(root); + + + while (!queue.isEmpty()){ + int curQueueSize=queue.size(); + List ls=new ArrayList<>(curQueueSize); + for (int i=0;i