Skip to content

Commit 94e002a

Browse files
committed
feat: add solutions to lc problem: No.3765
1 parent 5e2ef70 commit 94e002a

File tree

7 files changed

+502
-8
lines changed

7 files changed

+502
-8
lines changed

solution/3700-3799/3765.Complete Prime Number/README.md

Lines changed: 171 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,32 +91,199 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3765.Co
9191

9292
<!-- solution:start -->
9393

94-
### 方法一
94+
### 方法一:数学
95+
96+
我们定义一个函数 $\text{is\_prime}(x)$ 来判断一个数 $x$ 是否为质数。具体地,如果 $x < 2$,则 $x$ 不是质数;否则,我们检查从 $2$ 到 $\sqrt{x}$ 的所有整数 $i$,如果存在某个 $i$ 能整除 $x$,则 $x$ 不是质数,否则 $x$ 是质数。
97+
98+
接下来,我们将整数 $\textit{num}$ 转换为字符串 $s$,并依次检查 $s$ 的每个前缀和后缀所对应的整数是否为质数。对于前缀,我们从左到右构造整数 $x$,对于后缀,我们从右到左构造整数 $x$。如果在检查过程中发现某个前缀或后缀对应的整数不是质数,则返回 $\text{false}$;如果所有前缀和后缀对应的整数都是质数,则返回 $\text{true}$。
99+
100+
时间复杂度 $O(\sqrt{n} \times \log n)$,空间复杂度 $O(\log n)$,其中 $n$ 是整数 $\textit{num}$ 的大小。
95101

96102
<!-- tabs:start -->
97103

98104
#### Python3
99105

100106
```python
101-
107+
class Solution:
108+
def completePrime(self, num: int) -> bool:
109+
def is_prime(x: int) -> bool:
110+
if x < 2:
111+
return False
112+
return all(x % i for i in range(2, int(sqrt(x)) + 1))
113+
114+
s = str(num)
115+
x = 0
116+
for c in s:
117+
x = x * 10 + int(c)
118+
if not is_prime(x):
119+
return False
120+
x, p = 0, 1
121+
for c in s[::-1]:
122+
x = p * int(c) + x
123+
p *= 10
124+
if not is_prime(x):
125+
return False
126+
return True
102127
```
103128

104129
#### Java
105130

106131
```java
107-
132+
class Solution {
133+
public boolean completePrime(int num) {
134+
char[] s = String.valueOf(num).toCharArray();
135+
int x = 0;
136+
for (int i = 0; i < s.length; i++) {
137+
x = x * 10 + (s[i] - '0');
138+
if (!isPrime(x)) {
139+
return false;
140+
}
141+
}
142+
x = 0;
143+
int p = 1;
144+
for (int i = s.length - 1; i >= 0; i--) {
145+
x = p * (s[i] - '0') + x;
146+
p *= 10;
147+
if (!isPrime(x)) {
148+
return false;
149+
}
150+
}
151+
return true;
152+
}
153+
154+
private boolean isPrime(int x) {
155+
if (x < 2) {
156+
return false;
157+
}
158+
for (int i = 2; i * i <= x; i++) {
159+
if (x % i == 0) {
160+
return false;
161+
}
162+
}
163+
return true;
164+
}
165+
}
108166
```
109167

110168
#### C++
111169

112170
```cpp
113-
171+
class Solution {
172+
public:
173+
bool completePrime(int num) {
174+
auto isPrime = [&](int x) {
175+
if (x < 2) {
176+
return false;
177+
}
178+
for (int i = 2; i * i <= x; ++i) {
179+
if (x % i == 0) {
180+
return false;
181+
}
182+
}
183+
return true;
184+
};
185+
186+
string s = to_string(num);
187+
188+
int x = 0;
189+
for (char c : s) {
190+
x = x * 10 + (c - '0');
191+
if (!isPrime(x)) {
192+
return false;
193+
}
194+
}
195+
196+
x = 0;
197+
int p = 1;
198+
for (int i = (int) s.size() - 1; i >= 0; --i) {
199+
x = p * (s[i] - '0') + x;
200+
p *= 10;
201+
if (!isPrime(x)) {
202+
return false;
203+
}
204+
}
205+
206+
return true;
207+
}
208+
};
114209
```
115210

116211
#### Go
117212

118213
```go
214+
func completePrime(num int) bool {
215+
isPrime := func(x int) bool {
216+
if x < 2 {
217+
return false
218+
}
219+
for i := 2; i*i <= x; i++ {
220+
if x%i == 0 {
221+
return false
222+
}
223+
}
224+
return true
225+
}
226+
227+
s := strconv.Itoa(num)
228+
229+
x := 0
230+
for i := 0; i < len(s); i++ {
231+
x = x*10 + int(s[i]-'0')
232+
if !isPrime(x) {
233+
return false
234+
}
235+
}
236+
237+
x = 0
238+
p := 1
239+
for i := len(s) - 1; i >= 0; i-- {
240+
x = p*int(s[i]-'0') + x
241+
p *= 10
242+
if !isPrime(x) {
243+
return false
244+
}
245+
}
246+
247+
return true
248+
}
249+
```
119250

251+
#### TypeScript
252+
253+
```ts
254+
function completePrime(num: number): boolean {
255+
const isPrime = (x: number): boolean => {
256+
if (x < 2) return false;
257+
for (let i = 2; i * i <= x; i++) {
258+
if (x % i === 0) {
259+
return false;
260+
}
261+
}
262+
return true;
263+
};
264+
265+
const s = String(num);
266+
267+
let x = 0;
268+
for (let i = 0; i < s.length; i++) {
269+
x = x * 10 + (s.charCodeAt(i) - 48);
270+
if (!isPrime(x)) {
271+
return false;
272+
}
273+
}
274+
275+
x = 0;
276+
let p = 1;
277+
for (let i = s.length - 1; i >= 0; i--) {
278+
x = p * (s.charCodeAt(i) - 48) + x;
279+
p *= 10;
280+
if (!isPrime(x)) {
281+
return false;
282+
}
283+
}
284+
285+
return true;
286+
}
120287
```
121288

122289
<!-- tabs:end -->

0 commit comments

Comments
 (0)