File tree Expand file tree Collapse file tree 6 files changed +204
-2
lines changed
solution/1700-1799/1742.Maximum Number of Balls in a Box Expand file tree Collapse file tree 6 files changed +204
-2
lines changed Original file line number Diff line number Diff line change 65
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
66
67
67
``` python
68
-
68
+ class Solution :
69
+ def countBalls (self , lowLimit : int , highLimit : int ) -> int :
70
+ counter = [0 ] * 60
71
+ for i in range (lowLimit, highLimit + 1 ):
72
+ s = 0
73
+ while i:
74
+ s += (i % 10 )
75
+ i //= 10
76
+ counter[s] += 1
77
+ return max (counter)
69
78
```
70
79
71
80
### ** Java**
72
81
73
82
<!-- 这里可写当前语言的特殊实现逻辑 -->
74
83
75
84
``` java
85
+ class Solution {
86
+ public int countBalls (int lowLimit , int highLimit ) {
87
+ int [] counter = new int [60 ];
88
+ int ans = 0 ;
89
+ for (int i = lowLimit; i <= highLimit; ++ i) {
90
+ int s = 0 ;
91
+ int j = i;
92
+ while (j > 0 ) {
93
+ s += (j % 10 );
94
+ j /= 10 ;
95
+ }
96
+ ++ counter[s];
97
+ ans = Math . max(ans, counter[s]);
98
+ }
99
+ return ans;
100
+ }
101
+ }
102
+ ```
103
+
104
+ ### ** C++**
105
+
106
+ ``` cpp
107
+ class Solution {
108
+ public:
109
+ int countBalls(int lowLimit, int highLimit) {
110
+ vector<int > counter(60);
111
+ int ans = 0;
112
+ for (int i = lowLimit; i <= highLimit; ++i)
113
+ {
114
+ int s = 0, j = i;
115
+ while (j)
116
+ {
117
+ s += (j % 10);
118
+ j /= 10;
119
+ }
120
+ ++counter[ s] ;
121
+ ans = max(ans, counter[ s] );
122
+ }
123
+ return ans;
124
+ }
125
+ };
126
+ ```
76
127
128
+ ### **Go**
129
+
130
+ ```go
131
+ func countBalls(lowLimit int, highLimit int) int {
132
+ counter := make([]int, 60)
133
+ ans := 0
134
+ for i := lowLimit; i <= highLimit; i++ {
135
+ s, j := 0, i
136
+ for j > 0 {
137
+ s += (j % 10)
138
+ j /= 10
139
+ }
140
+ counter[s]++
141
+ if counter[s] > ans {
142
+ ans = counter[s]
143
+ }
144
+ }
145
+ return ans
146
+ }
77
147
```
78
148
79
149
### ** ...**
Original file line number Diff line number Diff line change @@ -57,13 +57,83 @@ Box 10 has the most number of balls with 2 balls.
57
57
### ** Python3**
58
58
59
59
``` python
60
-
60
+ class Solution :
61
+ def countBalls (self , lowLimit : int , highLimit : int ) -> int :
62
+ counter = [0 ] * 60
63
+ for i in range (lowLimit, highLimit + 1 ):
64
+ s = 0
65
+ while i:
66
+ s += (i % 10 )
67
+ i //= 10
68
+ counter[s] += 1
69
+ return max (counter)
61
70
```
62
71
63
72
### ** Java**
64
73
65
74
``` java
75
+ class Solution {
76
+ public int countBalls (int lowLimit , int highLimit ) {
77
+ int [] counter = new int [60 ];
78
+ int ans = 0 ;
79
+ for (int i = lowLimit; i <= highLimit; ++ i) {
80
+ int s = 0 ;
81
+ int j = i;
82
+ while (j > 0 ) {
83
+ s += (j % 10 );
84
+ j /= 10 ;
85
+ }
86
+ ++ counter[s];
87
+ ans = Math . max(ans, counter[s]);
88
+ }
89
+ return ans;
90
+ }
91
+ }
92
+ ```
93
+
94
+ ### ** C++**
95
+
96
+ ``` cpp
97
+ class Solution {
98
+ public:
99
+ int countBalls(int lowLimit, int highLimit) {
100
+ vector<int > counter(60);
101
+ int ans = 0;
102
+ for (int i = lowLimit; i <= highLimit; ++i)
103
+ {
104
+ int s = 0, j = i;
105
+ while (j)
106
+ {
107
+ s += (j % 10);
108
+ j /= 10;
109
+ }
110
+ ++counter[ s] ;
111
+ ans = max(ans, counter[ s] );
112
+ }
113
+ return ans;
114
+ }
115
+ };
116
+ ```
66
117
118
+ ### **Go**
119
+
120
+ ```go
121
+ func countBalls(lowLimit int, highLimit int) int {
122
+ counter := make([]int, 60)
123
+ ans := 0
124
+ for i := lowLimit; i <= highLimit; i++ {
125
+ s, j := 0, i
126
+ for j > 0 {
127
+ s += (j % 10)
128
+ j /= 10
129
+ }
130
+ counter[s]++
131
+ if counter[s] > ans {
132
+ ans = counter[s]
133
+ }
134
+ }
135
+ return ans
136
+ }
67
137
```
68
138
69
139
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int countBalls (int lowLimit, int highLimit) {
4
+ vector<int > counter (60 );
5
+ int ans = 0 ;
6
+ for (int i = lowLimit; i <= highLimit; ++i)
7
+ {
8
+ int s = 0 , j = i;
9
+ while (j)
10
+ {
11
+ s += (j % 10 );
12
+ j /= 10 ;
13
+ }
14
+ ++counter[s];
15
+ ans = max (ans, counter[s]);
16
+ }
17
+ return ans;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ func countBalls (lowLimit int , highLimit int ) int {
2
+ counter := make ([]int , 60 )
3
+ ans := 0
4
+ for i := lowLimit ; i <= highLimit ; i ++ {
5
+ s , j := 0 , i
6
+ for j > 0 {
7
+ s += (j % 10 )
8
+ j /= 10
9
+ }
10
+ counter [s ]++
11
+ if counter [s ] > ans {
12
+ ans = counter [s ]
13
+ }
14
+ }
15
+ return ans
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countBalls (int lowLimit , int highLimit ) {
3
+ int [] counter = new int [60 ];
4
+ int ans = 0 ;
5
+ for (int i = lowLimit ; i <= highLimit ; ++i ) {
6
+ int s = 0 ;
7
+ int j = i ;
8
+ while (j > 0 ) {
9
+ s += (j % 10 );
10
+ j /= 10 ;
11
+ }
12
+ ++counter [s ];
13
+ ans = Math .max (ans , counter [s ]);
14
+ }
15
+ return ans ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countBalls (self , lowLimit : int , highLimit : int ) -> int :
3
+ counter = [0 ] * 60
4
+ for i in range (lowLimit , highLimit + 1 ):
5
+ s = 0
6
+ while i :
7
+ s += (i % 10 )
8
+ i //= 10
9
+ counter [s ] += 1
10
+ return max (counter )
You can’t perform that action at this time.
0 commit comments