Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9bb322

Browse files
author
night
committed
clean the code
1 parentb22f1c1 commitc9bb322

File tree

13 files changed

+51
-56
lines changed

13 files changed

+51
-56
lines changed

‎build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11

22
plugins {
3-
id'org.jetbrains.kotlin.jvm' version'1.9.23'
3+
id'org.jetbrains.kotlin.jvm' version'2.1.20'
44
}
55

66
repositories {
77
mavenCentral()
88
mavenLocal()
9-
maven { url"https://jitpack.io" }
109
maven { url'https://repository.mulesoft.org/nexus/content/repositories/public/' }
1110
}
1211

‎gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

‎note/056/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Explanation: Intervals [1,4] and [4,5] are considerred overlapping.
3939
*/
4040
classSolution {
4141
publicList<Interval>merge(List<Interval>intervals) {
42+
ArrayUtils
4243
if (intervals==null|| intervals.size()<=1)return intervals;
4344
intervals.sort(newComparator<Interval>() {
4445
@Override

‎src/main/java/com/blankj/coding_interviews/_007/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import com.blankj.structure.TreeNode
55

66
classSolution {
77

8-
var preOrder:IntArray= intArrayOf()
9-
var inorderMap=hashMapOf<Int,Int>()
8+
privatevar preOrder:IntArray= intArrayOf()
9+
privatevar inorderMap=mutableMapOf<Int,Int>()
1010

1111
funbuildTree(preOrder:IntArray,inorder:IntArray):TreeNode? {
1212
if (preOrder.isEmpty()|| inorder.isEmpty())returnnull
@@ -21,7 +21,7 @@ class Solution {
2121
endPreorder:Int,
2222
startInorder:Int,
2323
endInorder:Int
24-
):TreeNode? {
24+
):TreeNode {
2525
val root=TreeNode(preOrder[startPreorder])
2626
val rootInorder= inorderMap[preOrder[startPreorder]]// root index in inorder array
2727
?:throwIllegalArgumentException("Could not find out preorder number${preOrder[startPreorder]} in inorder")

‎src/main/java/com/blankj/coding_interviews/_009/Solution.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ class CStack {
5353
}
5454

5555
/** Returns whether the stack is empty.*/
56-
funempty():Boolean {
57-
return queue.isEmpty()
58-
}
56+
funempty():Boolean= queue.isEmpty()
5957
}
6058

6159
funmain() {

‎src/main/java/com/blankj/coding_interviews/_010/Solution.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class Solution {
66
funfib(n:Int):Int {
77
val fib= intArrayOf(0,1)
88
if (nin0..1)return fib[n]
9-
varfibOne=1
10-
varfibTwo=0
11-
var fibN=fibTwo
9+
varfibMinusOne=1
10+
varfibMinusTwo=0
11+
var fibN=fibMinusTwo
1212
for (iin2..n) {
13-
fibN= (fibOne+fibTwo)%1000000007
14-
fibTwo=fibOne
15-
fibOne= fibN
13+
fibN= (fibMinusOne+fibMinusTwo)%1000000007
14+
fibMinusTwo=fibMinusOne
15+
fibMinusOne= fibN
1616
}
1717
return fibN
1818
}

‎src/main/java/com/blankj/duolingo/CountingMistakes.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@ class CountingMistakes {
1717
for (iin1..<submissions.size) {
1818
map.merge(submissions[i][index],1,Integer::sum)
1919
}
20-
var correctWordCount=Int.MIN_VALUE
21-
var mostCommonMistakes=Int.MIN_VALUE
22-
for (countin map.values) {
23-
if (count> correctWordCount) {
24-
mostCommonMistakes= correctWordCount
25-
correctWordCount= count
26-
}elseif (count> mostCommonMistakes&& count!= correctWordCount) {
27-
mostCommonMistakes= count
28-
}
29-
}
20+
var mostCommonMistakes= getMostCommonMistakes(map)
3021
if (mostCommonMistakes==Int.MIN_VALUE)continue
3122
//val mostCommonMistakes = sortedList.lastOrNull { it.value < sortedList.last().value } ?: continue
3223
res.addAll(map.filter { it.value== mostCommonMistakes }.entries)
3324
}
3425
res.sortWith(compareBy({-it.value }, { it.key }))
3526
return res.map { it.key }
3627
}
28+
29+
privatefungetMostCommonMistakes(map:MutableMap<String,Int>):Int {
30+
var correctWordCount=Int.MIN_VALUE
31+
var mostCommonMistakes=Int.MIN_VALUE
32+
for (countin map.values) {
33+
if (count> correctWordCount) {
34+
mostCommonMistakes= correctWordCount
35+
correctWordCount= count
36+
}elseif (count> mostCommonMistakes&& count!= correctWordCount) {
37+
mostCommonMistakes= count
38+
}
39+
}
40+
return mostCommonMistakes
41+
}
3742
}
3843

3944
funmain() {

‎src/main/java/com/blankj/duolingo/MoveInBoard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.blankj.duolingo
33
importcom.blankj.ext.print
44

55
/**
6-
* Time Complexity: O(4^n* n)
6+
* Time Complexity: O(4^n+ n)
77
* Space Complexity: O(4^n)
88
*
99
* Here, n represents the size of the board.

‎src/main/java/com/blankj/hard/_126/Solution.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.blankj.ext.print
44
importjava.util.*
55
importkotlin.collections.HashMap
66
importkotlin.collections.HashSet
7-
importkotlin.math.max
87

98
/**
109
* 假设 beginword 和 endword 之间的距离是 d。每个节点可以扩展出 k 个节点。
@@ -20,19 +19,19 @@ class Solution {
2019
val dict=HashSet(wordList)//未访问过的节点集合
2120
val res:MutableList<List<String>>=mutableListOf()
2221
if (!dict.contains(endWord))return res
23-
val begin=HashSet<String>()
22+
val begin=mutableSetOf<String>()
2423
begin.add(beginWord)//从上之下集合
25-
val end=HashSet<String>()
24+
val end=mutableSetOf<String>()
2625
end.add(endWord)//从下至上集合
27-
val map=HashMap<String,MutableList<String>>()//存储每个节点的邻居节点
26+
val map=mutableMapOf<String,MutableList<String>>()//存储每个节点的邻居节点
2827
if (doubleBfs(dict, begin, end, map,true)) {
2928
dfs(map, res, beginWord, endWord,ArrayDeque())
3029
}
3130
return res
3231
}
3332

3433
privatefundfs(
35-
map:HashMap<String,MutableList<String>>,
34+
map:MutableMap<String,MutableList<String>>,
3635
res:MutableList<List<String>>,
3736
beginWord:String,
3837
endWord:String,
@@ -56,10 +55,10 @@ class Solution {
5655
* 寻找每个节点的邻居节点
5756
*/
5857
privatefundoubleBfs(
59-
dict:HashSet<String>,
58+
dict:MutableSet<String>,
6059
begin:Set<String>,
6160
end:Set<String>,
62-
map:HashMap<String,MutableList<String>>,
61+
map:MutableMap<String,MutableList<String>>,
6362
isTopDown:Boolean
6463
):Boolean {
6564
//当一个方向节点数为空仍未找到 中间联通节点时,返回 false
@@ -70,7 +69,7 @@ class Solution {
7069
dict.removeAll(begin)
7170
dict.removeAll(end)//去除已访问节点
7271
var isTraversalEnd=false//是否遍历结束
73-
valvisited=HashSet<String>()//记录本层新增节点(未在 begin 和 end 中出现过)
72+
valvalidNeighbors=mutableSetOf<String>()//记录本层新增节点(未在 begin 和 end 中出现过)
7473
for (wordin begin) {//遍历begin中每个单词
7574
val chars= word.toCharArray()
7675
for (iin chars.indices) {
@@ -90,17 +89,17 @@ class Solution {
9089
neighborWords.add(value)
9190
}
9291
//如果找到了目标节点或者dict 中不包含该邻居节点,跳过本次循环
93-
if (isTraversalEnd||!dict.contains(neighborWord)) {
92+
if (isTraversalEnd||neighborWord!indict) {
9493
continue
9594
}
96-
visited.add(neighborWord)// 记录本层访问节点
95+
validNeighbors.add(neighborWord)// 记录本层访问节点
9796
neighborWords.add(value)
9897
}
9998
chars[i]= temp
10099
}
101100
}
102101
//使用短路或进行判断是否找到公共节点,找到就直接返回,否则继续以本层访问节点作为下层的 begin 继续深搜
103-
return isTraversalEnd|| doubleBfs(dict,visited, end, map, isTopDown)
102+
return isTraversalEnd|| doubleBfs(dict,validNeighbors, end, map, isTopDown)
104103
}
105104
}
106105

‎src/main/java/com/blankj/hard/_30/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Solution {
2424
curMap[prev]= curMap.getValue(prev)-1
2525
}
2626
}
27-
curMap[cur]= curMap.getOrDefault(cur,0)+1
27+
curMap.merge(cur,1,Integer::sum)
2828
if (curin count&& curMap[cur]== count[cur]&& curMap== count) {
2929
res.add(j- (m-1)* w)
3030
}

‎src/main/java/com/blankj/hard/_76/Solution.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@ class Solution {
99

1010
val tFreq=IntArray(128)
1111
val windowFreq=IntArray(128)
12-
t.forEach {
13-
tFreq[it.code]++
14-
}
12+
t.forEach { tFreq[it.code]++ }
1513
var left=0
1614
var right=0
1715
var count=0
1816
var minLen=Int.MAX_VALUE
1917
var start=0
2018
while (right< s.length) {
2119
val c= s[right]
22-
if (tFreq[c.code]==0) {
23-
right++
24-
continue
25-
}
26-
if (windowFreq[c.code]< tFreq[c.code] ) {
20+
// 移除掉考虑 tFreq[c.code] == 0 再 continue 的特殊处理, 使逻辑更加简洁
21+
if (windowFreq[c.code]< tFreq[c.code]) {
2722
count++
2823
}
2924
windowFreq[c.code]++
@@ -34,12 +29,8 @@ class Solution {
3429
start= left
3530
}
3631
val leftChar= s[left]
37-
if (tFreq[leftChar.code]==0) {
38-
windowFreq[leftChar.code]--
39-
left++
40-
continue
41-
}
42-
if (tFreq[leftChar.code]== windowFreq[leftChar.code]){
32+
// 移除掉考虑 tFreq[c.code] == 0 再 continue 的特殊处理, 使逻辑更加一致
33+
if (tFreq[leftChar.code]== windowFreq[leftChar.code]) {
4334
count--
4435
}
4536
windowFreq[leftChar.code]--
@@ -55,5 +46,8 @@ fun main() {
5546
Solution().minWindow("","aa")?.print()
5647
Solution().minWindow(null,"aa")?.print()
5748
Solution().minWindow("ADOBECODEBANC","ABC")?.print()
58-
Solution().minWindow("wegdtzwabazduwwdysdetrrctotpcepalxdewzezbfewbabbseinxbqqplitpxtcwwhuyntbtzxwzyaufihclztckdwccpeyonumbpnuonsnnsjscrvpsqsftohvfnvtbphcgxyumqjzltspmphefzjypsvugqqjhzlnylhkdqmolggxvneaopadivzqnpzurmhpxqcaiqruwztroxtcnvhxqgndyozpcigzykbiaucyvwrjvknifufxducbkbsmlanllpunlyohwfsssiazeixhebipfcdqdrcqiwftutcrbxjthlulvttcvdtaiwqlnsdvqkrngvghupcbcwnaqiclnvnvtfihylcqwvderjllannflchdklqxidvbjdijrnbpkftbqgpttcagghkqucpcgmfrqqajdbynitrbzgwukyaqhmibpzfxmkoeaqnftnvegohfudbgbbyiqglhhqevcszdkokdbhjjvqqrvrxyvvgldtuljygmsircydhalrlgjeyfvxdstmfyhzjrxsfpcytabdcmwqvhuvmpssingpmnpvgmpletjzunewbamwiirwymqizwxlmojsbaehupiocnmenbcxjwujimthjtvvhenkettylcoppdveeycpuybekulvpgqzmgjrbdrmficwlxarxegrejvrejmvrfuenexojqdqyfmjeoacvjvzsrqycfuvmozzuypfpsvnzjxeazgvibubunzyuvugmvhguyojrlysvxwxxesfioiebidxdzfpumyon","ozgzyywxvtublcl")?.print()
49+
Solution().minWindow(
50+
"wegdtzwabazduwwdysdetrrctotpcepalxdewzezbfewbabbseinxbqqplitpxtcwwhuyntbtzxwzyaufihclztckdwccpeyonumbpnuonsnnsjscrvpsqsftohvfnvtbphcgxyumqjzltspmphefzjypsvugqqjhzlnylhkdqmolggxvneaopadivzqnpzurmhpxqcaiqruwztroxtcnvhxqgndyozpcigzykbiaucyvwrjvknifufxducbkbsmlanllpunlyohwfsssiazeixhebipfcdqdrcqiwftutcrbxjthlulvttcvdtaiwqlnsdvqkrngvghupcbcwnaqiclnvnvtfihylcqwvderjllannflchdklqxidvbjdijrnbpkftbqgpttcagghkqucpcgmfrqqajdbynitrbzgwukyaqhmibpzfxmkoeaqnftnvegohfudbgbbyiqglhhqevcszdkokdbhjjvqqrvrxyvvgldtuljygmsircydhalrlgjeyfvxdstmfyhzjrxsfpcytabdcmwqvhuvmpssingpmnpvgmpletjzunewbamwiirwymqizwxlmojsbaehupiocnmenbcxjwujimthjtvvhenkettylcoppdveeycpuybekulvpgqzmgjrbdrmficwlxarxegrejvrejmvrfuenexojqdqyfmjeoacvjvzsrqycfuvmozzuypfpsvnzjxeazgvibubunzyuvugmvhguyojrlysvxwxxesfioiebidxdzfpumyon",
51+
"ozgzyywxvtublcl"
52+
)?.print()
5953
}

‎src/main/java/com/blankj/medium/_016/Solution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public int threeSumClosest(int[] nums, int target) {
2727
}
2828
if (sum >target) --right;
2929
else ++left;
30+
3031
}
3132
}
3233
returnres;

‎src/main/java/com/blankj/medium/_47/Solution.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ class Solution {
3131
}
3232

3333
privatefunisSwapped(nums:IntArray,start:Int,end:Int):Boolean {
34-
val range= start..<end
35-
return nums.indices.filter { itin range }
36-
.any { nums[it]== nums[end] }
34+
return (start..<end).any { nums[it]== nums[end] }
3735
}
3836
}
3937

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp