File tree Expand file tree Collapse file tree 7 files changed +364
-2
lines changed
solution/1300-1399/1390.Four Divisors Expand file tree Collapse file tree 7 files changed +364
-2
lines changed Original file line number Diff line number Diff line change 48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
+ ** 方法一:因数分解**
52
+
53
+ 我们可以对每个数进行因数分解,如果因数的个数为 $4$ 个,那么这个数就是符合题意的数,我们将其因数累加到答案中即可。
54
+
55
+ 时间复杂度 $O(n \times \sqrt{n})$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
56
+
51
57
<!-- tabs:start -->
52
58
53
59
### ** Python3**
54
60
55
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
62
57
63
``` python
58
-
64
+ class Solution :
65
+ def sumFourDivisors (self , nums : List[int ]) -> int :
66
+ def f (x : int ) -> int :
67
+ i = 2
68
+ cnt, s = 2 , x + 1
69
+ while i <= x // i:
70
+ if x % i == 0 :
71
+ cnt += 1
72
+ s += i
73
+ if i * i != x:
74
+ cnt += 1
75
+ s += x // i
76
+ i += 1
77
+ return s if cnt == 4 else 0
78
+
79
+ return sum (f(x) for x in nums)
59
80
```
60
81
61
82
### ** Java**
62
83
63
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
85
65
86
``` java
87
+ class Solution {
88
+ public int sumFourDivisors (int [] nums ) {
89
+ int ans = 0 ;
90
+ for (int x : nums) {
91
+ ans += f(x);
92
+ }
93
+ return ans;
94
+ }
95
+
96
+ private int f (int x ) {
97
+ int cnt = 2 , s = x + 1 ;
98
+ for (int i = 2 ; i <= x / i; ++ i) {
99
+ if (x % i == 0 ) {
100
+ ++ cnt;
101
+ s += i;
102
+ if (i * i != x) {
103
+ ++ cnt;
104
+ s += x / i;
105
+ }
106
+ }
107
+ }
108
+ return cnt == 4 ? s : 0 ;
109
+ }
110
+ }
111
+ ```
112
+
113
+ ### ** C++**
114
+
115
+ ``` cpp
116
+ class Solution {
117
+ public:
118
+ int sumFourDivisors(vector<int >& nums) {
119
+ int ans = 0;
120
+ for (int x : nums) {
121
+ ans += f(x);
122
+ }
123
+ return ans;
124
+ }
125
+
126
+ int f(int x) {
127
+ int cnt = 2, s = x + 1;
128
+ for (int i = 2; i <= x / i; ++i) {
129
+ if (x % i == 0) {
130
+ ++cnt;
131
+ s += i;
132
+ if (i * i != x) {
133
+ ++cnt;
134
+ s += x / i;
135
+ }
136
+ }
137
+ }
138
+ return cnt == 4 ? s : 0 ;
139
+ }
140
+ };
141
+ ```
142
+
143
+ ### ** Go**
144
+
145
+ ``` go
146
+ func sumFourDivisors (nums []int ) (ans int ) {
147
+ f := func (x int ) int {
148
+ cnt , s := 2 , x+1
149
+ for i := 2 ; i <= x/i; i++ {
150
+ if x%i == 0 {
151
+ cnt++
152
+ s += i
153
+ if i*i != x {
154
+ cnt++
155
+ s += x / i
156
+ }
157
+ }
158
+ }
159
+ if cnt == 4 {
160
+ return s
161
+ }
162
+ return 0
163
+ }
164
+ for _ , x := range nums {
165
+ ans += f (x)
166
+ }
167
+ return
168
+ }
169
+ ```
66
170
171
+ ### ** TypeScript**
172
+
173
+ ``` ts
174
+ function sumFourDivisors(nums : number []): number {
175
+ const f = (x : number ): number => {
176
+ let cnt = 2 ;
177
+ let s = x + 1 ;
178
+ for (let i = 2 ; i * i <= x ; ++ i ) {
179
+ if (x % i === 0 ) {
180
+ ++ cnt ;
181
+ s += i ;
182
+ if (i * i !== x ) {
183
+ ++ cnt ;
184
+ s += Math .floor (x / i );
185
+ }
186
+ }
187
+ }
188
+ return cnt === 4 ? s : 0 ;
189
+ };
190
+ let ans = 0 ;
191
+ for (const x of nums ) {
192
+ ans += f (x );
193
+ }
194
+ return ans ;
195
+ }
67
196
```
68
197
69
198
### ** ...**
Original file line number Diff line number Diff line change @@ -48,13 +48,136 @@ The answer is the sum of divisors of 21 only.
48
48
### ** Python3**
49
49
50
50
``` python
51
-
51
+ class Solution :
52
+ def sumFourDivisors (self , nums : List[int ]) -> int :
53
+ def f (x : int ) -> int :
54
+ i = 2
55
+ cnt, s = 2 , x + 1
56
+ while i <= x // i:
57
+ if x % i == 0 :
58
+ cnt += 1
59
+ s += i
60
+ if i * i != x:
61
+ cnt += 1
62
+ s += x // i
63
+ i += 1
64
+ return s if cnt == 4 else 0
65
+
66
+ return sum (f(x) for x in nums)
52
67
```
53
68
54
69
### ** Java**
55
70
56
71
``` java
72
+ class Solution {
73
+ public int sumFourDivisors (int [] nums ) {
74
+ int ans = 0 ;
75
+ for (int x : nums) {
76
+ ans += f(x);
77
+ }
78
+ return ans;
79
+ }
80
+
81
+ private int f (int x ) {
82
+ int cnt = 2 , s = x + 1 ;
83
+ for (int i = 2 ; i <= x / i; ++ i) {
84
+ if (x % i == 0 ) {
85
+ ++ cnt;
86
+ s += i;
87
+ if (i * i != x) {
88
+ ++ cnt;
89
+ s += x / i;
90
+ }
91
+ }
92
+ }
93
+ return cnt == 4 ? s : 0 ;
94
+ }
95
+ }
96
+ ```
97
+
98
+ ### ** C++**
99
+
100
+ ``` cpp
101
+ class Solution {
102
+ public:
103
+ int sumFourDivisors(vector<int >& nums) {
104
+ int ans = 0;
105
+ for (int x : nums) {
106
+ ans += f(x);
107
+ }
108
+ return ans;
109
+ }
110
+
111
+ int f(int x) {
112
+ int cnt = 2, s = x + 1;
113
+ for (int i = 2; i <= x / i; ++i) {
114
+ if (x % i == 0) {
115
+ ++cnt;
116
+ s += i;
117
+ if (i * i != x) {
118
+ ++cnt;
119
+ s += x / i;
120
+ }
121
+ }
122
+ }
123
+ return cnt == 4 ? s : 0 ;
124
+ }
125
+ };
126
+ ```
127
+
128
+ ### ** Go**
129
+
130
+ ``` go
131
+ func sumFourDivisors (nums []int ) (ans int ) {
132
+ f := func (x int ) int {
133
+ cnt , s := 2 , x+1
134
+ for i := 2 ; i <= x/i; i++ {
135
+ if x%i == 0 {
136
+ cnt++
137
+ s += i
138
+ if i*i != x {
139
+ cnt++
140
+ s += x / i
141
+ }
142
+ }
143
+ }
144
+ if cnt == 4 {
145
+ return s
146
+ }
147
+ return 0
148
+ }
149
+ for _ , x := range nums {
150
+ ans += f (x)
151
+ }
152
+ return
153
+ }
154
+ ```
57
155
156
+ ### ** TypeScript**
157
+
158
+ ``` ts
159
+ function sumFourDivisors(nums : number []): number {
160
+ const f = (x : number ): number => {
161
+ let cnt = 2 ;
162
+ let s = x + 1 ;
163
+ for (let i = 2 ; i * i <= x ; ++ i ) {
164
+ if (x % i === 0 ) {
165
+ ++ cnt ;
166
+ s += i ;
167
+ if (i * i !== x ) {
168
+ ++ cnt ;
169
+ s += Math .floor (x / i );
170
+ }
171
+ }
172
+ }
173
+ return cnt === 4 ? s : 0 ;
174
+ };
175
+ let ans = 0 ;
176
+ for (const x of nums ) {
177
+ ans += f (x );
178
+ }
179
+ return ans ;
180
+ }
58
181
```
59
182
60
183
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int sumFourDivisors (vector<int >& nums) {
4
+ int ans = 0 ;
5
+ for (int x : nums) {
6
+ ans += f (x);
7
+ }
8
+ return ans;
9
+ }
10
+
11
+ int f (int x) {
12
+ int cnt = 2 , s = x + 1 ;
13
+ for (int i = 2 ; i <= x / i; ++i) {
14
+ if (x % i == 0 ) {
15
+ ++cnt;
16
+ s += i;
17
+ if (i * i != x) {
18
+ ++cnt;
19
+ s += x / i;
20
+ }
21
+ }
22
+ }
23
+ return cnt == 4 ? s : 0 ;
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ func sumFourDivisors (nums []int ) (ans int ) {
2
+ f := func (x int ) int {
3
+ cnt , s := 2 , x + 1
4
+ for i := 2 ; i <= x / i ; i ++ {
5
+ if x % i == 0 {
6
+ cnt ++
7
+ s += i
8
+ if i * i != x {
9
+ cnt ++
10
+ s += x / i
11
+ }
12
+ }
13
+ }
14
+ if cnt == 4 {
15
+ return s
16
+ }
17
+ return 0
18
+ }
19
+ for _ , x := range nums {
20
+ ans += f (x )
21
+ }
22
+ return
23
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int sumFourDivisors (int [] nums ) {
3
+ int ans = 0 ;
4
+ for (int x : nums ) {
5
+ ans += f (x );
6
+ }
7
+ return ans ;
8
+ }
9
+
10
+ private int f (int x ) {
11
+ int cnt = 2 , s = x + 1 ;
12
+ for (int i = 2 ; i <= x / i ; ++i ) {
13
+ if (x % i == 0 ) {
14
+ ++cnt ;
15
+ s += i ;
16
+ if (i * i != x ) {
17
+ ++cnt ;
18
+ s += x / i ;
19
+ }
20
+ }
21
+ }
22
+ return cnt == 4 ? s : 0 ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def sumFourDivisors (self , nums : List [int ]) -> int :
3
+ def f (x : int ) -> int :
4
+ i = 2
5
+ cnt , s = 2 , x + 1
6
+ while i <= x // i :
7
+ if x % i == 0 :
8
+ cnt += 1
9
+ s += i
10
+ if i * i != x :
11
+ cnt += 1
12
+ s += x // i
13
+ i += 1
14
+ return s if cnt == 4 else 0
15
+
16
+ return sum (f (x ) for x in nums )
You can’t perform that action at this time.
0 commit comments