From f06095ad7ef017df702e213287c545e818395bf4 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Thu, 23 Oct 2025 16:32:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202025-10-23=E6=89=93=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1034.coloring-a-border.java | 68 +++++++++++++++++ 1037.valid-boomerang.java | 25 +++++++ 1046.last-stone-weight.java | 24 ++++++ 1138.alphabet-board-path.java | 75 +++++++++++++++++++ 142.linked-list-cycle-ii.java | 49 ++++++++++++ ...ubstring-without-repeating-characters.java | 39 ++++++++++ 392.is-subsequence.java | 20 +++++ 455.assign-cookies.java | 26 +++++++ 567.permutation-in-string.java | 52 +++++++++++++ 9 files changed, 378 insertions(+) create mode 100644 1034.coloring-a-border.java create mode 100644 1037.valid-boomerang.java create mode 100644 1046.last-stone-weight.java create mode 100644 1138.alphabet-board-path.java create mode 100644 142.linked-list-cycle-ii.java create mode 100644 3.longest-substring-without-repeating-characters.java create mode 100644 392.is-subsequence.java create mode 100644 455.assign-cookies.java create mode 100644 567.permutation-in-string.java diff --git a/1034.coloring-a-border.java b/1034.coloring-a-border.java new file mode 100644 index 0000000..55d471e --- /dev/null +++ b/1034.coloring-a-border.java @@ -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 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 + diff --git a/1138.alphabet-board-path.java b/1138.alphabet-board-path.java new file mode 100644 index 0000000..571c8a0 --- /dev/null +++ b/1138.alphabet-board-path.java @@ -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 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 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 + diff --git a/3.longest-substring-without-repeating-characters.java b/3.longest-substring-without-repeating-characters.java new file mode 100644 index 0000000..10cc8c6 --- /dev/null +++ b/3.longest-substring-without-repeating-characters.java @@ -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 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 + diff --git a/392.is-subsequence.java b/392.is-subsequence.java new file mode 100644 index 0000000..6757408 --- /dev/null +++ b/392.is-subsequence.java @@ -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 + diff --git a/455.assign-cookies.java b/455.assign-cookies.java new file mode 100644 index 0000000..ec2afb7 --- /dev/null +++ b/455.assign-cookies.java @@ -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 + diff --git a/567.permutation-in-string.java b/567.permutation-in-string.java new file mode 100644 index 0000000..d9d09a5 --- /dev/null +++ b/567.permutation-in-string.java @@ -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