文档添加关于字符串、字节和void数据类型的说明

在NumPy文档中,常规介绍部分现在包含了关于字符串、字节和void数据类型的信息,以及现有的数值类型。

Default Changelist
LC15.java
LeetCode40.java
LeetCode55.java
LeetCode94.java
LeetCode131.java
LeetCode209.java
LeetCode222.java
LeetCode276.java
p1.java
This commit is contained in:
whaifree 2024-09-24 22:59:28 +08:00
parent 6d49b27147
commit 2219024e1b
9 changed files with 495 additions and 4 deletions

View File

@ -0,0 +1,10 @@
package cn.whaifree.interview.qz.suiyou;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 19:18
* @注释
*/
public class p1 {
}

View File

@ -1,11 +1,7 @@
package cn.whaifree.leetCode.Greedy; package cn.whaifree.leetCode.Greedy;
import cn.whaifree.leetCode.Tree.LeetCode94;
import org.junit.Test; import org.junit.Test;
import java.net.URL;
/** /**
* @version 1.0 * @version 1.0
* @Author whai文海 * @Author whai文海
@ -18,8 +14,27 @@ public class LeetCode55 {
public void test() { public void test() {
System.out.println(new Solution().canJump(new int[]{2,0,0})); System.out.println(new Solution().canJump(new int[]{2,0,0}));
char c = 10 + 90;
System.out.println(c);
long g = 9999999999L;
System.out.println(g);
}
public static void main(String[] args) {
System.out.println(i());
}
public static int i() {
try {
return 1;
} catch (Exception e) {
} finally {
return 0;
}
} }

View File

@ -0,0 +1,68 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 10:25
* @注释
*/
public class LC15{
@Test
public void test() {
Solution solution = new Solution();
int[] nums = {-1, 0, 1, 2, -1, -4};
List<List<Integer>> lists = solution.threeSum(nums);
System.out.println(lists);
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> threeSum(int[] nums) {
return threeSum(nums, 0);
}
public List<List<Integer>> threeSum(int[] nums, int target) {
Arrays.sort(nums);
for (int left = 0; left < nums.length - 2; left++) {
if (left > 0 && nums[left] == nums[left - 1]) {
continue;
}
int right = nums.length - 1;
int mid = left + 1;
while (mid < right) {
int tmpSum = nums[left] + nums[mid] + nums[right];
if (tmpSum == target) {
res.add(Arrays.asList(nums[left], nums[mid], nums[right]));
while (mid < right && nums[mid] == nums[mid + 1]) {
mid++;
}
while (mid < right && nums[right] == nums[right - 1]) {
right--;
}
mid++;
right--;
}else if (tmpSum > target) {
right--;
} else {
mid++;
}
}
}
return res;
}
}
}

View File

@ -0,0 +1,61 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 12:46
* @注释
*/
public class LeetCode131 {
@Test
public void test() {
Solution solution = new Solution();
List<List<String>> lists = solution.partition("aab");
System.out.println(lists);
}
class Solution {
List<List<String>> res = new ArrayList<>();
List<String> path = new ArrayList<>();
public List<List<String>> partition(String s) {
backTracking(s, 0);
return res;
}
public void backTracking(String s, int start) {
if (start >= s.length()) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i < s.length(); i++) {
if (isHuiWen(s, start, i)) {
path.add(s.substring(start, i + 1));
backTracking(s, i + 1);
path.remove(path.size() - 1);
}
}
}
public boolean isHuiWen(String s, int start, int end) {
while (start < end) {
if (s.charAt(start) != s.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}
}

View File

@ -0,0 +1,95 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 10:41
* @注释
*/
public class LeetCode209 {
@Test
public void test() {
int[] nums = {1,4,4};
int target = 4;
int i = new Solution().minSubArrayLen(target, nums);
System.out.println(i);
}
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int right = 0;
int left = 0;
int nowSum = 0;
int res = Integer.MAX_VALUE;
while (right < nums.length) {
nowSum += nums[right];
while (left <= right && nowSum >= target) {
res = Math.min(res, right - left + 1);
nowSum -= nums[left];
left++;
}
right++;
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
public static void main(String[] args) {
int[] ints = {1, 2, 3, 4, 9, 10};
int i = Arrays.binarySearch(ints, 6);
System.out.println(i);
}
@Test
public void test1() {
int[] nums = {2, 3, 1, 2, 4, 3};
int target = 7;
int i = new Solution1().minSubArrayLen(target, nums);
System.out.println(i);
}
class Solution1 {
/**
* | x | target |
* 0 n i
* @param target
* @param nums
* @return
*/
public int minSubArrayLen(int target, int[] nums) {
int[] preSum = new int[nums.length];
preSum[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
preSum[i] = preSum[i - 1] + nums[i];
}
int res = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
int A = preSum[i];
if (A < target) {
continue;
}
int tar = A - target;
int index = Arrays.binarySearch(preSum, tar);
if (index < 0) {
index = -index - 1;
} else {
index = index + 1;
}
res = Math.min(res, i - index + 1);
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
}

View File

@ -0,0 +1,46 @@
package cn.whaifree.redo.redo_all_240924;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 12:05
* @注释
*/
public class LeetCode222 {
@Test
public void test() {
TreeNode treeNode = TreeNode.constructTreeByArray(1, 2, 3, 4);
System.out.println(new Solution().countNodes(treeNode));
}
class Solution {
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
TreeNode left = root;
int leftCount = 0;
while (left != null) {
leftCount++;
left = left.left;
}
TreeNode right = root;
int rightCount = 0;
while (right != null) {
rightCount++;
right = right.right;
}
if (leftCount == rightCount) {
return (2 << (leftCount - 1)) - 1;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
}

View File

@ -0,0 +1,44 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 13:51
* @注释
*/
public class LeetCode276 {
@Test
public void test() {
int[] nums = {0,0,0};
int res = new Solution().wiggleMaxLength(nums);
System.out.println(res);
}
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums.length < 2) {
return nums.length;
}
int preSum = 0;
int nowSum = 0;
int count = 1;
for (int i = 1; i < nums.length; i++) {
nowSum = nums[i] - nums[i - 1];
if ((nowSum > 0 && preSum <= 0) || (nowSum < 0 && preSum >= 0)) {
// 方向相反
count++;
preSum = nowSum;
} else {
// 方向相同
}
}
return count;
}
}
}

View File

@ -0,0 +1,67 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 12:14
* @注释
*/
public class LeetCode40 {
@Test
public void test() {
int[] candidates = {10,1,2,7,6,1,5};
int target = 8;
Solution solution = new Solution();
List<List<Integer>> lists = solution.combinationSum2(candidates, target);
System.out.println(lists);
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
int nowSum = 0;
boolean[] used;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
used = new boolean[candidates.length];
Arrays.sort(candidates);
backTracking(candidates, 0, target);
return res;
}
public void backTracking(int[] candidates, int start, int target) {
if (target == nowSum) {
res.add(new ArrayList<>(path));
return;
}
if (start > candidates.length) {
return;
}
for (int i = start; i < candidates.length; i++) {
if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
continue;
}
if (nowSum > target) {
return;
}
used[i] = true;
path.add(candidates[i]);
nowSum += candidates[i];
backTracking(candidates, i + 1, target);
nowSum -= candidates[i];
path.remove(path.size() - 1);
used[i] = false;
}
}
}
}

View File

@ -0,0 +1,85 @@
package cn.whaifree.redo.redo_all_240924;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/24 11:40
* @注释
*/
public class LeetCode94 {
@Test
public void test() {
TreeNode treeNode = TreeNode.constructTreeByArray(new Integer[]{2, 3, 6, 8, 9});
System.out.println(new Solution().inorderTraversal(treeNode));
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> list = new ArrayList<>();
Deque<TreeNode> deque = new LinkedList<>();
deque.push(root);
while (!deque.isEmpty()) {
TreeNode pop = deque.pop();
if (pop != null) {
if (pop.left != null) {
deque.push(pop.left);
}
if (pop.right != null) {
deque.push(pop.right);
}
deque.push(pop);
deque.push(null);
}else {
list.add(deque.pop().val);
}
}
Collections.reverse(list);
return list;
}
}
@Test
public void test2() {
TreeNode treeNode = TreeNode.constructTreeByArray(new Integer[]{2, 3, 6, 8, 9});
System.out.println(Level.levelOrder(treeNode));
}
class Level{
public static List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Deque<TreeNode> deque = new LinkedList<>();
deque.add(root);
while (!deque.isEmpty()) {
int size = deque.size();
ArrayList<Integer> e = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode pop = deque.pop();
if (pop.left != null) {
deque.add(pop.left);
}
if (pop.right != null) {
deque.add(pop.right);
}
e.add(pop.val);
}
res.add(e);
}
return res;
}
}
}