Skip to content

Commit

Permalink
feat: add solutions to lc project (doocs#2212)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Jan 13, 2024
1 parent 9b2c079 commit 7659a0c
Show file tree
Hide file tree
Showing 6,820 changed files with 128,614 additions and 60,941 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ node_modules/
/solution/bash_problem_readme_template.md
/solution/bash_problem_readme_template_en.md
/solution/0100-0199/0177.Nth Highest Salary/Solution.sql
/solution/0100-0199/0178.Rank Scores/Solution2.sql
/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution2.sql
/solution/1400-1499/1454.Active Users/Solution.sql
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
/solution/2100-2199/2118.Build the Equation/Solution.sql
Expand Down
25 changes: 25 additions & 0 deletions basic/sorting/BubbleSort/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <vector>

using namespace std;

void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
bool change = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
change = true;
}
}
if (!change) break;
}
}

int main() {
vector<int> arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
bubbleSort(arr);
for (int v : arr) cout << v << " ";
cout << endl;
}
45 changes: 45 additions & 0 deletions basic/sorting/BubbleSort/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using static System.Console;
namespace Pro;
public class Program
{
public static void Main()
{
int[] test = new int[] { 56, 876, 34, 23, 45, 501, 2, 3, 4, 6, 5, 7, 8, 9, 11, 10, 12, 23, 34 };
BubbleSortNums(test);
foreach (var item in test)
{
WriteLine(item);
}
ReadLine();
}
public static void BubbleSortNums(int[] nums)
{
int numchange = 0;
for (int initial = 0; initial < nums.Length - numchange; initial++)
{
WriteLine($"{initial} start ");
// 记录此值 用于迭代开始位置
bool changelog = false;
for (int second_sortnum = initial; second_sortnum < nums.Length - 1; second_sortnum++)
{
if (nums[second_sortnum] > nums[second_sortnum + 1])
{
swap(ref nums[second_sortnum], ref nums[second_sortnum + 1]);
if (!changelog)
{
// 记录转换的位置,让initial开始位置从转换位置前开始
initial = ((second_sortnum - 2) > 0) ? (second_sortnum - 2) : -1;
numchange += 1;
}
changelog = true;
}
}
}
}
private static void swap(ref int compare_left, ref int compare_right)
{
int temp = compare_left;
compare_left = compare_right;
compare_right = temp;
}
}
22 changes: 22 additions & 0 deletions basic/sorting/BubbleSort/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

func bubbleSort(nums []int) {
hasChange := true
for i, n := 0, len(nums); i < n-1 && hasChange; i++ {
hasChange = false
for j := 0; j < n-i-1; j++ {
if nums[j] > nums[j+1] {
nums[j], nums[j+1] = nums[j+1], nums[j]
hasChange = true
}
}
}
}

func main() {
nums := []int{1, 2, 7, 9, 5, 8}
bubbleSort(nums)
fmt.Println(nums)
}
29 changes: 29 additions & 0 deletions basic/sorting/BubbleSort/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Arrays;

public class BubbleSort {

private static void bubbleSort(int[] nums) {
boolean hasChange = true;
for (int i = 0, n = nums.length; i < n - 1 && hasChange; ++i) {
hasChange = false;
for (int j = 0; j < n - i - 1; ++j) {
if (nums[j] > nums[j + 1]) {
swap(nums, j, j + 1);
hasChange = true;
}
}
}
}

private static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}

public static void main(String[] args) {
int[] nums = {1, 2, 7, 9, 5, 8};
bubbleSort(nums);
System.out.println(Arrays.toString(nums));
}
}
22 changes: 22 additions & 0 deletions basic/sorting/BubbleSort/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function bubbleSort(inputArr) {
for (let i = inputArr.length - 1; i > 0; i--) {
let hasChange = false;
for (let j = 0; j < i; j++) {
if (inputArr[j] > inputArr[j + 1]) {
const temp = inputArr[j];
inputArr[j] = inputArr[j + 1];
inputArr[j + 1] = temp;
hasChange = true;
}
}

if (!hasChange) {
break;
}
}

return inputArr;
}

const arr = [6, 3, 2, 1, 5];
console.log(bubbleSort(arr));
26 changes: 26 additions & 0 deletions basic/sorting/BubbleSort/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def bubbleSort(arr):
n = len(arr)
# Iterate over all array elements
for i in range(n):
# Last i elements are already in place
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]


# 改进版本
def bubbleSort(arr):
n = len(arr)
for i in range(n - 1):
has_change = False
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
has_change = True
if not has_change:
break


arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print(arr)
18 changes: 18 additions & 0 deletions basic/sorting/BubbleSort/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn bubble_sort(nums: &mut Vec<i32>) {
let n = nums.len();
for i in 0..n - 1 {
for j in i..n {
if nums[i] > nums[j] {
let temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
}

fn main() {
let mut nums = vec![1, 2, 7, 9, 5, 8];
bubble_sort(&mut nums);
println!("{:?}", nums);
}
51 changes: 51 additions & 0 deletions basic/sorting/HeapSort/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import "fmt"

const N = 100010

var (
size int
h []int
)

func up(u int) {
for u/2 != 0 && h[u/2] > h[u] { //父节点比当前结点小
h[u/2], h[u] = h[u], h[u/2] //交换
u /= 2
}
}
func down(u int) {
t := u //t 最小值
if u*2 <= size && h[2*u] < h[t] { //左孩子存在且小于t
t = u * 2
}
if u*2+1 <= size && h[2*u+1] < h[t] { //右孩子存在且小于t
t = 2*u + 1
}
if u != t {
h[u], h[t] = h[t], h[u]
down(t) //递归处理
}
}
func main() {
var n, m int
h = make([]int, N)
fmt.Scanf("%d%d", &n, &m)
//创建一维数组1
for i := 1; i <= n; i++ {
fmt.Scanf("%d", &h[i])
}
size = n
// 一维数组变为小根堆
for i := n / 2; i != 0; i-- {
down(i)
}

for ; m != 0; m-- {
fmt.Printf("%d ", h[1])
h[1] = h[size]
size--
down(1)
}
}
50 changes: 50 additions & 0 deletions basic/sorting/HeapSort/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.Scanner;

public class Main {
private static int[] h = new int[100010];
private static int size;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
for (int i = 1; i <= n; ++i) {
h[i] = sc.nextInt();
}
size = n;
for (int i = n / 2; i > 0; --i) {
down(i);
}
while (m-- > 0) {
System.out.print(h[1] + " ");
h[1] = h[size--];
down(1);
}
}

public static void down(int u) {
int t = u;
if (u * 2 <= size && h[u * 2] < h[t]) {
t = u * 2;
}
if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) {
t = u * 2 + 1;
}
if (t != u) {
swap(t, u);
down(t);
}
}

public static void up(int u) {
while (u / 2 > 0 && h[u / 2] > h[u]) {
swap(u / 2, u);
u /= 2;
}
}

public static void swap(int i, int j) {
int t = h[i];
h[i] = h[j];
h[j] = t;
}
}
34 changes: 34 additions & 0 deletions basic/sorting/HeapSort/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
n, m = list(map(int, input().split(" ")))
h = [0] + list(map(int, input().split(" ")))

size = n


def down(u):
t = u
if u * 2 <= size and h[u * 2] < h[t]:
t = u * 2
if u * 2 + 1 <= size and h[u * 2 + 1] < h[t]:
t = u * 2 + 1
if t != u:
h[t], h[u] = h[u], h[t]
down(t)


def up(u):
while u // 2 > 0 and h[u // 2] > h[u]:
h[u // 2], h[u] = h[u], h[u // 2]
u //= 2


for i in range(n // 2, 0, -1):
down(i)

res = []
for i in range(m):
res.append(h[1])
h[1] = h[size]
size -= 1
down(1)

print(' '.join(list(map(str, res))))
Loading

0 comments on commit 7659a0c

Please sign in to comment.