Skip to content

Commit 7659a0c

Browse files
authored
feat: add solutions to lc project (doocs#2212)
1 parent 9b2c079 commit 7659a0c

File tree

6,820 files changed

+128614
-60941
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

6,820 files changed

+128614
-60941
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ node_modules/
1414
/solution/bash_problem_readme_template.md
1515
/solution/bash_problem_readme_template_en.md
1616
/solution/0100-0199/0177.Nth Highest Salary/Solution.sql
17+
/solution/0100-0199/0178.Rank Scores/Solution2.sql
18+
/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution2.sql
1719
/solution/1400-1499/1454.Active Users/Solution.sql
1820
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
1921
/solution/2100-2199/2118.Build the Equation/Solution.sql

basic/sorting/BubbleSort/Solution.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void bubbleSort(vector<int>& arr) {
7+
int n = arr.size();
8+
for (int i = 0; i < n - 1; ++i) {
9+
bool change = false;
10+
for (int j = 0; j < n - i - 1; ++j) {
11+
if (arr[j] > arr[j + 1]) {
12+
swap(arr[j], arr[j + 1]);
13+
change = true;
14+
}
15+
}
16+
if (!change) break;
17+
}
18+
}
19+
20+
int main() {
21+
vector<int> arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
22+
bubbleSort(arr);
23+
for (int v : arr) cout << v << " ";
24+
cout << endl;
25+
}

basic/sorting/BubbleSort/Solution.cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using static System.Console;
2+
namespace Pro;
3+
public class Program
4+
{
5+
public static void Main()
6+
{
7+
int[] test = new int[] { 56, 876, 34, 23, 45, 501, 2, 3, 4, 6, 5, 7, 8, 9, 11, 10, 12, 23, 34 };
8+
BubbleSortNums(test);
9+
foreach (var item in test)
10+
{
11+
WriteLine(item);
12+
}
13+
ReadLine();
14+
}
15+
public static void BubbleSortNums(int[] nums)
16+
{
17+
int numchange = 0;
18+
for (int initial = 0; initial < nums.Length - numchange; initial++)
19+
{
20+
WriteLine($"{initial} start ");
21+
// 记录此值 用于迭代开始位置
22+
bool changelog = false;
23+
for (int second_sortnum = initial; second_sortnum < nums.Length - 1; second_sortnum++)
24+
{
25+
if (nums[second_sortnum] > nums[second_sortnum + 1])
26+
{
27+
swap(ref nums[second_sortnum], ref nums[second_sortnum + 1]);
28+
if (!changelog)
29+
{
30+
// 记录转换的位置,让initial开始位置从转换位置前开始
31+
initial = ((second_sortnum - 2) > 0) ? (second_sortnum - 2) : -1;
32+
numchange += 1;
33+
}
34+
changelog = true;
35+
}
36+
}
37+
}
38+
}
39+
private static void swap(ref int compare_left, ref int compare_right)
40+
{
41+
int temp = compare_left;
42+
compare_left = compare_right;
43+
compare_right = temp;
44+
}
45+
}

basic/sorting/BubbleSort/Solution.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func bubbleSort(nums []int) {
6+
hasChange := true
7+
for i, n := 0, len(nums); i < n-1 && hasChange; i++ {
8+
hasChange = false
9+
for j := 0; j < n-i-1; j++ {
10+
if nums[j] > nums[j+1] {
11+
nums[j], nums[j+1] = nums[j+1], nums[j]
12+
hasChange = true
13+
}
14+
}
15+
}
16+
}
17+
18+
func main() {
19+
nums := []int{1, 2, 7, 9, 5, 8}
20+
bubbleSort(nums)
21+
fmt.Println(nums)
22+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Arrays;
2+
3+
public class BubbleSort {
4+
5+
private static void bubbleSort(int[] nums) {
6+
boolean hasChange = true;
7+
for (int i = 0, n = nums.length; i < n - 1 && hasChange; ++i) {
8+
hasChange = false;
9+
for (int j = 0; j < n - i - 1; ++j) {
10+
if (nums[j] > nums[j + 1]) {
11+
swap(nums, j, j + 1);
12+
hasChange = true;
13+
}
14+
}
15+
}
16+
}
17+
18+
private static void swap(int[] nums, int i, int j) {
19+
int t = nums[i];
20+
nums[i] = nums[j];
21+
nums[j] = t;
22+
}
23+
24+
public static void main(String[] args) {
25+
int[] nums = {1, 2, 7, 9, 5, 8};
26+
bubbleSort(nums);
27+
System.out.println(Arrays.toString(nums));
28+
}
29+
}

basic/sorting/BubbleSort/Solution.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function bubbleSort(inputArr) {
2+
for (let i = inputArr.length - 1; i > 0; i--) {
3+
let hasChange = false;
4+
for (let j = 0; j < i; j++) {
5+
if (inputArr[j] > inputArr[j + 1]) {
6+
const temp = inputArr[j];
7+
inputArr[j] = inputArr[j + 1];
8+
inputArr[j + 1] = temp;
9+
hasChange = true;
10+
}
11+
}
12+
13+
if (!hasChange) {
14+
break;
15+
}
16+
}
17+
18+
return inputArr;
19+
}
20+
21+
const arr = [6, 3, 2, 1, 5];
22+
console.log(bubbleSort(arr));

basic/sorting/BubbleSort/Solution.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def bubbleSort(arr):
2+
n = len(arr)
3+
# Iterate over all array elements
4+
for i in range(n):
5+
# Last i elements are already in place
6+
for j in range(n - i - 1):
7+
if arr[j] > arr[j + 1]:
8+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
9+
10+
11+
# 改进版本
12+
def bubbleSort(arr):
13+
n = len(arr)
14+
for i in range(n - 1):
15+
has_change = False
16+
for j in range(n - i - 1):
17+
if arr[j] > arr[j + 1]:
18+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
19+
has_change = True
20+
if not has_change:
21+
break
22+
23+
24+
arr = [64, 34, 25, 12, 22, 11, 90]
25+
bubbleSort(arr)
26+
print(arr)

basic/sorting/BubbleSort/Solution.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn bubble_sort(nums: &mut Vec<i32>) {
2+
let n = nums.len();
3+
for i in 0..n - 1 {
4+
for j in i..n {
5+
if nums[i] > nums[j] {
6+
let temp = nums[i];
7+
nums[i] = nums[j];
8+
nums[j] = temp;
9+
}
10+
}
11+
}
12+
}
13+
14+
fn main() {
15+
let mut nums = vec![1, 2, 7, 9, 5, 8];
16+
bubble_sort(&mut nums);
17+
println!("{:?}", nums);
18+
}

basic/sorting/HeapSort/Solution.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import "fmt"
4+
5+
const N = 100010
6+
7+
var (
8+
size int
9+
h []int
10+
)
11+
12+
func up(u int) {
13+
for u/2 != 0 && h[u/2] > h[u] { //父节点比当前结点小
14+
h[u/2], h[u] = h[u], h[u/2] //交换
15+
u /= 2
16+
}
17+
}
18+
func down(u int) {
19+
t := u //t 最小值
20+
if u*2 <= size && h[2*u] < h[t] { //左孩子存在且小于t
21+
t = u * 2
22+
}
23+
if u*2+1 <= size && h[2*u+1] < h[t] { //右孩子存在且小于t
24+
t = 2*u + 1
25+
}
26+
if u != t {
27+
h[u], h[t] = h[t], h[u]
28+
down(t) //递归处理
29+
}
30+
}
31+
func main() {
32+
var n, m int
33+
h = make([]int, N)
34+
fmt.Scanf("%d%d", &n, &m)
35+
//创建一维数组1
36+
for i := 1; i <= n; i++ {
37+
fmt.Scanf("%d", &h[i])
38+
}
39+
size = n
40+
// 一维数组变为小根堆
41+
for i := n / 2; i != 0; i-- {
42+
down(i)
43+
}
44+
45+
for ; m != 0; m-- {
46+
fmt.Printf("%d ", h[1])
47+
h[1] = h[size]
48+
size--
49+
down(1)
50+
}
51+
}

basic/sorting/HeapSort/Solution.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
private static int[] h = new int[100010];
5+
private static int size;
6+
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt(), m = sc.nextInt();
10+
for (int i = 1; i <= n; ++i) {
11+
h[i] = sc.nextInt();
12+
}
13+
size = n;
14+
for (int i = n / 2; i > 0; --i) {
15+
down(i);
16+
}
17+
while (m-- > 0) {
18+
System.out.print(h[1] + " ");
19+
h[1] = h[size--];
20+
down(1);
21+
}
22+
}
23+
24+
public static void down(int u) {
25+
int t = u;
26+
if (u * 2 <= size && h[u * 2] < h[t]) {
27+
t = u * 2;
28+
}
29+
if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) {
30+
t = u * 2 + 1;
31+
}
32+
if (t != u) {
33+
swap(t, u);
34+
down(t);
35+
}
36+
}
37+
38+
public static void up(int u) {
39+
while (u / 2 > 0 && h[u / 2] > h[u]) {
40+
swap(u / 2, u);
41+
u /= 2;
42+
}
43+
}
44+
45+
public static void swap(int i, int j) {
46+
int t = h[i];
47+
h[i] = h[j];
48+
h[j] = t;
49+
}
50+
}

basic/sorting/HeapSort/Solution.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
n, m = list(map(int, input().split(" ")))
2+
h = [0] + list(map(int, input().split(" ")))
3+
4+
size = n
5+
6+
7+
def down(u):
8+
t = u
9+
if u * 2 <= size and h[u * 2] < h[t]:
10+
t = u * 2
11+
if u * 2 + 1 <= size and h[u * 2 + 1] < h[t]:
12+
t = u * 2 + 1
13+
if t != u:
14+
h[t], h[u] = h[u], h[t]
15+
down(t)
16+
17+
18+
def up(u):
19+
while u // 2 > 0 and h[u // 2] > h[u]:
20+
h[u // 2], h[u] = h[u], h[u // 2]
21+
u //= 2
22+
23+
24+
for i in range(n // 2, 0, -1):
25+
down(i)
26+
27+
res = []
28+
for i in range(m):
29+
res.append(h[1])
30+
h[1] = h[size]
31+
size -= 1
32+
down(1)
33+
34+
print(' '.join(list(map(str, res))))

0 commit comments

Comments
 (0)