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

Commite6f5c15

Browse files
authored
Create 0992-subarrays-with-k-different-integers.kt
1 parentd642910 commite6f5c15

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
classSolution {
2+
funsubarraysWithKDistinct(nums:IntArray,k:Int):Int {
3+
val count=HashMap<Int,Int>()
4+
var res=0
5+
6+
var l=0
7+
var m=0
8+
for (rin0 until nums.size) {
9+
count[nums[r]]= (count[nums[r]]?:0)+1
10+
11+
while (count.size> k) {
12+
count[nums[m]]= (count[nums[m]]?:0)-1
13+
if ((count[nums[m]]?:0)==0) count.remove(nums[m])
14+
m++
15+
l= m
16+
}
17+
18+
while ((count[nums[m]]?:0)>1) {
19+
count[nums[m]]= (count[nums[m]]?:0)-1
20+
m++
21+
}
22+
23+
if (count.size== k) res+= (m- l+1)
24+
}
25+
26+
return res
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp