Skip to content

Commit 5eee963

Browse files
authored
Merge pull request doocs#4 from doocs/master
merge
2 parents 0a8700c + 64e0170 commit 5eee963

File tree

14 files changed

+276
-6
lines changed

14 files changed

+276
-6
lines changed

.github/workflows/sync.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
build:
99
runs-on: ubuntu-latest
1010
steps:
11-
- name: Sync to Gitee
11+
- name: Sync to gitee.com
1212
uses: wearerequired/git-mirror-action@master
1313
env:
14-
SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }}
14+
SSH_PRIVATE_KEY: ${{ secrets.RSA_PRIVATE_KEY }}
1515
with:
1616
source-repo: "[email protected]:doocs/leetcode.git"
1717
destination-repo: "[email protected]:Doocs/leetcode.git"

basic/sort/QuickSort.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Arrays;
2+
3+
public class QuickSort {
4+
5+
private static void quickSort(int[] nums) {
6+
quickSort(nums, 0, nums.length - 1);
7+
}
8+
9+
private static void quickSort(int[] nums, int low, int high) {
10+
if (low >= high) {
11+
return;
12+
}
13+
int[] p = partition(nums, low, high);
14+
quickSort(nums, low, p[0] - 1);
15+
quickSort(nums, p[0] + 1, high);
16+
}
17+
18+
private static int[] partition(int[] nums, int low, int high) {
19+
int less = low - 1, more = high;
20+
21+
while (low < more) {
22+
if (nums[low] < nums[high]) {
23+
swap(nums, ++less, low++);
24+
} else if (nums[low] > nums[high]) {
25+
swap(nums, --more, low);
26+
} else {
27+
++low;
28+
}
29+
}
30+
swap(nums, more, high);
31+
return new int[] {less + 1, more};
32+
}
33+
34+
private static void swap(int[] nums, int i, int j) {
35+
int t = nums[i];
36+
nums[i] = nums[j];
37+
nums[j] = t;
38+
}
39+
40+
public static void main(String[] args) {
41+
int[] nums = {1, 2, 7, 4, 5, 3};
42+
quickSort(nums);
43+
System.out.println(Arrays.toString(nums));
44+
}
45+
}

lcci/01.02.Check Permutation/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ class Solution:
4141
<!-- 这里可写当前语言的特殊实现逻辑 -->
4242

4343
```java
44-
44+
class Solution {
45+
public boolean checkPermutation(String s1, String s2) {
46+
if (s1 == null || s2 == null || s1.length() != s2.length()) {
47+
return false;
48+
}
49+
char[] c1 = s1.toCharArray();
50+
char[] c2 = s2.toCharArray();
51+
Arrays.sort(c1);
52+
Arrays.sort(c2);
53+
return Arrays.equals(c1, c2);
54+
}
55+
}
4556
```
4657

4758
### ...

lcci/01.02.Check Permutation/README_EN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ class Solution:
5656
### Java
5757

5858
```java
59-
59+
class Solution {
60+
public boolean CheckPermutation(String s1, String s2) {
61+
if (s1 == null || s2 == null || s1.length() != s2.length()) {
62+
return false;
63+
}
64+
char[] c1 = s1.toCharArray();
65+
char[] c2 = s2.toCharArray();
66+
Arrays.sort(c1);
67+
Arrays.sort(c2);
68+
return Arrays.equals(c1, c2);
69+
}
70+
}
6071
```
6172

6273
### ...
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean CheckPermutation(String s1, String s2) {
3+
if (s1 == null || s2 == null || s1.length() != s2.length()) {
4+
return false;
5+
}
6+
char[] c1 = s1.toCharArray();
7+
char[] c2 = s2.toCharArray();
8+
Arrays.sort(c1);
9+
Arrays.sort(c2);
10+
return Arrays.equals(c1, c2);
11+
}
12+
}

lcci/01.03.String to URL/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@ class Solution:
4141
<!-- 这里可写当前语言的特殊实现逻辑 -->
4242

4343
```java
44-
44+
class Solution {
45+
public String replaceSpaces(String S, int length) {
46+
char[] c = S.toCharArray();
47+
int j = c.length;
48+
for (int i = length - 1; i >= 0; i--) {
49+
if (c[i] == ' ') {
50+
c[--j] = '0';
51+
c[--j] = '2';
52+
c[--j] = '%';
53+
} else {
54+
c[--j] = c[i];
55+
}
56+
}
57+
return new String(c, j, c.length - j);
58+
}
59+
}
4560
```
4661

4762
### ...

lcci/01.03.String to URL/README_EN.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,22 @@ class Solution:
6767
### Java
6868

6969
```java
70-
70+
class Solution {
71+
public String replaceSpaces(String S, int length) {
72+
char[] c = S.toCharArray();
73+
int j = c.length;
74+
for (int i = length - 1; i >= 0; i--) {
75+
if (c[i] == ' ') {
76+
c[--j] = '0';
77+
c[--j] = '2';
78+
c[--j] = '%';
79+
} else {
80+
c[--j] = c[i];
81+
}
82+
}
83+
return new String(c, j, c.length - j);
84+
}
85+
}
7186
```
7287

7388
### ...
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public String replaceSpaces(String S, int length) {
3+
char[] c = S.toCharArray();
4+
int j = c.length;
5+
for (int i = length - 1; i >= 0; i--) {
6+
if (c[i] == ' ') {
7+
c[--j] = '0';
8+
c[--j] = '2';
9+
c[--j] = '%';
10+
} else {
11+
c[--j] = c[i];
12+
}
13+
}
14+
return new String(c, j, c.length - j);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution
2+
{
3+
public:
4+
vector<int> spiralOrder(vector<vector<int>> &matrix)
5+
{
6+
vector<int> ans;
7+
if (matrix.size() == 0)
8+
return ans;
9+
int left = 0, top = 0, bottom = matrix.size() - 1, right = matrix[0].size() - 1;
10+
while (true)
11+
{
12+
for (int i = left; i <= right; i++)
13+
ans.push_back(matrix[top][i]);
14+
top++;
15+
if (top > bottom)
16+
break;
17+
for (int i = top; i <= bottom; i++)
18+
ans.push_back(matrix[i][right]);
19+
right--;
20+
if (right < left)
21+
break;
22+
for (int i = right; i >= left; i--)
23+
ans.push_back(matrix[bottom][i]);
24+
bottom--;
25+
if (bottom < top)
26+
break;
27+
for (int i = bottom; i >= top; i--)
28+
ans.push_back(matrix[i][left]);
29+
left++;
30+
if (left > right)
31+
break;
32+
}
33+
return ans;
34+
}
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
impl Solution {
2+
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
3+
let mut ans=Vec::new();
4+
if matrix.len()==0{
5+
return ans;
6+
}
7+
let (mut left,mut right,mut top,mut bottom)=(0,matrix[0].len()-1,0,matrix.len()-1);
8+
loop{
9+
for i in left..right+1{
10+
ans.push(matrix[top][i]);
11+
}
12+
top+=1;
13+
if (top as i32)>(bottom as i32){
14+
break;
15+
}
16+
for i in top..bottom+1{
17+
ans.push(matrix[i][right]);
18+
}
19+
right-=1;
20+
if (right as i32)<(left as i32){
21+
break;
22+
}
23+
for i in (left..right+1).rev(){
24+
ans.push(matrix[bottom][i]);
25+
}
26+
bottom-=1;
27+
if (bottom as i32)<(top as i32){
28+
break;
29+
}
30+
for i in (top..bottom+1).rev(){
31+
ans.push(matrix[i][left]);
32+
}
33+
left+=1;
34+
if (left as i32)>(right as i32){
35+
break;
36+
}
37+
}
38+
ans
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var spiralOrder = (matrix: number[][]): number[] => {
2+
let ans: number[] = [];
3+
if (matrix.length === 0) return ans;
4+
let top = 0,
5+
left = 0,
6+
bottom = matrix.length - 1,
7+
right = matrix[0].length - 1;
8+
while (true) {
9+
for (let i = left; i <= right; i++) ans.push(matrix[top][i]);
10+
top++;
11+
if (top > bottom) break;
12+
for (let i = top; i <= bottom; i++) ans.push(matrix[i][right]);
13+
right--;
14+
if (right < left) break;
15+
for (let i = right; i >= left; i--) ans.push(matrix[bottom][i]);
16+
bottom--;
17+
if (bottom < top) break;
18+
for (let i = bottom; i >= top; i--) ans.push(matrix[i][left]);
19+
left++;
20+
if (left > right) break;
21+
}
22+
return ans;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution
2+
{
3+
public:
4+
vector<int> productExceptSelf(vector<int> &nums)
5+
{
6+
vector<int> dpLeft(nums.size(), 1);
7+
vector<int> dpRight(nums.size(), 1);
8+
for (int i = 1; i < nums.size(); i++)
9+
{
10+
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
11+
}
12+
for (int i = nums.size() - 2; i >= 0; i--)
13+
{
14+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
15+
}
16+
vector<int> result;
17+
for (int i = 0; i < nums.size(); i++)
18+
{
19+
result.push_back(dpLeft[i] * dpRight[i]);
20+
}
21+
return result;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
3+
let mut dp_left=vec![1_i32;nums.len()];
4+
let mut dp_right=vec![1_i32;nums.len()];
5+
for i in 1..nums.len(){
6+
dp_left[i]=dp_left[i-1]*nums[i-1];
7+
}
8+
for i in (0..(nums.len()-1)).rev(){
9+
dp_right[i]=dp_right[i+1]*nums[i+1];
10+
}
11+
dp_left.into_iter().enumerate().map(|(i,x)| x*dp_right[i]).collect()
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function productExceptSelf(nums: number[]): number[] {
2+
let dpLeft = Array(nums.length).fill(1);
3+
let dpRight = Array(nums.length).fill(1);
4+
for (let i = 1; i < nums.length; i++) {
5+
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
6+
}
7+
for (let i = nums.length - 2; i >= 0; i--) {
8+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
9+
}
10+
return dpLeft.map((x, i) => x * dpRight[i]);
11+
}

0 commit comments

Comments
 (0)