Skip to content

feat: update solutions to lcci problems #2227

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 1 commit into from
Jan 16, 2024
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
78 changes: 39 additions & 39 deletions lcci/01.03.String to URL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,6 @@ class Solution:
return S[:length].replace(' ', '%20')
```

```java
class Solution {
public String replaceSpaces(String S, int length) {
char[] cs = S.toCharArray();
int j = cs.length;
for (int i = length - 1; i >= 0; --i) {
if (cs[i] == ' ') {
cs[--j] = '0';
cs[--j] = '2';
cs[--j] = '%';
} else {
cs[--j] = cs[i];
}
}
return new String(cs, j, cs.length - j);
}
}
```

```go
func replaceSpaces(S string, length int) string {
// return url.PathEscape(S[:length])
j := len(S)
b := []byte(S)
for i := length - 1; i >= 0; i-- {
if b[i] == ' ' {
b[j-1] = '0'
b[j-2] = '2'
b[j-3] = '%'
j -= 3
} else {
b[j-1] = b[i]
j--
}
}
return string(b[j:])
}
```

```ts
function replaceSpaces(S: string, length: number): string {
return S.slice(0, length).replace(/\s/g, '%20');
Expand Down Expand Up @@ -121,6 +82,45 @@ class Solution:
return ''.join(['%20' if c == ' ' else c for c in S[:length]])
```

```java
class Solution {
public String replaceSpaces(String S, int length) {
char[] cs = S.toCharArray();
int j = cs.length;
for (int i = length - 1; i >= 0; --i) {
if (cs[i] == ' ') {
cs[--j] = '0';
cs[--j] = '2';
cs[--j] = '%';
} else {
cs[--j] = cs[i];
}
}
return new String(cs, j, cs.length - j);
}
}
```

```go
func replaceSpaces(S string, length int) string {
// return url.PathEscape(S[:length])
j := len(S)
b := []byte(S)
for i := length - 1; i >= 0; i-- {
if b[i] == ' ' {
b[j-1] = '0'
b[j-2] = '2'
b[j-3] = '%'
j -= 3
} else {
b[j-1] = b[i]
j--
}
}
return string(b[j:])
}
```

```rust
impl Solution {
pub fn replace_spaces(s: String, length: i32) -> String {
Expand Down
78 changes: 39 additions & 39 deletions lcci/01.03.String to URL/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,6 @@ class Solution:
return S[:length].replace(' ', '%20')
```

```java
class Solution {
public String replaceSpaces(String S, int length) {
char[] cs = S.toCharArray();
int j = cs.length;
for (int i = length - 1; i >= 0; --i) {
if (cs[i] == ' ') {
cs[--j] = '0';
cs[--j] = '2';
cs[--j] = '%';
} else {
cs[--j] = cs[i];
}
}
return new String(cs, j, cs.length - j);
}
}
```

```go
func replaceSpaces(S string, length int) string {
// return url.PathEscape(S[:length])
j := len(S)
b := []byte(S)
for i := length - 1; i >= 0; i-- {
if b[i] == ' ' {
b[j-1] = '0'
b[j-2] = '2'
b[j-3] = '%'
j -= 3
} else {
b[j-1] = b[i]
j--
}
}
return string(b[j:])
}
```

```ts
function replaceSpaces(S: string, length: number): string {
return S.slice(0, length).replace(/\s/g, '%20');
Expand Down Expand Up @@ -134,6 +95,45 @@ class Solution:
return ''.join(['%20' if c == ' ' else c for c in S[:length]])
```

```java
class Solution {
public String replaceSpaces(String S, int length) {
char[] cs = S.toCharArray();
int j = cs.length;
for (int i = length - 1; i >= 0; --i) {
if (cs[i] == ' ') {
cs[--j] = '0';
cs[--j] = '2';
cs[--j] = '%';
} else {
cs[--j] = cs[i];
}
}
return new String(cs, j, cs.length - j);
}
}
```

```go
func replaceSpaces(S string, length int) string {
// return url.PathEscape(S[:length])
j := len(S)
b := []byte(S)
for i := length - 1; i >= 0; i-- {
if b[i] == ' ' {
b[j-1] = '0'
b[j-2] = '2'
b[j-3] = '%'
j -= 3
} else {
b[j-1] = b[i]
j--
}
}
return string(b[j:])
}
```

```rust
impl Solution {
pub fn replace_spaces(s: String, length: i32) -> String {
Expand Down
2 changes: 1 addition & 1 deletion lcci/01.03.String to URL/Solution2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Solution:
def replaceSpaces(self, S: str, length: int) -> str:
return ''.join(['%20' if c == ' ' else c for c in S[:length]])
return "".join(["%20" if c == " " else c for c in S[:length]])
8 changes: 7 additions & 1 deletion lcci/01.04.Palindrome Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ impl Solution {

<!-- tabs:end -->

### 方法二
### 方法二:哈希表的另一种实现

我们用哈希表 $vis$ 存储每个字符是否出现过。若出现过,则从哈希表中删除该字符;否则,将该字符加入哈希表。

最后判断哈希表中字符的个数是否小于 $2$,若是,则是回文排列。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。

<!-- tabs:start -->

Expand Down
8 changes: 7 additions & 1 deletion lcci/01.04.Palindrome Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ impl Solution {

<!-- tabs:end -->

### Solution 2
### Solution 2: Another Implementation of Hash Table

We use a hash table $vis$ to store whether each character has appeared. If it has appeared, we remove the character from the hash table; otherwise, we add the character to the hash table.

Finally, we check whether the number of characters in the hash table is less than $2$. If it is, then it is a palindrome permutation.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

<!-- tabs:start -->

Expand Down
34 changes: 14 additions & 20 deletions lcci/01.06.Compress String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ class Solution:
return min(S, t, key=len)
```

```python
class Solution:
def compressString(self, S: str) -> str:
t = []
i, n = 0, len(S)
while i < n:
j = i + 1
while j < n and S[j] == S[i]:
j += 1
t.append(S[i] + str(j - i))
i = j
return min(S, "".join(t), key=len)
```

```java
class Solution {
public String compressString(String S) {
Expand Down Expand Up @@ -155,24 +169,4 @@ var compressString = function (S) {

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```python
class Solution:
def compressString(self, S: str) -> str:
t = []
i, n = 0, len(S)
while i < n:
j = i + 1
while j < n and S[j] == S[i]:
j += 1
t.append(S[i] + str(j - i))
i = j
return min(S, "".join(t), key=len)
```

<!-- tabs:end -->

<!-- end -->
34 changes: 14 additions & 20 deletions lcci/01.06.Compress String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ class Solution:
return min(S, t, key=len)
```

```python
class Solution:
def compressString(self, S: str) -> str:
t = []
i, n = 0, len(S)
while i < n:
j = i + 1
while j < n and S[j] == S[i]:
j += 1
t.append(S[i] + str(j - i))
i = j
return min(S, "".join(t), key=len)
```

```java
class Solution {
public String compressString(String S) {
Expand Down Expand Up @@ -161,24 +175,4 @@ var compressString = function (S) {

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```python
class Solution:
def compressString(self, S: str) -> str:
t = []
i, n = 0, len(S)
while i < n:
j = i + 1
while j < n and S[j] == S[i]:
j += 1
t.append(S[i] + str(j - i))
i = j
return min(S, "".join(t), key=len)
```

<!-- tabs:end -->

<!-- end -->
35 changes: 0 additions & 35 deletions lcci/01.09.String Rotation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,39 +104,4 @@ impl Solution {

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```rust
impl Solution {
pub fn is_fliped_string(s1: String, s2: String) -> bool {
if s1 == s2 {
return true;
}
if s1.len() != s2.len() {
return false;
}
let s2: Vec<char> = (s2.clone() + &s2).chars().collect();
let n = s1.len();
let m = s2.len();
for i in 0..m - n {
let mut is_pass = true;
for (j, c) in s1.chars().enumerate() {
if c != s2[i + j] {
is_pass = false;
break;
}
}
if is_pass {
return true;
}
}
false
}
}
```

<!-- tabs:end -->

<!-- end -->
35 changes: 0 additions & 35 deletions lcci/01.09.String Rotation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,39 +104,4 @@ impl Solution {

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```rust
impl Solution {
pub fn is_fliped_string(s1: String, s2: String) -> bool {
if s1 == s2 {
return true;
}
if s1.len() != s2.len() {
return false;
}
let s2: Vec<char> = (s2.clone() + &s2).chars().collect();
let n = s1.len();
let m = s2.len();
for i in 0..m - n {
let mut is_pass = true;
for (j, c) in s1.chars().enumerate() {
if c != s2[i + j] {
is_pass = false;
break;
}
}
if is_pass {
return true;
}
}
false
}
}
```

<!-- tabs:end -->

<!-- end -->
Loading