Skip to content

[pull] main from doocs:main #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/chatgpt-cr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: chatgpt-cr

permissions:
contents: read
issues: write
pull-requests: write

on:
pull_request:
types: [opened, reopened]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: anc95/ChatGPT-CodeReview@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
LANGUAGE: Chinese
90 changes: 89 additions & 1 deletion lcp/LCP 66. 最小展台数量/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,110 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:计数**

我们用哈希表或数组 $cnt$ 记录当前可用的展台以及数量。

然后遍历 $demand$,对于每一天,遍历该天的展台需求,如果 $cnt$ 中有该展台,则将其数量减一,否则我们需要准备一个新的展台,答案加一。遍历结束后,将该天的所有展台都加入 $cnt$ 中。

最后返回答案即可。

时间复杂度 $O(L)$,空间复杂度 $O(C)$。其中 $L$ 为 $demand$ 中所有字符串的长度之和,而 $C$ 为字符集的大小,本题中 $C = 26$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def minNumBooths(self, demand: List[str]) -> int:
cnt = Counter()
ans = 0
for s in demand:
for c in s:
if cnt[c]:
cnt[c] -= 1
else:
ans += 1
for c in s:
cnt[c] += 1
return ans
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int minNumBooths(String[] demand) {
int[] cnt = new int[26];
int ans = 0;
for (var s : demand) {
int m = s.length();
for (int i = 0; i < m; ++i) {
int j = s.charAt(i) - 'a';
if (cnt[j] > 0) {
--cnt[j];
} else {
++ans;
}
}
for (int i = 0; i < m; ++i) {
++cnt[s.charAt(i) - 'a'];
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int minNumBooths(vector<string>& demand) {
int cnt[26]{};
int ans = 0;
for (auto& s : demand) {
for (char& c : s) {
if (cnt[c - 'a']) {
--cnt[c - 'a'];
} else {
++ans;
}
}
for (char& c : s) {
++cnt[c - 'a'];
}
}
return ans;
}
};
```

### **Go**

```go
func minNumBooths(demand []string) (ans int) {
cnt := [26]int{}
for _, s := range demand {
for _, c := range s {
if cnt[c-'a'] > 0 {
cnt[c-'a']--
} else {
ans++
}
}
for _, c := range s {
cnt[c-'a']++
}
}
return
}
```

### **...**
Expand Down
20 changes: 20 additions & 0 deletions lcp/LCP 66. 最小展台数量/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int minNumBooths(vector<string>& demand) {
int cnt[26]{};
int ans = 0;
for (auto& s : demand) {
for (char& c : s) {
if (cnt[c - 'a']) {
--cnt[c - 'a'];
} else {
++ans;
}
}
for (char& c : s) {
++cnt[c - 'a'];
}
}
return ans;
}
};
16 changes: 16 additions & 0 deletions lcp/LCP 66. 最小展台数量/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func minNumBooths(demand []string) (ans int) {
cnt := [26]int{}
for _, s := range demand {
for _, c := range s {
if cnt[c-'a'] > 0 {
cnt[c-'a']--
} else {
ans++
}
}
for _, c := range s {
cnt[c-'a']++
}
}
return
}
21 changes: 21 additions & 0 deletions lcp/LCP 66. 最小展台数量/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int minNumBooths(String[] demand) {
int[] cnt = new int[26];
int ans = 0;
for (var s : demand) {
int m = s.length();
for (int i = 0; i < m; ++i) {
int j = s.charAt(i) - 'a';
if (cnt[j] > 0) {
--cnt[j];
} else {
++ans;
}
}
for (int i = 0; i < m; ++i) {
++cnt[s.charAt(i) - 'a'];
}
}
return ans;
}
}
13 changes: 13 additions & 0 deletions lcp/LCP 66. 最小展台数量/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def minNumBooths(self, demand: List[str]) -> int:
cnt = Counter()
ans = 0
for s in demand:
for c in s:
if cnt[c]:
cnt[c] -= 1
else:
ans += 1
for c in s:
cnt[c] += 1
return ans
30 changes: 25 additions & 5 deletions solution/0200-0299/0263.Ugly Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<pre>
<strong>输入:</strong>n = 14
<strong>输出:</strong>false
<strong>解释:</strong>14 不是丑数,因为它包含了另外一个质因数&nbsp;<code>7 </code>
<strong>解释:</strong>14 不是丑数,因为它包含了另外一个质因数 7
</pre>

<p>&nbsp;</p>
Expand Down Expand Up @@ -121,16 +121,16 @@ public:
*/
var isUgly = function (n) {
if (n < 1) return false;
while (n % 2 == 0) {
while (n % 2 === 0) {
n /= 2;
}
while (n % 3 == 0) {
while (n % 3 === 0) {
n /= 3;
}
while (n % 5 == 0) {
while (n % 5 === 0) {
n /= 5;
}
return n == 1;
return n === 1;
};
```

Expand All @@ -150,6 +150,26 @@ func isUgly(n int) bool {
}
```

### **PHP**

```php
class Solution {
/**
* @param Integer $n
* @return Boolean
*/
function isUgly($n) {
while ($n) {
if ($n % 2 == 0) $n = $n / 2;
else if ($n % 3 == 0) $n = $n / 3;
else if ($n % 5 == 0) $n = $n / 5;
else break;
}
return $n == 1;
}
}
```

### **...**

```
Expand Down
28 changes: 24 additions & 4 deletions solution/0200-0299/0263.Ugly Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ public:
*/
var isUgly = function (n) {
if (n < 1) return false;
while (n % 2 == 0) {
while (n % 2 === 0) {
n /= 2;
}
while (n % 3 == 0) {
while (n % 3 === 0) {
n /= 3;
}
while (n % 5 == 0) {
while (n % 5 === 0) {
n /= 5;
}
return n == 1;
return n === 1;
};
```

Expand All @@ -136,6 +136,26 @@ func isUgly(n int) bool {
}
```

### **PHP**

```php
class Solution {
/**
* @param Integer $n
* @return Boolean
*/
function isUgly($n) {
while ($n) {
if ($n % 2 == 0) $n = $n / 2;
else if ($n % 3 == 0) $n = $n / 3;
else if ($n % 5 == 0) $n = $n / 5;
else break;
}
return $n == 1;
}
}
```

### **...**

```
Expand Down
8 changes: 4 additions & 4 deletions solution/0200-0299/0263.Ugly Number/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/
var isUgly = function (n) {
if (n < 1) return false;
while (n % 2 == 0) {
while (n % 2 === 0) {
n /= 2;
}
while (n % 3 == 0) {
while (n % 3 === 0) {
n /= 3;
}
while (n % 5 == 0) {
while (n % 5 === 0) {
n /= 5;
}
return n == 1;
return n === 1;
};
15 changes: 15 additions & 0 deletions solution/0200-0299/0263.Ugly Number/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
/**
* @param Integer $n
* @return Boolean
*/
function isUgly($n) {
while ($n) {
if ($n % 2 == 0) $n = $n / 2;
else if ($n % 3 == 0) $n = $n / 3;
else if ($n % 5 == 0) $n = $n / 5;
else break;
}
return $n == 1;
}
}
Loading