feat: 2025-10-28打卡
This commit is contained in:
47
162.find-peak-element.java
Normal file
47
162.find-peak-element.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=162 lang=java
|
||||||
|
*
|
||||||
|
* [162] Find Peak Element
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int findPeakElement(int[] nums) {
|
||||||
|
int l = 0, r = nums.length-1, m;
|
||||||
|
while(l <= r) {
|
||||||
|
m = l + (r-l)/2;
|
||||||
|
if(isPeak(m, nums)) {
|
||||||
|
return m;
|
||||||
|
} else if(isIncreasing(m, nums)) {
|
||||||
|
l = m + 1;
|
||||||
|
} else {
|
||||||
|
r = m - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RuntimeException("input is not valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isPeak(int i, int[] nums) {
|
||||||
|
boolean left = false, right = false;
|
||||||
|
if(i == 0) {
|
||||||
|
left = true;
|
||||||
|
} else {
|
||||||
|
left = nums[i] > nums[i-1];
|
||||||
|
}
|
||||||
|
if(i == nums.length-1) {
|
||||||
|
right = true;
|
||||||
|
} else {
|
||||||
|
right = nums[i] > nums[i+1];
|
||||||
|
}
|
||||||
|
return left && right;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isIncreasing(int i, int[] nums) {
|
||||||
|
if(isPeak(i, nums)) {
|
||||||
|
throw new RuntimeException("unexpected input");
|
||||||
|
}
|
||||||
|
return i>0?nums[i]-nums[i-1]>0:nums[i+1]-nums[i]>0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
24
167.two-sum-ii-input-array-is-sorted.java
Normal file
24
167.two-sum-ii-input-array-is-sorted.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=167 lang=java
|
||||||
|
*
|
||||||
|
* [167] Two Sum II - Input Array Is Sorted
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int[] twoSum(int[] numbers, int target) {
|
||||||
|
int i = 0, j = numbers.length-1;
|
||||||
|
while(i<j) {
|
||||||
|
if(numbers[i] + numbers[j] == target) {
|
||||||
|
return new int[] {i+1, j+1};
|
||||||
|
} else if(numbers[i] + numbers[j] < target) {
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new int[]{0, 0};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
30
209.minimum-size-subarray-sum.java
Normal file
30
209.minimum-size-subarray-sum.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=209 lang=java
|
||||||
|
*
|
||||||
|
* [209] Minimum Size Subarray Sum
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int minSubArrayLen(int target, int[] nums) {
|
||||||
|
if(nums == null || nums.length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int sum = 0, i = 0, min = 0;
|
||||||
|
for(int j = 0; j<nums.length; j++) {
|
||||||
|
sum += nums[j];
|
||||||
|
while(sum >= target) {
|
||||||
|
if(min == 0) {
|
||||||
|
min = j - i + 1;
|
||||||
|
} else {
|
||||||
|
min = Math.min(j-i+1, min);
|
||||||
|
}
|
||||||
|
sum -= nums[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
58
230.kth-smallest-element-in-a-bst.java
Normal file
58
230.kth-smallest-element-in-a-bst.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=230 lang=java
|
||||||
|
*
|
||||||
|
* [230] Kth Smallest Element in a BST
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* public class TreeNode {
|
||||||
|
* int val;
|
||||||
|
* TreeNode left;
|
||||||
|
* TreeNode right;
|
||||||
|
* TreeNode() {}
|
||||||
|
* TreeNode(int val) { this.val = val; }
|
||||||
|
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||||
|
* this.val = val;
|
||||||
|
* this.left = left;
|
||||||
|
* this.right = right;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
static class State{
|
||||||
|
int result;
|
||||||
|
int order;
|
||||||
|
int targetOrder;
|
||||||
|
boolean found;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int kthSmallest(TreeNode root, int k) {
|
||||||
|
State st = new State();
|
||||||
|
st.found = false;
|
||||||
|
st.targetOrder = k;
|
||||||
|
st.order = 0;
|
||||||
|
travel(root, st);
|
||||||
|
return st.result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void travel(TreeNode node, State st) {
|
||||||
|
if(node == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
travel(node.left, st);
|
||||||
|
st.order++;
|
||||||
|
if(st.found) {
|
||||||
|
return;
|
||||||
|
} else if(st.order == st.targetOrder) {
|
||||||
|
st.result = node.val;
|
||||||
|
st.found = true;
|
||||||
|
} else {
|
||||||
|
travel(node.right, st);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
26
278.first-bad-version.java
Normal file
26
278.first-bad-version.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=278 lang=java
|
||||||
|
*
|
||||||
|
* [278] First Bad Version
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
/* The isBadVersion API is defined in the parent class VersionControl.
|
||||||
|
boolean isBadVersion(int version); */
|
||||||
|
|
||||||
|
public class Solution extends VersionControl {
|
||||||
|
public int firstBadVersion(int n) {
|
||||||
|
int l = 1, r = n, m;
|
||||||
|
while(l < r) {
|
||||||
|
m = l + (r-l)/2;
|
||||||
|
if(isBadVersion(m)) {
|
||||||
|
r = m;
|
||||||
|
} else {
|
||||||
|
l = m + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
44
29.divide-two-integers.java
Normal file
44
29.divide-two-integers.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=29 lang=java
|
||||||
|
*
|
||||||
|
* [29] Divide Two Integers
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int divide(int dividend, int divisor) {
|
||||||
|
long x = Math.abs((long)dividend),
|
||||||
|
y = Math.abs((long)divisor);
|
||||||
|
long r = 0;
|
||||||
|
while(true) {
|
||||||
|
long[] t = floor(x, y);
|
||||||
|
if(t[0] == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
x -= t[1];
|
||||||
|
r += t[0];
|
||||||
|
}
|
||||||
|
int sign = (dividend>=0?1:-1) * (divisor>=0?1:-1);
|
||||||
|
r = r * sign;
|
||||||
|
return (int)Math.max(Math.min(r, Integer.MAX_VALUE), Integer.MIN_VALUE);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private long[] floor(long x, long y) {
|
||||||
|
if(x <= y) {
|
||||||
|
return x == y ? new long[]{1, y}:new long[]{0, 0};
|
||||||
|
}
|
||||||
|
long s = y;
|
||||||
|
long r = 1;
|
||||||
|
while(true) {
|
||||||
|
if(s + s > x) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
s = s + s;
|
||||||
|
r = r + r;
|
||||||
|
}
|
||||||
|
return new long[] {r, s};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
63
33.search-in-rotated-sorted-array.java
Normal file
63
33.search-in-rotated-sorted-array.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=33 lang=java
|
||||||
|
*
|
||||||
|
* [33] Search in Rotated Sorted Array
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int search(int[] nums, int target) {
|
||||||
|
if(nums == null ||nums.length == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int r;
|
||||||
|
if(target >= nums[0]) {
|
||||||
|
// find in ascending side
|
||||||
|
r = searchInAscendingSide(nums, target);
|
||||||
|
} else {
|
||||||
|
// find in descending side
|
||||||
|
r = searchInDescendingSide(nums, target);
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int searchInAscendingSide(int[] nums, int target) {
|
||||||
|
int l = 0, r = nums.length-1, m;
|
||||||
|
while(l<=r) {
|
||||||
|
m = l + (r-l)/2;
|
||||||
|
if(nums[m] == target) {
|
||||||
|
return m;
|
||||||
|
} else if(!isAscendingSide(m, nums)) {
|
||||||
|
r = m -1;
|
||||||
|
} else if(nums[m] < target) {
|
||||||
|
l = m + 1;
|
||||||
|
} else {
|
||||||
|
r = m -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int searchInDescendingSide(int[] nums, int target) {
|
||||||
|
int l = 0, r = nums.length-1, m;
|
||||||
|
while(l <= r) {
|
||||||
|
m = l + (r-l)/2;
|
||||||
|
if(nums[m] == target) {
|
||||||
|
return m;
|
||||||
|
} else if(isAscendingSide(m, nums)) {
|
||||||
|
l = m + 1;
|
||||||
|
} else if(nums[m] < target) {
|
||||||
|
l = m + 1;
|
||||||
|
} else {
|
||||||
|
r = m - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAscendingSide(int i, int[] nums) {
|
||||||
|
return nums[i]>=nums[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=34 lang=java
|
||||||
|
*
|
||||||
|
* [34] Find First and Last Position of Element in Sorted Array
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int[] searchRange(int[] nums, int target) {
|
||||||
|
if(nums==null || nums.length == 0) {
|
||||||
|
return new int[]{-1, -1};
|
||||||
|
}
|
||||||
|
return new int[] {
|
||||||
|
firstEq(nums, target),
|
||||||
|
lastEq(nums, target)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private int firstEq(int[] nums, int target) {
|
||||||
|
int l = 0, r = nums.length-1, m, v=-1;
|
||||||
|
while(l <= r) {
|
||||||
|
m = l + (r-l)/2;
|
||||||
|
if(nums[m] < target) {
|
||||||
|
l = m + 1;
|
||||||
|
} else if(nums[m] > target) {
|
||||||
|
r = m - 1;
|
||||||
|
} else {
|
||||||
|
v = m;
|
||||||
|
r = m-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int lastEq(int[] nums, int target) {
|
||||||
|
int l = 0, r = nums.length-1, m, v=-1;
|
||||||
|
while(l<=r) {
|
||||||
|
m = l + (r-l)/2;
|
||||||
|
if(nums[m] > target) {
|
||||||
|
r = m - 1;
|
||||||
|
} else if(nums[m] < target) {
|
||||||
|
l = m + 1;
|
||||||
|
} else {
|
||||||
|
v = m;
|
||||||
|
l = m + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
46
349.intersection-of-two-arrays.java
Normal file
46
349.intersection-of-two-arrays.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=349 lang=java
|
||||||
|
*
|
||||||
|
* [349] Intersection of Two Arrays
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int[] intersection(int[] nums1, int[] nums2) {
|
||||||
|
Arrays.sort(nums1);
|
||||||
|
Arrays.sort(nums2);
|
||||||
|
List<Integer> r = new ArrayList<>();
|
||||||
|
for(int i=0; i<nums1.length; i++) {
|
||||||
|
if(i>0 && nums1[i]==nums1[i-1]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(binarySearch(nums1[i], nums2)!=-1) {
|
||||||
|
r.add(nums1[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.stream().mapToInt(i->i).toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int binarySearch(int target, int[] nums) {
|
||||||
|
if(nums == null || nums.length == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int l = 0, r = nums.length-1, m;
|
||||||
|
while(l <= r) {
|
||||||
|
m = l + (r-l)/2;
|
||||||
|
if(nums[m] == target) {
|
||||||
|
return m;
|
||||||
|
} else if(nums[m] > target) {
|
||||||
|
r = m - 1;
|
||||||
|
} else {
|
||||||
|
l = m + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
32
350.intersection-of-two-arrays-ii.java
Normal file
32
350.intersection-of-two-arrays-ii.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=350 lang=java
|
||||||
|
*
|
||||||
|
* [350] Intersection of Two Arrays II
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int[] intersect(int[] nums1, int[] nums2) {
|
||||||
|
Arrays.sort(nums1);
|
||||||
|
Arrays.sort(nums2);
|
||||||
|
int i = 0, j = 0;
|
||||||
|
List<Integer> ret = new ArrayList<>();
|
||||||
|
while(i<nums1.length && j < nums2.length) {
|
||||||
|
if(nums1[i] == nums2[j]) {
|
||||||
|
ret.add(nums1[i]);
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
} else if(nums1[i] < nums2[j]) {
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret.stream().mapToInt(f->f).toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
25
367.valid-perfect-square.java
Normal file
25
367.valid-perfect-square.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=367 lang=java
|
||||||
|
*
|
||||||
|
* [367] Valid Perfect Square
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public boolean isPerfectSquare(int num) {
|
||||||
|
long i = 0, j = num, m;
|
||||||
|
while(i <= j) {
|
||||||
|
m = i + (j-i)/2;
|
||||||
|
if(m * m == num) {
|
||||||
|
return true;
|
||||||
|
} else if(m * m <num) {
|
||||||
|
i = m + 1;
|
||||||
|
} else {
|
||||||
|
j = m - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
33
50.pow-x-n.java
Normal file
33
50.pow-x-n.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=50 lang=java
|
||||||
|
*
|
||||||
|
* [50] Pow(x, n)
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public double myPow(double x, int n) {
|
||||||
|
Map<Long, Double> track = new HashMap<>();
|
||||||
|
long m = n;
|
||||||
|
return m>=0?myPow(x, m, track):1/myPow(x, -m, track);
|
||||||
|
}
|
||||||
|
|
||||||
|
private double myPow(double x, long n, Map<Long, Double> track) {
|
||||||
|
if(n < 0) {
|
||||||
|
throw new RuntimeException("not supported number");
|
||||||
|
} else if(n <= 1) {
|
||||||
|
return n==0?1:x;
|
||||||
|
} else if(track.containsKey(n)) {
|
||||||
|
return track.get(n);
|
||||||
|
}
|
||||||
|
long l = n / 2;
|
||||||
|
double r = myPow(x, l, track) * myPow(x, n-l, track);
|
||||||
|
track.put(n, r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
@@ -7,13 +7,13 @@
|
|||||||
// @lc code=start
|
// @lc code=start
|
||||||
class Solution {
|
class Solution {
|
||||||
public int mySqrt(int x) {
|
public int mySqrt(int x) {
|
||||||
long l = 0, r = Integer.MAX_VALUE;
|
long l = 0, r = x+(long)1, m;
|
||||||
while(r-l > 1) {
|
while(r - l > 1) {
|
||||||
long mid = l + (r-l)/2;
|
m = l + (r-l)/2;
|
||||||
if(mid * mid <= (long)x) {
|
if(m*m <= x) {
|
||||||
l = mid;
|
l = m;
|
||||||
} else {
|
} else {
|
||||||
r = mid;
|
r = m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (int)l;
|
return (int)l;
|
||||||
|
|||||||
Reference in New Issue
Block a user