File tree Expand file tree Collapse file tree 6 files changed +260
-2
lines changed
solution/1300-1399/1399.Count Largest Group Expand file tree Collapse file tree 6 files changed +260
-2
lines changed Original file line number Diff line number Diff line change 51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
+ ** 方法一:哈希表模拟**
55
+
54
56
<!-- tabs:start -->
55
57
56
58
### ** Python3**
57
59
58
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
61
60
62
``` python
61
-
63
+ class Solution :
64
+ def countLargestGroup (self , n : int ) -> int :
65
+ cnt = Counter()
66
+ ans, mx = 0 , 0
67
+ for i in range (1 , n + 1 ):
68
+ t = sum (int (v) for v in str (i))
69
+ cnt[t] += 1
70
+ if mx < cnt[t]:
71
+ mx = cnt[t]
72
+ ans = 1
73
+ elif mx == cnt[t]:
74
+ ans += 1
75
+ return ans
62
76
```
63
77
64
78
### ** Java**
65
79
66
80
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
81
68
82
``` java
83
+ class Solution {
84
+ public int countLargestGroup (int n ) {
85
+ int [] cnt = new int [40 ];
86
+ int mx = 0 , ans = 0 ;
87
+ for (int i = 1 ; i <= n; ++ i) {
88
+ int t = 0 ;
89
+ int j = i;
90
+ while (j != 0 ) {
91
+ t += j % 10 ;
92
+ j /= 10 ;
93
+ }
94
+ ++ cnt[t];
95
+ if (mx < cnt[t]) {
96
+ mx = cnt[t];
97
+ ans = 1 ;
98
+ } else if (mx == cnt[t]) {
99
+ ++ ans;
100
+ }
101
+ }
102
+ return ans;
103
+ }
104
+ }
105
+ ```
106
+
107
+ ### ** C++**
108
+
109
+ ``` cpp
110
+ class Solution {
111
+ public:
112
+ int countLargestGroup(int n) {
113
+ vector<int > cnt(40);
114
+ int mx = 0, ans = 0;
115
+ for (int i = 1; i <= n; ++i)
116
+ {
117
+ int t = 0;
118
+ int j = i;
119
+ while (j)
120
+ {
121
+ t += j % 10;
122
+ j /= 10;
123
+ }
124
+ ++cnt[ t] ;
125
+ if (mx < cnt[ t] )
126
+ {
127
+ mx = cnt[ t] ;
128
+ ans = 1;
129
+ }
130
+ else if (mx == cnt[ t] ) ++ans;
131
+ }
132
+ return ans;
133
+ }
134
+ };
135
+ ```
69
136
137
+ ### **Go**
138
+
139
+ ```go
140
+ func countLargestGroup(n int) int {
141
+ cnt := make([]int, 40)
142
+ mx, ans := 0, 0
143
+ for i := 1; i <= n; i++ {
144
+ t := 0
145
+ j := i
146
+ for j != 0 {
147
+ t += j % 10
148
+ j /= 10
149
+ }
150
+ cnt[t]++
151
+ if mx < cnt[t] {
152
+ mx = cnt[t]
153
+ ans = 1
154
+ } else if mx == cnt[t] {
155
+ ans++
156
+ }
157
+ }
158
+ return ans
159
+ }
70
160
```
71
161
72
162
### ** ...**
Original file line number Diff line number Diff line change @@ -43,13 +43,101 @@ There are 4 groups with largest size.
43
43
### ** Python3**
44
44
45
45
``` python
46
-
46
+ class Solution :
47
+ def countLargestGroup (self , n : int ) -> int :
48
+ cnt = Counter()
49
+ ans, mx = 0 , 0
50
+ for i in range (1 , n + 1 ):
51
+ t = sum (int (v) for v in str (i))
52
+ cnt[t] += 1
53
+ if mx < cnt[t]:
54
+ mx = cnt[t]
55
+ ans = 1
56
+ elif mx == cnt[t]:
57
+ ans += 1
58
+ return ans
47
59
```
48
60
49
61
### ** Java**
50
62
51
63
``` java
64
+ class Solution {
65
+ public int countLargestGroup (int n ) {
66
+ int [] cnt = new int [40 ];
67
+ int mx = 0 , ans = 0 ;
68
+ for (int i = 1 ; i <= n; ++ i) {
69
+ int t = 0 ;
70
+ int j = i;
71
+ while (j != 0 ) {
72
+ t += j % 10 ;
73
+ j /= 10 ;
74
+ }
75
+ ++ cnt[t];
76
+ if (mx < cnt[t]) {
77
+ mx = cnt[t];
78
+ ans = 1 ;
79
+ } else if (mx == cnt[t]) {
80
+ ++ ans;
81
+ }
82
+ }
83
+ return ans;
84
+ }
85
+ }
86
+ ```
87
+
88
+ ### ** C++**
89
+
90
+ ``` cpp
91
+ class Solution {
92
+ public:
93
+ int countLargestGroup(int n) {
94
+ vector<int > cnt(40);
95
+ int mx = 0, ans = 0;
96
+ for (int i = 1; i <= n; ++i)
97
+ {
98
+ int t = 0;
99
+ int j = i;
100
+ while (j)
101
+ {
102
+ t += j % 10;
103
+ j /= 10;
104
+ }
105
+ ++cnt[ t] ;
106
+ if (mx < cnt[ t] )
107
+ {
108
+ mx = cnt[ t] ;
109
+ ans = 1;
110
+ }
111
+ else if (mx == cnt[ t] ) ++ans;
112
+ }
113
+ return ans;
114
+ }
115
+ };
116
+ ```
52
117
118
+ ### **Go**
119
+
120
+ ```go
121
+ func countLargestGroup(n int) int {
122
+ cnt := make([]int, 40)
123
+ mx, ans := 0, 0
124
+ for i := 1; i <= n; i++ {
125
+ t := 0
126
+ j := i
127
+ for j != 0 {
128
+ t += j % 10
129
+ j /= 10
130
+ }
131
+ cnt[t]++
132
+ if mx < cnt[t] {
133
+ mx = cnt[t]
134
+ ans = 1
135
+ } else if mx == cnt[t] {
136
+ ans++
137
+ }
138
+ }
139
+ return ans
140
+ }
53
141
```
54
142
55
143
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int countLargestGroup (int n) {
4
+ vector<int > cnt (40 );
5
+ int mx = 0 , ans = 0 ;
6
+ for (int i = 1 ; i <= n; ++i)
7
+ {
8
+ int t = 0 ;
9
+ int j = i;
10
+ while (j)
11
+ {
12
+ t += j % 10 ;
13
+ j /= 10 ;
14
+ }
15
+ ++cnt[t];
16
+ if (mx < cnt[t])
17
+ {
18
+ mx = cnt[t];
19
+ ans = 1 ;
20
+ }
21
+ else if (mx == cnt[t]) ++ans;
22
+ }
23
+ return ans;
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ func countLargestGroup (n int ) int {
2
+ cnt := make ([]int , 40 )
3
+ mx , ans := 0 , 0
4
+ for i := 1 ; i <= n ; i ++ {
5
+ t := 0
6
+ j := i
7
+ for j != 0 {
8
+ t += j % 10
9
+ j /= 10
10
+ }
11
+ cnt [t ]++
12
+ if mx < cnt [t ] {
13
+ mx = cnt [t ]
14
+ ans = 1
15
+ } else if mx == cnt [t ] {
16
+ ans ++
17
+ }
18
+ }
19
+ return ans
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countLargestGroup (int n ) {
3
+ int [] cnt = new int [40 ];
4
+ int mx = 0 , ans = 0 ;
5
+ for (int i = 1 ; i <= n ; ++i ) {
6
+ int t = 0 ;
7
+ int j = i ;
8
+ while (j != 0 ) {
9
+ t += j % 10 ;
10
+ j /= 10 ;
11
+ }
12
+ ++cnt [t ];
13
+ if (mx < cnt [t ]) {
14
+ mx = cnt [t ];
15
+ ans = 1 ;
16
+ } else if (mx == cnt [t ]) {
17
+ ++ans ;
18
+ }
19
+ }
20
+ return ans ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countLargestGroup (self , n : int ) -> int :
3
+ cnt = Counter ()
4
+ ans , mx = 0 , 0
5
+ for i in range (1 , n + 1 ):
6
+ t = sum (int (v ) for v in str (i ))
7
+ cnt [t ] += 1
8
+ if mx < cnt [t ]:
9
+ mx = cnt [t ]
10
+ ans = 1
11
+ elif mx == cnt [t ]:
12
+ ans += 1
13
+ return ans
You can’t perform that action at this time.
0 commit comments