添加了LeetCode题目解答,包括216、222、232、236、257、347、77、94、101、102、105、110题。

This commit is contained in:
whaifree 2024-07-28 22:04:52 +08:00
parent a91dc82012
commit 6320272406
12 changed files with 611 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 0:16
* @注释
*/
public class LeetCode101 {
public static void main(String[] args) {
System.out.println("hello world");
System.out.println(isSymmetric(TreeNode.constructTree(new Integer[]{1, 2, 2, 3, 1, 4, 3})));
}
public static boolean isSymmetric(TreeNode root) {
return isSymmetric(root, root);
}
private static boolean isSymmetric(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
boolean one = isSymmetric(p.right, q.left);
boolean two = isSymmetric(p.left, q.right);
return one && two;
}
}

View File

@ -0,0 +1,68 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/25 22:45
* @注释
*/
public class LeetCode102 {
public static void main(String[] args) {
TreeNode treeNode = TreeNode.constructTreeByArray(3, 9, 20, null, null, 15, 7);
for (List<Integer> integers : levelOrder1(treeNode)) {
System.out.println(integers);
}
}
public static List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.add(root);
while (!deque.isEmpty()) {
int size = deque.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode pop = deque.pop();
list.add(pop.val);
if (pop.left != null) {
deque.add(pop.left);
}
if (pop.right != null) {
deque.add(pop.right);
}
}
res.add(list);
}
return res;
}
public static List<List<Integer>> levelOrder1(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
level(root, 0, res);
return res;
}
public static void level(TreeNode root, int inLevel, List<List<Integer>> res) {
if (root == null) {
return;
}
if (res.size() == inLevel) {
res.add(new ArrayList<>());
}
res.get(inLevel).add(root.val);
level(root.left, inLevel + 1, res);
level(root.right, inLevel + 1, res);
}
}

View File

@ -0,0 +1,64 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 18:31
* @注释
*/
public class LeetCode105 {
public static void main(String[] args) {
// int[] preorder = {3, 9, 20, 15, 7};
// int[] inorder = {9, 3, 15, 20, 7};
// TreeNode root = buildTree(preorder, inorder);
// root.printTree();
//
// TreeNode treeNode = buildTree(new int[]{1}, new int[]{1});
// TreeNode.printTree(treeNode);
buildTree(new int[]{1,2,3}, new int[]{3,2,1}).printTree();
}
/**
*
*
* @param preorder {3, 9, 20, 15, 7}
* @param inorder {9, 3, 15, 20, 7}
* @return
*/
public static TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
}
public static TreeNode build(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd) {
if (preStart > preEnd) {
return null;
}
if (preStart == preEnd) {
return new TreeNode(preorder[preStart]);
}
if (inStart == inEnd) {
return new TreeNode(inorder[inStart]);
}
// 前序中的第一个
int want = preorder[preStart];
// 在中序中找到分界的位置
int inorderIndex = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == want) {
inorderIndex = i;
break;
}
}
// 这个节点中序的左边有几个节点
int leftInorderLen = inorderIndex - inStart;
TreeNode node = new TreeNode(want);
node.left = build(preorder, inorder, preStart + 1, preStart + leftInorderLen, inStart, inorderIndex - 1);
node.right = build(preorder, inorder, preStart + leftInorderLen + 1, preEnd, inorderIndex + 1, inEnd);
return node;
}
}

View File

@ -0,0 +1,39 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 0:23
* @注释
*/
public class LeetCode110 {
public static void main(String[] args) {
TreeNode treeNode = TreeNode.constructTreeByArray(1, 2, 2, 3, 3, null, null, 4, 4);
// System.out.println(isBalanced(treeNode));
System.out.println(isBalanced(TreeNode.constructTreeByArray(1,2,2,3,null,null,3,4,null,null,4)));
}
public static boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
return level(root) != -1;
}
public static int level(TreeNode root) {
if (root == null) {
return 0;
}
int left = level(root.left);
if (left==-1) return -1;
int right = level(root.right);
if (right==-1) return -1;
if (Math.abs(left - right) > 1) {
return -1;
}
return Math.max(left, right) + 1;
}
}

View File

@ -0,0 +1,48 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 21:46
* @注释
*/
public class LeetCode216 {
public static void main(String[] args) {
System.out.println(combinationSum3(9, 45));
}
static List<List<Integer>> res = new ArrayList<>();
static List<Integer> path = new ArrayList<>();
static int nowSum = 0;
public static List<List<Integer>> combinationSum3(int k, int n) {
circle(k, n, 1);
return res;
}
public static void circle(int k, int n, int start) {
if (nowSum == n && path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= 9; i++) {
/**
* nowSum + i > n
* path.size+9-i>k
*/
if (nowSum + i > n) {
break;
}
if ((9 - i + 1) < k - path.size()) {
break;
}
nowSum += i;
path.add(i);
circle(k, n, i + 1);
path.remove(path.size() - 1);
nowSum -= i;
}
}
}

View File

@ -0,0 +1,46 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 19:40
* @注释
*/
public class LeetCode222 {
public static void main(String[] args) {
System.out.println(countNodes(TreeNode.constructTreeByArray(1, 2, 3, 4, 5, 6)));
}
public static int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
return count(root);
}
public static int count(TreeNode root) {
if (root == null) {
return 0;
}
TreeNode i = root;
int leftDepth = 0;
while (i.left != null) {
i = i.left;
leftDepth++;
}
i = root;
int rightDepth = 0;
while (i.right != null) {
i = i.right;
rightDepth++;
}
if (leftDepth == rightDepth) {
return (2 << leftDepth) - 1;
}else {
return count(root.left) + count(root.right) + 1;
}
}
}

View File

@ -0,0 +1,67 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/24 22:17
* @注释
*/
public class LeetCode232 {
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue
System.out.println(myQueue.peek());
System.out.println(myQueue.pop());
System.out.println(myQueue.empty());
System.out.println(myQueue.pop());
System.out.println(myQueue.empty());
}
static class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
public MyQueue() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
while (stack1.size() > 1) {
stack2.push(stack1.pop());
}
int res = stack1.pop();
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return res;
}
public int peek() {
while (stack1.size() > 1) {
stack2.push(stack1.pop());
}
int res = stack1.pop();
stack2.push(res);
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return res;
}
public boolean empty() {
return stack1.isEmpty();
}
}
}

View File

@ -0,0 +1,34 @@
package cn.whaifree.redo.redo_all_240721;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 20:43
* @注释
*/
import cn.whaifree.leetCode.model.TreeNode;
public class LeetCode236 {
public static void main(String[] args) {
TreeNode treeNode = TreeNode.constructTreeByArray(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
lowestCommonAncestor(treeNode, new TreeNode(6), new TreeNode(4)).printTree();
}
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root.val == p.val || root.val == q.val) {
return root;
}
TreeNode right = lowestCommonAncestor(root.right, p, q);
TreeNode left = lowestCommonAncestor(root.left, p, q);
if (right == null && left == null) {
return null;
}
if (right != null && left != null) {
return root;
} if (right != null) {
return right;
} else {
return left;
}
}
}

View File

@ -0,0 +1,56 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 0:32
* @注释
*/
public class LeetCode257 {
public static void main(String[] args) {
TreeNode treeNode = TreeNode.constructTreeByArray(3, 9, 20, null, null, 15, 7);
for (String binaryTreePath : binaryTreePaths(treeNode)) {
System.out.println(binaryTreePath);
}
for (String binaryTreePath : binaryTreePaths(TreeNode.constructTreeByArray(1,2,3,null,null,4,5))) {
System.out.println(binaryTreePath);
}
}
public static List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
search(root, res, path);
return res;
}
public static void search(TreeNode root,List<String> res, List<Integer> path) {
if (root == null) {
return;
}
path.add(root.val);
if (root.left == null && root.right == null) {
int val = root.val;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < path.size() - 1; i++) {
stringBuilder.append(path.get(i)).append("->");
}
stringBuilder.append(val);
res.add(stringBuilder.toString());
}
if (root.right != null) {
search(root.right, res, path);
}
if (root.left != null) {
search(root.left, res, path);
}
path.remove(path.size() - 1);
}
}

View File

@ -0,0 +1,46 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/24 22:23
* @注释
*/
public class LeetCode347 {
public static void main(String[] args) {
int[] nums = {1,1,1,2,2,3};
int k = 2;
int[] ints = topKFrequent(nums, k);
System.out.println(Arrays.toString(ints));
}
public static int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (map.get(o1) > map.get(o2)) {
return -1;
}else {
return 1;
}
}
}
);
priorityQueue.addAll(map.keySet());
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < k; i++) {
res.add(priorityQueue.poll());
}
return res.stream().mapToInt(Integer::intValue).toArray();
}
}

View File

@ -0,0 +1,50 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 20:26
* @注释
*/
public class LeetCode77
{
public static void main(String[] args)
{
int n = 4;
int k = 2;
System.out.println(new LeetCode77().combine(n, k));
}
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combine(int n, int k)
{
circle(n, 1, k);
return res;
}
public void circle(int n, int start, int k) {
if (path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
/**
* n start k i
* 能够提供的数 n-i
* 还需要的数 k - path.size
*/
for (int i = start; i <= n; i++) {
// 剪枝
if (n - i < k - path.size() - 1) {
break;
}
path.add(i);
circle(n, i + 1, k);
path.remove(path.size() - 1);
}
}
}

View File

@ -0,0 +1,58 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/24 22:37
* @注释
*/
public class LeetCode94 {
public static void main(String[] args) {
TreeNode treeNode = TreeNode.constructTreeByArray();
List<Integer> integers = inorderTraversal(treeNode);
for (Integer integer : integers) {
System.out.println(integer);
}
}
/**
*
* @param root
* @return
*/
public static List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) {
return res;
}
Deque<TreeNode> stack = new LinkedList<>();
stack.add(root);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
if (pop != null) {
if (pop.right != null) {
stack.push(pop.right);
}
stack.push(pop);
stack.push(null);
if (pop.left != null) {
stack.push(pop.left);
}
}else {
res.add(stack.pop().val);
}
}
return res;
}
}