feat: 2025-10-23打卡
This commit is contained in:
68
1034.coloring-a-border.java
Normal file
68
1034.coloring-a-border.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1034 lang=java
|
||||
*
|
||||
* [1034] Coloring A Border
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int[][] colorBorder(int[][] grid, int row, int col, int color) {
|
||||
int[][] track = new int[grid.length][grid[0].length];
|
||||
clearState(track);
|
||||
int[][] cgrid = copyGrid(grid);
|
||||
colorBorder(cgrid, grid, track, row, col, color, grid[row][col]);
|
||||
return cgrid;
|
||||
}
|
||||
|
||||
private void colorBorder(int[][] cgrid, int[][] grid, int[][] track, int i, int j, int borderColor, int componentColor) {
|
||||
if(track[i][j] != 0 || grid[i][j] != componentColor) {
|
||||
return;
|
||||
}
|
||||
if(isBorder(grid, i, j, componentColor)) {
|
||||
cgrid[i][j] = borderColor;
|
||||
}
|
||||
track[i][j] = 1;
|
||||
if(i+1 < grid.length && track[i+1][j] == 0) {
|
||||
colorBorder(cgrid, grid, track, i+1, j, borderColor, componentColor);
|
||||
}
|
||||
if(i-1 >= 0 && track[i-1][j] == 0) {
|
||||
colorBorder(cgrid, grid, track, i-1, j, borderColor, componentColor);
|
||||
}
|
||||
if(j+1 < grid[0].length && track[i][j+1] == 0) {
|
||||
colorBorder(cgrid, grid, track, i, j+1, borderColor, componentColor);
|
||||
}
|
||||
if(j-1 >= 0 && track[i][j-1] == 0) {
|
||||
colorBorder(cgrid, grid, track, i, j-1, borderColor, componentColor);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isBorder(int[][] grid, int i, int j, int componentColor) {
|
||||
if(i == 0 || i == grid.length-1 || j == 0 || j == grid[0].length - 1) {
|
||||
return true;
|
||||
}
|
||||
if(i + 1 < grid.length && grid[i+1][j] != componentColor || i-1 >= 0 && grid[i-1][j] != componentColor || j+1 < grid[0].length && grid[i][j+1] != componentColor || j-1 >=0 && grid[i][j-1] != componentColor) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void clearState(int[][] g) {
|
||||
for(int i = 0; i < g.length; i++) {
|
||||
for (int j = 0; j < g[i].length; j++) {
|
||||
g[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int[][] copyGrid(int[][] grid) {
|
||||
int[][] copy = new int[grid.length][grid[0].length];
|
||||
for(int i = 0; i<grid.length; i++) {
|
||||
for(int j = 0; j<grid[0].length; j++) {
|
||||
copy[i][j] = grid[i][j];
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
25
1037.valid-boomerang.java
Normal file
25
1037.valid-boomerang.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1037 lang=java
|
||||
*
|
||||
* [1037] Valid Boomerang
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
/**
|
||||
* y1 / x1 = y0 / x0
|
||||
* x0*y1 = x1*y0
|
||||
*
|
||||
* @param points
|
||||
* @return
|
||||
*/
|
||||
public boolean isBoomerang(int[][] points) {
|
||||
int x0 = points[1][0] - points[0][0];
|
||||
int y0 = points[1][1] - points[0][1];
|
||||
int x1 = points[2][0] - points[0][0];
|
||||
int y1 = points[2][1] - points[0][1];
|
||||
return x0 * y1 != x1 * y0;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
24
1046.last-stone-weight.java
Normal file
24
1046.last-stone-weight.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1046 lang=java
|
||||
*
|
||||
* [1046] Last Stone Weight
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int lastStoneWeight(int[] stones) {
|
||||
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b)->b-a);
|
||||
for(int i = 0; i < stones.length; i++) {
|
||||
pq.add(stones[i]);
|
||||
}
|
||||
int a, b;
|
||||
while(pq.size() > 1) {
|
||||
a = pq.poll();
|
||||
b = pq.poll();
|
||||
pq.add(a < b?b-a:a-b);
|
||||
}
|
||||
return pq.peek();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
75
1138.alphabet-board-path.java
Normal file
75
1138.alphabet-board-path.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1138 lang=java
|
||||
*
|
||||
* [1138] Alphabet Board Path
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public String alphabetBoardPath(String target) {
|
||||
char cur = 'a';
|
||||
StringBuilder bd = new StringBuilder();
|
||||
for(int i = 0; i < target.length(); i++) {
|
||||
bd.append(actions(cur, target.charAt(i)));
|
||||
cur = target.charAt(i);
|
||||
}
|
||||
return bd.toString();
|
||||
}
|
||||
|
||||
private int[] distance(char a, char b) {
|
||||
int[] pa = new int[] {
|
||||
(a-'a') / 5,
|
||||
(a-'a') % 5
|
||||
};
|
||||
int[] pb = new int[] {
|
||||
(b - 'a') / 5,
|
||||
(b - 'a') % 5
|
||||
};
|
||||
return new int[] {
|
||||
pb[0] - pa[0],
|
||||
pb[1] - pa[1]
|
||||
};
|
||||
}
|
||||
|
||||
private String actions(char s, char d) {
|
||||
int[] move = distance(s, d);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if(move[0] == 0 && move[1] == 0) {
|
||||
builder.append('!');
|
||||
} else {
|
||||
if(s == 'z') {
|
||||
builder.append(rowActions(move[0]));
|
||||
builder.append(colActions(move[1]));
|
||||
} else {
|
||||
builder.append(colActions(move[1]));
|
||||
builder.append(rowActions(move[0]));
|
||||
}
|
||||
builder.append('!');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String rowActions(int x) {
|
||||
if(x == 0) {
|
||||
return "";
|
||||
}
|
||||
return x > 0 ? repeatChar('D', x):repeatChar('U', -x);
|
||||
}
|
||||
|
||||
private String colActions(int x) {
|
||||
if(x == 0) {
|
||||
return "";
|
||||
}
|
||||
return x > 0 ? repeatChar('R', x):repeatChar('L', -x);
|
||||
}
|
||||
|
||||
private String repeatChar(char ch, int n) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
for(int i = 0; i<n; i++) {
|
||||
s.append(ch);
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
49
142.linked-list-cycle-ii.java
Normal file
49
142.linked-list-cycle-ii.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* @lc app=leetcode id=142 lang=java
|
||||
*
|
||||
* [142] Linked List Cycle II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
/**
|
||||
* 2 * k - s - (k - s) = p * r
|
||||
* 2 * k - s - k + s = p * r
|
||||
* k = p * r, 且 k > s
|
||||
*
|
||||
* s + p * r + m - 2m = q * r
|
||||
* s = q * r - p * r + m
|
||||
*
|
||||
* @param head
|
||||
* @return
|
||||
*/
|
||||
public ListNode detectCycle(ListNode head) {
|
||||
if(head == null) {
|
||||
return null;
|
||||
}
|
||||
Set<ListNode> tracked = new HashSet<>();
|
||||
ListNode cur = head, r = null;
|
||||
while(cur != null) {
|
||||
if(tracked.contains(cur)) {
|
||||
r = cur;
|
||||
break;
|
||||
}
|
||||
tracked.add(cur);
|
||||
cur = cur.next;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
39
3.longest-substring-without-repeating-characters.java
Normal file
39
3.longest-substring-without-repeating-characters.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* @lc app=leetcode id=3 lang=java
|
||||
*
|
||||
* [3] Longest Substring Without Repeating Characters
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
if(s.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
Set<Character> chs = new HashSet<>();
|
||||
int i = 0, j = 0, r = 0, cur = 0;
|
||||
while(j < s.length()) {
|
||||
if(!chs.contains(s.charAt(j))) {
|
||||
chs.add(s.charAt(j));
|
||||
cur++;
|
||||
if(cur > r) {
|
||||
r = cur;
|
||||
}
|
||||
} else {
|
||||
while(s.charAt(i) != s.charAt(j)) {
|
||||
chs.remove(s.charAt(i));
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
cur = j - i + 1;
|
||||
if(cur > r) {
|
||||
r = cur;
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
20
392.is-subsequence.java
Normal file
20
392.is-subsequence.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* @lc app=leetcode id=392 lang=java
|
||||
*
|
||||
* [392] Is Subsequence
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int i = 0;
|
||||
for(int j = 0; j < t.length(); j++) {
|
||||
if(i < s.length() && t.charAt(j) == s.charAt(i)) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return i == s.length();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
26
455.assign-cookies.java
Normal file
26
455.assign-cookies.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @lc app=leetcode id=455 lang=java
|
||||
*
|
||||
* [455] Assign Cookies
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int findContentChildren(int[] g, int[] s) {
|
||||
Arrays.sort(g);
|
||||
Arrays.sort(s);
|
||||
int i = g.length - 1, j = s.length - 1, r = 0;
|
||||
while(i>=0 && j>=0) {
|
||||
if(s[j] >= g[i]) {
|
||||
j--;
|
||||
i--;
|
||||
r++;
|
||||
} else {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
52
567.permutation-in-string.java
Normal file
52
567.permutation-in-string.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* @lc app=leetcode id=567 lang=java
|
||||
*
|
||||
* [567] Permutation in String
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean checkInclusion(String s1, String s2) {
|
||||
if(s1.length() > s2.length()) {
|
||||
return false;
|
||||
}
|
||||
int[] c1 = strCompose(s1);
|
||||
int[] c2 = strCompose(s2.substring(0, s1.length()));
|
||||
boolean f = isComposeSame(c1, c2);
|
||||
int j = s1.length();
|
||||
while(!f && j < s2.length()) {
|
||||
c2[s2.charAt(j - s1.length())-'a']--;
|
||||
c2[s2.charAt(j)-'a']++;
|
||||
if(isComposeSame(c1, c2)) {
|
||||
f = true;
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
private int[] strCompose(String s) {
|
||||
int[] c = new int[26];
|
||||
for(int i = 0; i < s.length(); i++) {
|
||||
c[s.charAt(i)-'a']++;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private boolean isComposeSame(int[] a, int[] b) {
|
||||
if(a.length != b.length) {
|
||||
return false;
|
||||
}
|
||||
boolean matches = true;
|
||||
for(int i=0; i<a.length; i++) {
|
||||
if(a[i] != b[i]) {
|
||||
matches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user