File tree Expand file tree Collapse file tree 3 files changed +26
-11
lines changed
count-number-of-bad-pairs Expand file tree Collapse file tree 3 files changed +26
-11
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/count-number-of-bad-pairs/
14
+
13
15
https://leetcode.cn/problems/number-of-good-pairs
14
16
15
17
https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string
Original file line number Diff line number Diff line change
1
+ function countBadPairs ( nums : number [ ] ) : number {
2
+ const n = nums . length ;
3
+ const cnt = new Map < number , number > ( ) ;
4
+ for ( const [ i , num ] of nums . entries ( ) ) {
5
+ cnt . set ( num - i , ( cnt . get ( num - i ) ?? 0 ) + 1 ) ;
6
+ }
7
+ const ans = Array . from ( cnt . values ( ) ) . reduce (
8
+ ( a , v ) => a + Math . floor ( ( v * ( v - 1 ) ) / 2 ) ,
9
+ 0 ,
10
+ ) ;
11
+ return Math . floor ( ( n * ( n - 1 ) ) / 2 ) - ans ;
12
+ }
13
+ export default countBadPairs ;
Original file line number Diff line number Diff line change 1
- export default function numIdenticalPairs ( nums : number [ ] ) : number {
2
- const cnt = new Map < number , number > ( ) ;
3
- for ( const num of nums ) {
4
- cnt . set ( num , ( cnt . get ( num ) ?? 0 ) + 1 ) ;
5
- }
6
- const ans = Array . from ( cnt . values ( ) ) . reduce (
7
- ( a , v ) => a + Math . floor ( ( v * ( v - 1 ) ) / 2 ) ,
8
- 0
9
- ) ;
10
- return ans ;
11
- }
1
+ export default function numIdenticalPairs ( nums : number [ ] ) : number {
2
+ const cnt = new Map < number , number > ( ) ;
3
+ for ( const num of nums ) {
4
+ cnt . set ( num , ( cnt . get ( num ) ?? 0 ) + 1 ) ;
5
+ }
6
+ const ans = Array . from ( cnt . values ( ) ) . reduce (
7
+ ( a , v ) => a + Math . floor ( ( v * ( v - 1 ) ) / 2 ) ,
8
+ 0 ,
9
+ ) ;
10
+ return ans ;
11
+ }
You can’t perform that action at this time.
0 commit comments