Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problems: No.2255,2256 #1717

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
yanglbme merged 1 commit into main from dev
Sep 28, 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
33 changes: 21 additions & 12 deletions solution/2200-2299/2255.Count Prefixes of a Given String/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ words 中是 s = "abc" 前缀的字符串为:

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

**方法一:遍历计数**

我们直接遍历数组 $words,ドル对于每个字符串 $w,ドル判断 $s$ 是否以 $w$ 为前缀,如果是则答案加一。

遍历结束后,返回答案即可。

时间复杂度 $O(m \times n),ドル其中 $m$ 和 $n$ 分别是数组 $words$ 的长度和字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -54,7 +62,7 @@ words 中是 s = "abc" 前缀的字符串为:
```python
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(word == s[: len(word)] for word in words)
return sum(s.startswith(w) for w in words)
```

### **Java**
Expand All @@ -65,8 +73,8 @@ class Solution:
class Solution {
public int countPrefixes(String[] words, String s) {
int ans = 0;
for (String word : words) {
if (word.equals(s.substring(0, Math.min(s.length(), word.length())))) {
for (String w : words) {
if (s.startsWith(w)) {
++ans;
}
}
Expand All @@ -82,9 +90,9 @@ class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int ans = 0;
for (auto& word : words)
if (s.substr(0, word.size()) == word)
++ans;
for (auto& w : words) {
ans += s.starts_with(w);
}
return ans;
}
};
Expand All @@ -93,21 +101,22 @@ public:
### **Go**

```go
func countPrefixes(words []string, s string) int {
ans := 0
for _, word := range words {
if strings.HasPrefix(s, word) {
func countPrefixes(words []string, s string) (ans int) {
for _, w := range words {
if strings.HasPrefix(s, w) {
ans++
}
}
return ans
return
}
```

### **TypeScript**

```ts

function countPrefixes(words: string[], s: string): number {
return words.filter(w => s.startsWith(w)).length;
}
```

### **...**
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,22 @@ Note that the same string can occur multiple times in words, and it should be co

## Solutions

**Solution 1: Traversal Counting**

We directly traverse the array words, and for each string w, we check if s starts with w as a prefix. If it does, we increment the answer by one.

After the traversal, we return the answer.

The time complexity is $O(m \times n),ドル where $m$ and $n$ are the lengths of the array words and the string s, respectively. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(word == s[: len(word)] for word in words)
return sum(s.startswith(w) for w in words)
```

### **Java**
Expand All @@ -57,8 +65,8 @@ class Solution:
class Solution {
public int countPrefixes(String[] words, String s) {
int ans = 0;
for (String word : words) {
if (word.equals(s.substring(0, Math.min(s.length(), word.length())))) {
for (String w : words) {
if (s.startsWith(w)) {
++ans;
}
}
Expand All @@ -74,9 +82,9 @@ class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int ans = 0;
for (auto& word : words)
if (s.substr(0, word.size()) == word)
++ans;
for (auto& w : words) {
ans += s.starts_with(w);
}
return ans;
}
};
Expand All @@ -85,21 +93,22 @@ public:
### **Go**

```go
func countPrefixes(words []string, s string) int {
ans := 0
for _, word := range words {
if strings.HasPrefix(s, word) {
func countPrefixes(words []string, s string) (ans int) {
for _, w := range words {
if strings.HasPrefix(s, w) {
ans++
}
}
return ans
return
}
```

### **TypeScript**

```ts

function countPrefixes(words: string[], s: string): number {
return words.filter(w => s.startsWith(w)).length;
}
```

### **...**
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int ans = 0;
for (auto& word : words)
if (s.substr(0, word.size()) == word)
++ans;
return ans;
}
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int ans = 0;
for (auto& w : words) {
ans += s.starts_with(w);
}
return ans;
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
func countPrefixes(words []string, s string) int {
ans := 0
for _, word := range words {
if strings.HasPrefix(s, word) {
func countPrefixes(words []string, s string) (ans int) {
for _, w := range words {
if strings.HasPrefix(s, w) {
ans++
}
}
return ans
return
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution {
public int countPrefixes(String[] words, String s) {
int ans = 0;
for (String word : words) {
if (word.equals(s.substring(0, Math.min(s.length(), word.length())))) {
++ans;
}
}
return ans;
}
class Solution {
public int countPrefixes(String[] words, String s) {
int ans = 0;
for (String w : words) {
if (s.startsWith(w)) {
++ans;
}
}
return ans;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(word == s[: len(word)] for word in words)
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(s.startswith(w) for w in words)
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function countPrefixes(words: string[], s: string): number {
return words.filter(w => s.startsWith(w)).length;
}
105 changes: 66 additions & 39 deletions solution/2200-2299/2256.Minimum Average Difference/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@

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

**方法一:遍历**

我们直接遍历数组 $nums,ドル对于每个下标 $i,ドル维护前 $i + 1$ 个元素的和 $pre$ 和后 $n - i - 1$ 个元素的和 $suf,ドル计算平均差的绝对值 $t,ドル如果 $t$ 小于当前最小值 $mi,ドル则更新答案 $ans = i$ 和最小值 $mi = t$。

遍历结束后,返回答案即可。

时间复杂度 $O(n),ドル其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -67,14 +75,15 @@
```python
class Solution:
def minimumAverageDifference(self, nums: List[int]) -> int:
s = list(accumulate(nums))
ans, n = 0, len(nums)
mi = inf
for i in range(n):
a = s[i] // (i + 1)
b = 0 if i == n - 1 else (s[-1] - s[i]) // (n - i - 1)
t = abs(a - b)
if mi > t:
pre, suf = 0, sum(nums)
n = len(nums)
ans, mi = 0, inf
for i, x in enumerate(nums):
pre += x
suf -= x
a = pre // (i + 1)
b = 0 if n - i - 1 == 0 else suf // (n - i - 1)
if (t := abs(a - b)) < mi:
ans = i
mi = t
return ans
Expand All @@ -88,18 +97,19 @@ class Solution:
class Solution {
public int minimumAverageDifference(int[] nums) {
int n = nums.length;
long[] s = new long[n];
s[0] = nums[0];
for (int i = 1; i < n; ++i) {
s[i] = s[i - 1] + nums[i];
long pre = 0, suf = 0;
for (int x : nums) {
suf += x;
}
int ans = 0;
long mi = Long.MAX_VALUE;
for (int i = 0; i < n; ++i) {
long a = s[i] / (i + 1);
long b = i == n - 1 ? 0 : (s[n - 1] - s[i]) / (n - i - 1);
pre += nums[i];
suf -= nums[i];
long a = pre / (i + 1);
long b = n - i - 1 == 0 ? 0 : suf / (n - i - 1);
long t = Math.abs(a - b);
if (mi > t) {
if (t < mi) {
ans = i;
mi = t;
}
Expand All @@ -112,22 +122,22 @@ class Solution {
### **C++**

```cpp
typedef long long ll;

class Solution {
public:
int minimumAverageDifference(vector<int>& nums) {
int n = nums.size();
vector<ll> s(n);
s[0] = nums[0];
for (int i = 1; i < n; ++i) s[i] = s[i - 1] + nums[i];
using ll = long long;
ll pre = 0;
ll suf = accumulate(nums.begin(), nums.end(), 0LL);
int ans = 0;
ll mi = LONG_MAX;
ll mi = suf;
for (int i = 0; i < n; ++i) {
ll a = s[i] / (i + 1);
ll b = i == n - 1 ? 0 : (s[n - 1] - s[i]) / (n - i - 1);
pre += nums[i];
suf -= nums[i];
ll a = pre / (i + 1);
ll b = n - i - 1 == 0 ? 0 : suf / (n - i - 1);
ll t = abs(a - b);
if (mi > t) {
if (t < mi) {
ans = i;
mi = t;
}
Expand All @@ -140,28 +150,27 @@ public:
### **Go**

```go
func minimumAverageDifference(nums []int) int {
func minimumAverageDifference(nums []int) (ans int) {
n := len(nums)
s := make([]int, n)
s[0] = nums[0]
for i := 1; i < n; i++ {
s[i] = s[i-1] + nums[i]
pre, suf := 0, 0
for _, x := range nums {
suf += x
}
ans := 0
mi := math.MaxInt32
for i := 0; i < n; i++ {
a := s[i] / (i + 1)
mi := suf
for i, x := range nums {
pre += x
suf -= x
a := pre / (i + 1)
b := 0
if i != n-1 {
b = (s[n-1] - s[i]) / (n - i - 1)
if n-i-1 != 0 {
b = suf / (n - i - 1)
}
t := abs(a - b)
if mi > t {
if t := abs(a - b); t < mi {
ans = i
mi = t
}
}
return ans
return
}

func abs(x int) int {
Expand All @@ -175,7 +184,25 @@ func abs(x int) int {
### **TypeScript**

```ts

function minimumAverageDifference(nums: number[]): number {
const n = nums.length;
let pre = 0;
let suf = nums.reduce((a, b) => a + b);
let ans = 0;
let mi = suf;
for (let i = 0; i < n; ++i) {
pre += nums[i];
suf -= nums[i];
const a = Math.floor(pre / (i + 1));
const b = n - i - 1 === 0 ? 0 : Math.floor(suf / (n - i - 1));
const t = Math.abs(a - b);
if (t < mi) {
ans = i;
mi = t;
}
}
return ans;
}
```

### **...**
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /