init commit
This commit is contained in:
commit
199bbf2628
20
.gitignore
vendored
Normal file
20
.gitignore
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Dependencies
|
||||||
|
/node_modules
|
||||||
|
|
||||||
|
# Production
|
||||||
|
/build
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
.docusaurus
|
||||||
|
.cache-loader
|
||||||
|
|
||||||
|
# Misc
|
||||||
|
.DS_Store
|
||||||
|
.env.local
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
5
.markdownlint.json
Normal file
5
.markdownlint.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"MD025": false,
|
||||||
|
"MD033": false,
|
||||||
|
"MD045": false
|
||||||
|
}
|
48
README.md
Normal file
48
README.md
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# BLOG
|
||||||
|
|
||||||
|
好记性不如烂笔头,自从工作以后写得越来越少了,在大学时还时不时的写点,现在回顾起来,感觉不到工作后的任何积累,写作水平也比以前差了不少,实在惭愧。
|
||||||
|
|
||||||
|
自前段时间看了大佬们的博客,下定决心重新开写,重新整理了一下过往的文章,~~黑历史去掉~~,然后重新挑选了一个博客程序,你感兴趣的话可以看看[blog](https://github.com/codfrm/blog)仓库,这个仓库之前也有一些代码记录,已经移动到了[archive](./archive)目录下。
|
||||||
|
|
||||||
|
整理时,回头看之前的文章,能感受到自己的一些技术进步,也能看到之前的一些感悟和中二(~~黑历史~~),从当年的小白变成了现在的中白,等以后成为了老白,再回头看看,应该也会有不一样的感悟。
|
||||||
|
|
||||||
|
我将以前的一些我觉得有意义的文章搬到了这里,日期应该都是 **2024 年 03 月 17 日**,重新进行了分类:[`DEV`](./docs/dev/)编程语言、软件开发;[`OPS`](./docs/ops/)服务器运维、工具分享;[`随笔`](./docs/note/)日常生活、感悟。
|
||||||
|
|
||||||
|
写博客其实也不止是为了记录,更多的是为了学习、为了分享、为了交流,也是为了能够在互联网上留下一片足迹,希望能够坚持下去。
|
||||||
|
|
||||||
|
如果你对本博客程序有兴趣,可以看看我这篇文章:[我为什么换掉 Wordpress 而选择 Docusaurus?](./docs/note/为什么换掉Wordpress.md)
|
||||||
|
如果你有其它问题,欢迎在文章下面留言,或者在[GitHub](https://github.com/codfrm/blog)上提 issue,我会尽快回复。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
```go title="main.go"
|
||||||
|
fmt.Println("Hello, World!")
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts title="main.ts"
|
||||||
|
console.log("Hello, World!");
|
||||||
|
```
|
||||||
|
|
||||||
|
```php title="main.php"
|
||||||
|
echo "Hello, World!";
|
||||||
|
```
|
||||||
|
|
||||||
|
```python title="main.py"
|
||||||
|
print('Hello, World!')
|
||||||
|
```
|
||||||
|
|
||||||
|
```c title="main.c"
|
||||||
|
printf("Hello, World!");
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp title="main.cpp"
|
||||||
|
std::cout << "Hello, World!";
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash title="main.sh"
|
||||||
|
echo "Hello, World!"
|
||||||
|
```
|
||||||
|
|
||||||
|
```docker title="Dockerfile"
|
||||||
|
RUN echo "Hello, World!"
|
||||||
|
```
|
30
archive/.gitignore
vendored
Normal file
30
archive/.gitignore
vendored
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
*.o
|
||||||
|
debug/*
|
||||||
|
debug
|
||||||
|
.vscode/ipch
|
||||||
|
# Created by https://www.gitignore.io/api/go
|
||||||
|
# Edit at https://www.gitignore.io/?templates=go
|
||||||
|
.idea
|
||||||
|
|
||||||
|
### Go ###
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
### Go Patch ###
|
||||||
|
/vendor/
|
||||||
|
/Godeps/
|
||||||
|
|
||||||
|
# End of https://www.gitignore.io/api/go
|
17
archive/.vscode/c_cpp_properties.json
vendored
Normal file
17
archive/.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"/usr/include/**"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/bin/gcc",
|
||||||
|
"cStandard": "c11",
|
||||||
|
"cppStandard": "c++17",
|
||||||
|
"intelliSenseMode": "gcc-x64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
59
archive/.vscode/launch.json
vendored
Normal file
59
archive/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
{
|
||||||
|
// 使用 IntelliSense 了解相关属性。
|
||||||
|
// 悬停以查看现有属性的描述。
|
||||||
|
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "go",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${fileDirname}",
|
||||||
|
"env": {},
|
||||||
|
"args": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Debug_win_x86",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}\\debug\\${fileBasenameNoExtension}.exe",
|
||||||
|
"args": [],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": true,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"targetArchitecture": "x86",
|
||||||
|
"miDebuggerPath": "gdb.exe",
|
||||||
|
"preLaunchTask": "debug_win",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Debug_linux_x86",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}\\debug\\${fileBasenameNoExtension}",
|
||||||
|
"args": [],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": false,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"preLaunchTask": "debug",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
44
archive/.vscode/settings.json
vendored
Normal file
44
archive/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"new": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"random": "c"
|
||||||
|
}
|
||||||
|
}
|
60
archive/.vscode/tasks.json
vendored
Normal file
60
archive/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
{
|
||||||
|
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||||
|
// for the documentation about the tasks.json format
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "debug",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"DEBUG=TRUE",
|
||||||
|
"NAME=${fileBasenameNoExtension}",
|
||||||
|
"FILE=\"${file}\""
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": {
|
||||||
|
"owner": "cpp",
|
||||||
|
"fileLocation": [
|
||||||
|
"relative",
|
||||||
|
"${workspaceRoot}"
|
||||||
|
],
|
||||||
|
"pattern": {
|
||||||
|
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
|
||||||
|
"file": 1,
|
||||||
|
"line": 2,
|
||||||
|
"column": 3,
|
||||||
|
"severity": 4,
|
||||||
|
"message": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},{
|
||||||
|
"label": "debug_win",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "mingw32-make",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"DEBUG=TRUE",
|
||||||
|
"NAME=${fileBasenameNoExtension}",
|
||||||
|
"FILE=\"${file}\""
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": {
|
||||||
|
"owner": "cpp",
|
||||||
|
"fileLocation": [
|
||||||
|
"relative",
|
||||||
|
"${workspaceRoot}"
|
||||||
|
],
|
||||||
|
"pattern": {
|
||||||
|
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
|
||||||
|
"file": 1,
|
||||||
|
"line": 2,
|
||||||
|
"column": 3,
|
||||||
|
"severity": 4,
|
||||||
|
"message": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
20
archive/Makefile
Normal file
20
archive/Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
export DEBUG
|
||||||
|
# -fexec-charset=GBK
|
||||||
|
g=gcc
|
||||||
|
ifeq ($(DEBUG), TRUE)
|
||||||
|
g+= -g
|
||||||
|
endif
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(g) -g -fsanitize=address "$(FILE)" -o debug/$(NAME) -lm
|
||||||
|
|
||||||
|
rebuild:clean build
|
||||||
|
|
||||||
|
test:test.o
|
||||||
|
$(g) debug/test.o -o debug/test
|
||||||
|
|
||||||
|
test.o:
|
||||||
|
$(g) -c test.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm debug/$(NAME).o debug/$(NAME) debug/$(NAME).h.gch debug/$(NAME).exe
|
6
archive/algorithm/.gitignore
vendored
Normal file
6
archive/algorithm/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
*.exe
|
||||||
|
*.o
|
||||||
|
debug/*
|
||||||
|
debug
|
||||||
|
.vscode/ipch
|
||||||
|
.idea
|
50
archive/algorithm/.vscode/launch.json
vendored
Normal file
50
archive/algorithm/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
{
|
||||||
|
// 使用 IntelliSense 了解相关属性。
|
||||||
|
// 悬停以查看现有属性的描述。
|
||||||
|
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Debug_win_x86",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}\\debug\\${fileBasenameNoExtension}.exe",
|
||||||
|
"args": [],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": true,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"targetArchitecture": "x86",
|
||||||
|
"miDebuggerPath": "gdb.exe",
|
||||||
|
"preLaunchTask": "debug_win",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Debug_linux_x86",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}\\debug\\${fileBasenameNoExtension}",
|
||||||
|
"args": [],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": false,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"preLaunchTask": "debug",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
13
archive/algorithm/.vscode/settings.json
vendored
Normal file
13
archive/algorithm/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"stdio.h": "c",
|
||||||
|
"u_link_list.h": "c",
|
||||||
|
"math.h": "c",
|
||||||
|
"malloc.h": "c",
|
||||||
|
"string.h": "c",
|
||||||
|
"stdlib.h": "c",
|
||||||
|
"cmath": "c",
|
||||||
|
"limits": "c"
|
||||||
|
},
|
||||||
|
"C_Cpp.errorSquiggles": "Disabled"
|
||||||
|
}
|
60
archive/algorithm/.vscode/tasks.json
vendored
Normal file
60
archive/algorithm/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
{
|
||||||
|
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||||
|
// for the documentation about the tasks.json format
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "debug",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"DEBUG=TRUE",
|
||||||
|
"NAME=${fileBasenameNoExtension}",
|
||||||
|
"FILE=\"${file}\""
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": {
|
||||||
|
"owner": "cpp",
|
||||||
|
"fileLocation": [
|
||||||
|
"relative",
|
||||||
|
"${workspaceRoot}"
|
||||||
|
],
|
||||||
|
"pattern": {
|
||||||
|
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
|
||||||
|
"file": 1,
|
||||||
|
"line": 2,
|
||||||
|
"column": 3,
|
||||||
|
"severity": 4,
|
||||||
|
"message": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},{
|
||||||
|
"label": "debug_win",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "mingw32-make",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"DEBUG=TRUE",
|
||||||
|
"NAME=${fileBasenameNoExtension}",
|
||||||
|
"FILE=\"${file}\""
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"problemMatcher": {
|
||||||
|
"owner": "cpp",
|
||||||
|
"fileLocation": [
|
||||||
|
"relative",
|
||||||
|
"${workspaceRoot}"
|
||||||
|
],
|
||||||
|
"pattern": {
|
||||||
|
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
|
||||||
|
"file": 1,
|
||||||
|
"line": 2,
|
||||||
|
"column": 3,
|
||||||
|
"severity": 4,
|
||||||
|
"message": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
49
archive/algorithm/README.md
Normal file
49
archive/algorithm/README.md
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# 数据结构和算法
|
||||||
|
> 学习一些基本的数据结构和算法
|
||||||
|
|
||||||
|
# 环境
|
||||||
|
* vscode
|
||||||
|
* Windows 10
|
||||||
|
* MinGW
|
||||||
|
> 开始使用linux,配置改为
|
||||||
|
* vscode
|
||||||
|
* Ubuntu 18.04
|
||||||
|
* gcc 7.3 make 4.1
|
||||||
|
> 开始使用golang,c缺少太多基本的数据结构了
|
||||||
|
* vscode
|
||||||
|
* go 12
|
||||||
|
|
||||||
|
# 使用
|
||||||
|
已经配置好,直接用vscode打开,打开一个.c文件,按下f5就可以调试了(调试的是当前的文件)
|
||||||
|
|
||||||
|
golang 使用vscode插件`run code`
|
||||||
|
# 目录
|
||||||
|
|
||||||
|
## algorithm 排序算法
|
||||||
|
```
|
||||||
|
bubble 冒泡
|
||||||
|
radix_sort.c 基数排序
|
||||||
|
```
|
||||||
|
|
||||||
|
## problem 解决问题
|
||||||
|
```
|
||||||
|
calculator 后缀表达式
|
||||||
|
```
|
||||||
|
|
||||||
|
## struct 数据结构
|
||||||
|
```
|
||||||
|
adt 抽象数据类型
|
||||||
|
tree 树
|
||||||
|
```
|
||||||
|
|
||||||
|
## leetcode
|
||||||
|
|
||||||
|
# 其他目录
|
||||||
|
|
||||||
|
## tests 测试目录
|
||||||
|
```
|
||||||
|
sort 排序效率
|
||||||
|
```
|
||||||
|
|
||||||
|
## debug 调试文件的存放目录
|
||||||
|
|
51
archive/algorithm/algorithm/bit_add.c
Normal file
51
archive/algorithm/algorithm/bit_add.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 位运算加法
|
||||||
|
* 就和我们算十位加法差不多
|
||||||
|
* 10+1=11
|
||||||
|
* 11+1=100
|
||||||
|
* 11+11=0
|
||||||
|
* 画个式子
|
||||||
|
* 010 0011 0011
|
||||||
|
* +01 +001 +011
|
||||||
|
* ---- --``- -``-
|
||||||
|
* 011 0100 0110
|
||||||
|
*
|
||||||
|
* 位运算中,^(位异或)相当于加法,但是不会进位
|
||||||
|
* &(位与)判断进位,然后<<(左移)动一位
|
||||||
|
* 例如11+11可以解析成下面这样
|
||||||
|
* 11^11=00
|
||||||
|
* (11&11)<<1=110
|
||||||
|
* 完整例子2:101+11
|
||||||
|
* 101^11=110
|
||||||
|
* (101&11)<<1=010
|
||||||
|
* 重复
|
||||||
|
* 110^010=100
|
||||||
|
* (110&010)<<1=100
|
||||||
|
* 重复
|
||||||
|
* 100^100=0
|
||||||
|
* 100&100<<1=1000
|
||||||
|
* 重复
|
||||||
|
* 0^1000=1000
|
||||||
|
* 0&1000<<1=0
|
||||||
|
* 结束,返回1000
|
||||||
|
*/
|
||||||
|
|
||||||
|
int bit_add(int num1, int num2)
|
||||||
|
{
|
||||||
|
while (num2)
|
||||||
|
{
|
||||||
|
int tmp = num1 ^ num2;
|
||||||
|
num2 = (num1 & num2) << 1;
|
||||||
|
num1 = tmp;
|
||||||
|
}
|
||||||
|
return num1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", bit_add(32, 1));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
44
archive/algorithm/algorithm/bit_division.c
Normal file
44
archive/algorithm/algorithm/bit_division.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 位运算除法
|
||||||
|
* 好像可以直接的用减法来算,到不能减了停止
|
||||||
|
* 但是这个方法,需要算太多次,O(N)
|
||||||
|
* 那么我们每次将被除数扩大一倍(使用左移),从最大的开始
|
||||||
|
*/
|
||||||
|
|
||||||
|
int add(int num1, int num2)
|
||||||
|
{
|
||||||
|
while (num2)
|
||||||
|
{
|
||||||
|
int tmp = num1 ^ num2;
|
||||||
|
num2 = (num1 & num2) << 1;
|
||||||
|
num1 = tmp;
|
||||||
|
}
|
||||||
|
return num1;
|
||||||
|
}
|
||||||
|
int sub(int num1, int num2)
|
||||||
|
{
|
||||||
|
return add(num1, add(~num2, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int bit_division(int num1, int num2)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 31; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((num1 >> i) >= num2)
|
||||||
|
{
|
||||||
|
result = add(result, 1 << i);
|
||||||
|
num1 = sub(num1, num2 << i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", bit_division(10, 3));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
51
archive/algorithm/algorithm/bit_mul.c
Normal file
51
archive/algorithm/algorithm/bit_mul.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 位运算乘法(要不循环加法,滑稽)
|
||||||
|
* 按照算10位一样,列个式子
|
||||||
|
* 0011
|
||||||
|
* x010
|
||||||
|
* ----
|
||||||
|
* 00
|
||||||
|
* 11
|
||||||
|
* ----
|
||||||
|
* 0110
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
int add(int num1, int num2)
|
||||||
|
{
|
||||||
|
while (num2)
|
||||||
|
{
|
||||||
|
int tmp = num1 ^ num2;
|
||||||
|
num2 = (num1 & num2) << 1;
|
||||||
|
num1 = tmp;
|
||||||
|
}
|
||||||
|
return num1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bit_mul(int num1, int num2)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
//只考虑了正数,负数的话,先右移31,看结果为1还是0,为1则是负数(补码)
|
||||||
|
//然后将数字转换为正数(负负得正,再转换成一次负数)
|
||||||
|
//最后算出结果的时候,如果是负再转换回去
|
||||||
|
while (num2)
|
||||||
|
{
|
||||||
|
if (num2 & 1)
|
||||||
|
{
|
||||||
|
result = add(result, num1);
|
||||||
|
}
|
||||||
|
num1 = num1 << 1;
|
||||||
|
num2 = num2 >> 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", bit_mul(8, 2));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
30
archive/algorithm/algorithm/bit_sub.c
Normal file
30
archive/algorithm/algorithm/bit_sub.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 位运算减法
|
||||||
|
* 减法就是加法,只是后者换成了一个负数
|
||||||
|
* 负数补码:求负整数的补码,将其对应正数二进制表示所有位取反(包括符号位,0变1,1变0)后加1
|
||||||
|
*/
|
||||||
|
|
||||||
|
int add(int num1, int num2)
|
||||||
|
{
|
||||||
|
while (num2)
|
||||||
|
{
|
||||||
|
int tmp = num1 ^ num2;
|
||||||
|
num2 = (num1 & num2) << 1;
|
||||||
|
num1 = tmp;
|
||||||
|
}
|
||||||
|
return num1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bit_sub(int num1, int num2)
|
||||||
|
{
|
||||||
|
return add(num1, add(~num2, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", bit_sub(32, 1));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
26
archive/algorithm/algorithm/bubble.c
Normal file
26
archive/algorithm/algorithm/bubble.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
/**
|
||||||
|
* 冒泡排序,网上copy来的,只是想对比速度
|
||||||
|
*/
|
||||||
|
void bubble_sort(int a[], int n)
|
||||||
|
{
|
||||||
|
int i, j, temp;
|
||||||
|
for (j = 0; j < n - 1; j++)
|
||||||
|
for (i = 0; i < n - 1 - j; i++)
|
||||||
|
if (a[i] > a[i + 1])
|
||||||
|
{
|
||||||
|
temp = a[i];
|
||||||
|
a[i] = a[i + 1];
|
||||||
|
a[i + 1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef MAIN_FUNC
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int number[] = {10, 56, 4894, 32, 894, 2354, 819, 15468, 1654, 89};
|
||||||
|
bubble_sort(number, 10);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
55
archive/algorithm/algorithm/heap.c
Normal file
55
archive/algorithm/algorithm/heap.c
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 优先队列--堆 堆排序
|
||||||
|
* 堆是一个完全二叉树
|
||||||
|
*/
|
||||||
|
|
||||||
|
void swap(int *array, int pos, int pos2)
|
||||||
|
{
|
||||||
|
int tmp = array[pos];
|
||||||
|
array[pos] = array[pos2];
|
||||||
|
array[pos2] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void heap_sort(int *heap, int len)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 构造堆
|
||||||
|
*/
|
||||||
|
for (int i = (len / 2 - 1); i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (heap[i] > heap[(i + 1) * 2 - 1])
|
||||||
|
{
|
||||||
|
swap(heap, i, (i + 1) * 2 - 1);
|
||||||
|
}
|
||||||
|
if (len > (i + 1) * 2 && heap[i] > heap[(i + 1) * 2])
|
||||||
|
{
|
||||||
|
swap(heap, i, (i + 1) * 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 调整堆
|
||||||
|
*/
|
||||||
|
if (len > 1)
|
||||||
|
{
|
||||||
|
heap_sort(&heap[1], len - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int heap[] = {17, 489, 5, 23, 6746, 64, 49, 8, 41, 56, 46, 17, 15,
|
||||||
|
461, 54, 61, 165, 89, 15, 64, 14, 664};
|
||||||
|
heap_sort(heap, sizeof(heap) / sizeof(int));
|
||||||
|
print_r(heap, sizeof(heap) / sizeof(int));
|
||||||
|
return 0;
|
||||||
|
}
|
6
archive/algorithm/algorithm/prim.c
Normal file
6
archive/algorithm/algorithm/prim.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
58
archive/algorithm/algorithm/qsort-1.c
Normal file
58
archive/algorithm/algorithm/qsort-1.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
//练习快排+去重
|
||||||
|
#define AMOUNT 1000
|
||||||
|
|
||||||
|
void my_qsort(int* num, int len) {
|
||||||
|
//选第一位为基数
|
||||||
|
if (len <= 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int base = num[0]; //基数
|
||||||
|
int n = len - 1, i = 1;
|
||||||
|
for (; i < len; i++) {
|
||||||
|
//从左往右扫描,遇到比base大的定住,然后右往左扫描,比len小的交换,然后继续
|
||||||
|
// 4 2 3 5 7 2 1 4 2 3 1 7 2 5 4 2 3 1 2 7 5
|
||||||
|
// 2 2 3 1 4 7 5
|
||||||
|
if (num[i] > base) {
|
||||||
|
for (; n > i; n--) {
|
||||||
|
if (num[n] < base) {
|
||||||
|
//交换
|
||||||
|
int tmp = num[i];
|
||||||
|
num[i] = num[n];
|
||||||
|
num[n] = tmp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == n) {
|
||||||
|
//相遇了,结束,并交换基数
|
||||||
|
int tmp = num[i - 1];
|
||||||
|
num[i - 1] = num[0];
|
||||||
|
num[0] = tmp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//对位置左右两边排序,分治的思路
|
||||||
|
my_qsort(num, i);
|
||||||
|
my_qsort(&num[i], len - i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int num[AMOUNT] = {0};
|
||||||
|
srand(1);
|
||||||
|
for (int i = 0; i < AMOUNT; i++) {
|
||||||
|
num[i] = rand();
|
||||||
|
printf("%d\t", num[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
my_qsort(num, AMOUNT);
|
||||||
|
printf("%d\t", num[0]);
|
||||||
|
for (int i = 1; i < AMOUNT; i++) {
|
||||||
|
//去重输出
|
||||||
|
if (num[i] != num[i - 1]) {
|
||||||
|
printf("%d\t", num[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
getchar();
|
||||||
|
}
|
90
archive/algorithm/algorithm/qsort.c
Normal file
90
archive/algorithm/algorithm/qsort.c
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
/**
|
||||||
|
* 快速排序O(NlogN)和插入排序O(N^2)
|
||||||
|
* 根据《数据结构和算法》学习
|
||||||
|
*/
|
||||||
|
void swap(int *array, int pos, int pos2);
|
||||||
|
int *insert_sort(int *array, int len)
|
||||||
|
{
|
||||||
|
for (int p = 1; p < len; p++)
|
||||||
|
{
|
||||||
|
int tmp = array[p], i;
|
||||||
|
for (i = p; i > 0 && array[i - 1] > tmp; i--) //插入排序,将前一个一直往后面插入,直到遇到,比tmp小的情况
|
||||||
|
{
|
||||||
|
array[i] = array[i - 1];
|
||||||
|
}
|
||||||
|
array[i] = tmp;
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 快速排序,递归,利用分治的方法
|
||||||
|
* 首先确定一个基数,比其小的移向左,大的移向右
|
||||||
|
* 然后递归调用
|
||||||
|
*/
|
||||||
|
int *quick_sort(int *array, int len)
|
||||||
|
{
|
||||||
|
if (len <= 20) //长度小于20的采用插入排序法
|
||||||
|
{
|
||||||
|
return insert_sort(array, len);
|
||||||
|
}
|
||||||
|
//先取 左 中 右 三个数字
|
||||||
|
int l = 0, m, r = len - 2, pos = r / 2; //-2是取倒数第二个
|
||||||
|
//将三个数字排序,左最小,右最大
|
||||||
|
if (array[l] > array[r])
|
||||||
|
swap(array, l, r);
|
||||||
|
if (array[l] > array[pos])
|
||||||
|
swap(array, l, pos);
|
||||||
|
if (array[pos] > array[r])
|
||||||
|
swap(array, pos, r);
|
||||||
|
|
||||||
|
swap(array, pos, len - 1); //交换中间的数字和最后一个
|
||||||
|
|
||||||
|
m = array[len - 1];
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
while (array[++l] < m)
|
||||||
|
; //左边的一直向右移动,直到遇到大于m的
|
||||||
|
while (array[--r] > m)
|
||||||
|
; //右边的一直向左移动,直到遇到小于m的
|
||||||
|
if (l >= r)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
swap(array, l, r);
|
||||||
|
}
|
||||||
|
//交换l和最后的
|
||||||
|
swap(array, l, len - 1);
|
||||||
|
quick_sort(array, l);
|
||||||
|
quick_sort(&array[l + 1], len - l - 1);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int *array, int pos, int pos2)
|
||||||
|
{
|
||||||
|
int tmp = array[pos];
|
||||||
|
array[pos] = array[pos2];
|
||||||
|
array[pos2] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef MAIN_FUNC
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a[] = {8, 1, 4, 7, 7, 6, 2, 1, 1, 6, 68, 1, 5, 5, 4, 54, 89, 42, 7, 0};
|
||||||
|
quick_sort(a, sizeof(a) / sizeof(int));
|
||||||
|
print_r(a, sizeof(a) / sizeof(int));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
70
archive/algorithm/algorithm/radix_sort.c
Normal file
70
archive/algorithm/algorithm/radix_sort.c
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "../struct/adt/u_link_list.h"
|
||||||
|
/**
|
||||||
|
* 基数排序
|
||||||
|
*/
|
||||||
|
#define getN(number, N) (int)((float)(number / pow(10, N - 1))) % 10
|
||||||
|
#define NODE_SIZE sizeof(struct node)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基数排序,LDS
|
||||||
|
* 参考地址:http://blog.csdn.net/lemon_tree12138/article/details/51695211
|
||||||
|
*/
|
||||||
|
void radix_sort_lds(int *number, int len)
|
||||||
|
{
|
||||||
|
int *buck = malloc(sizeof(int) * len); //桶子
|
||||||
|
int buck_sub[10] = {0}; //每一个桶的结束下标
|
||||||
|
int now_bit = 0, max_bit = 0; //现在的位数,最大的位数
|
||||||
|
for (int i = 0; i < len; i++) //获取最大的数字
|
||||||
|
{
|
||||||
|
if (number[i] > max_bit)
|
||||||
|
{
|
||||||
|
max_bit = number[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int tmp = max_bit;
|
||||||
|
max_bit = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
max_bit++;
|
||||||
|
tmp /= 10;
|
||||||
|
} while (tmp > 0); //取出最大位数
|
||||||
|
for (now_bit = 1; now_bit <= max_bit; now_bit++)
|
||||||
|
{
|
||||||
|
memset(buck_sub, 0, sizeof(buck_sub));
|
||||||
|
// memset(buck, 0, sizeof(int) * len);
|
||||||
|
for (int i = 0; i < len; i++) //获取每个桶中有多少个数字
|
||||||
|
{
|
||||||
|
buck_sub[getN(number[i], now_bit)] += 1;
|
||||||
|
}
|
||||||
|
for (int i = 1; i < 10; i++) //将数量转换为数组下标
|
||||||
|
{
|
||||||
|
buck_sub[i] = buck_sub[i] + buck_sub[i - 1];
|
||||||
|
}
|
||||||
|
for (int i = len - 1; i >= 0; i--) //将数组分配到桶中
|
||||||
|
{
|
||||||
|
int bit = getN(number[i], now_bit);
|
||||||
|
buck[--buck_sub[bit]] = number[i];
|
||||||
|
}
|
||||||
|
//将桶中的数据交换过去,直接拷贝内存
|
||||||
|
memcpy(number, buck, sizeof(int) * len);
|
||||||
|
}
|
||||||
|
free(buck);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef MAIN_FUNC
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int number[] = {1000, 999, 103};
|
||||||
|
radix_sort_lds(number, sizeof(number) / 4);
|
||||||
|
for (int i = 0; i < sizeof(number) / 4; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", number[i]);
|
||||||
|
}
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
13
archive/algorithm/algorithm/ssq.c
Normal file
13
archive/algorithm/algorithm/ssq.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
for(int i=0;i<=3;i++){
|
||||||
|
for(int n=0;n<=3;n++){
|
||||||
|
if(8-i-n<=6){
|
||||||
|
printf("red:%d;yellow:%d,green:%d\n",i,n,8-i-n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
139
archive/algorithm/geektime/c/06.c
Normal file
139
archive/algorithm/geektime/c/06.c
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <memory.h>
|
||||||
|
|
||||||
|
struct node;
|
||||||
|
typedef struct node Node;
|
||||||
|
|
||||||
|
struct node
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
Node *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
void insert(Node *header, int val, int pos)
|
||||||
|
{
|
||||||
|
Node *tmp = header;
|
||||||
|
int tmpPos = 0;
|
||||||
|
if (pos == -1)
|
||||||
|
{
|
||||||
|
pos = __INT_MAX__;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (tmp->next != NULL && tmpPos < pos)
|
||||||
|
{
|
||||||
|
tmpPos++;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
Node *i = malloc(sizeof(Node));
|
||||||
|
i->next = tmp->next;
|
||||||
|
i->val = val;
|
||||||
|
tmp->next = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete (Node *header, int pos)
|
||||||
|
{
|
||||||
|
Node *tmp = header;
|
||||||
|
int tmpPos = 0;
|
||||||
|
while (tmp->next != NULL && tmpPos < pos)
|
||||||
|
{
|
||||||
|
tmpPos++;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
Node *del = tmp->next;
|
||||||
|
if (del == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tmp->next = tmp->next->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *reverse(Node *header)
|
||||||
|
{
|
||||||
|
Node *last = NULL;
|
||||||
|
Node *tmp = header;
|
||||||
|
while (tmp = tmp->next)
|
||||||
|
{
|
||||||
|
Node *now = malloc(sizeof(Node));
|
||||||
|
memset(now, 0, sizeof(Node));
|
||||||
|
now->val = tmp->val;
|
||||||
|
now->next = last;
|
||||||
|
last = now;
|
||||||
|
}
|
||||||
|
Node *newHeader = malloc(sizeof(Node));
|
||||||
|
memset(newHeader, 0, sizeof(Node));
|
||||||
|
newHeader->next = last;
|
||||||
|
return newHeader;
|
||||||
|
}
|
||||||
|
|
||||||
|
int isRing(Node *header)
|
||||||
|
{
|
||||||
|
if (header == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Node *k = header, *m = header;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (k->next == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
k = k->next;
|
||||||
|
if (k->next == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
k = k->next;
|
||||||
|
m = m->next;
|
||||||
|
} while (m != k);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Node *header = malloc(sizeof(Node));
|
||||||
|
header->val = -1;
|
||||||
|
header->next = NULL;
|
||||||
|
|
||||||
|
insert(header, 1, -1);
|
||||||
|
insert(header, 2, -1);
|
||||||
|
insert(header, 3, -1);
|
||||||
|
insert(header, 4, 2);
|
||||||
|
insert(header, 5, 0);
|
||||||
|
insert(header, 6, 0);
|
||||||
|
|
||||||
|
delete (header, 0);
|
||||||
|
Node *tmp = header;
|
||||||
|
while (tmp = tmp->next)
|
||||||
|
{
|
||||||
|
printf("%d\t", tmp->val);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *header2 = malloc(sizeof(Node));
|
||||||
|
header2->val = -1;
|
||||||
|
header2->next = NULL;
|
||||||
|
insert(header2, 10, -1);
|
||||||
|
insert(header2, 12, -1);
|
||||||
|
delete (header2, 0);
|
||||||
|
tmp = header2;
|
||||||
|
printf("\nheader2\n");
|
||||||
|
while (tmp = tmp->next)
|
||||||
|
{
|
||||||
|
printf("%d\t", tmp->val);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *r = reverse(header);
|
||||||
|
tmp = r;
|
||||||
|
printf("\reverse\n");
|
||||||
|
while (tmp = tmp->next)
|
||||||
|
{
|
||||||
|
printf("%d\t", tmp->val);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n%d\n", isRing(header));
|
||||||
|
header->next->next->next->next->next = header->next->next;
|
||||||
|
printf("\n%d\n", isRing(header));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
151
archive/algorithm/geektime/sort.go
Normal file
151
archive/algorithm/geektime/sort.go
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
package geektime
|
||||||
|
|
||||||
|
func bubble_1(arr []int) []int {
|
||||||
|
ret := make([]int, len(arr))
|
||||||
|
copy(ret, arr)
|
||||||
|
for i := range ret {
|
||||||
|
for j := range ret[i:] {
|
||||||
|
j = i + j
|
||||||
|
if ret[i] > ret[j] {
|
||||||
|
ret[i], ret[j] = ret[j], ret[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func bubble_2(arr []int) []int {
|
||||||
|
ret := make([]int, len(arr))
|
||||||
|
copy(ret, arr)
|
||||||
|
for i := range ret {
|
||||||
|
flag := false
|
||||||
|
for j := range ret[i:] {
|
||||||
|
j = i + j
|
||||||
|
if ret[i] > ret[j] {
|
||||||
|
flag = true
|
||||||
|
ret[i], ret[j] = ret[j], ret[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !flag {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func insert(arr []int) []int {
|
||||||
|
ret := make([]int, len(arr))
|
||||||
|
copy(ret, arr)
|
||||||
|
for i := 1; i < len(ret); i++ {
|
||||||
|
val := ret[i]
|
||||||
|
j := i - 1
|
||||||
|
for ; j >= 0; j-- {
|
||||||
|
if ret[j] > val {
|
||||||
|
ret[j+1] = ret[j]
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret[j+1] = val
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func selection(arr []int) []int {
|
||||||
|
ret := make([]int, len(arr))
|
||||||
|
copy(ret, arr)
|
||||||
|
for i := range ret {
|
||||||
|
min := i
|
||||||
|
for j := range ret[i:] {
|
||||||
|
j = i + j
|
||||||
|
if ret[min] > ret[j] {
|
||||||
|
min = j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret[min], ret[i] = ret[i], ret[min]
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
//归并排序
|
||||||
|
func merge_sort(arr []int) []int {
|
||||||
|
ret := make([]int, len(arr))
|
||||||
|
copy(ret, arr)
|
||||||
|
dichotomize(ret, 0, len(ret)-1)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func dichotomize(arr []int, i, j int) (int, int) {
|
||||||
|
if i >= j {
|
||||||
|
return i, j
|
||||||
|
}
|
||||||
|
i1, j1 := dichotomize(arr, i, (j-i)/2+i)
|
||||||
|
i2, j2 := dichotomize(arr, (j-i)/2+i+1, j)
|
||||||
|
merge(arr, i1, j1, i2, j2)
|
||||||
|
return i, j
|
||||||
|
}
|
||||||
|
|
||||||
|
func merge(arr []int, i1, j1, i2, j2 int) {
|
||||||
|
pos2 := i2
|
||||||
|
tmp := make([]int, j2-i1+1)
|
||||||
|
tmppos := 0
|
||||||
|
pos1 := i1
|
||||||
|
for ; pos1 <= j1 && pos2 <= j2; tmppos++ {
|
||||||
|
if arr[pos1] > arr[pos2] {
|
||||||
|
tmp[tmppos] = arr[pos2]
|
||||||
|
pos2++
|
||||||
|
} else {
|
||||||
|
tmp[tmppos] = arr[pos1]
|
||||||
|
pos1++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//剩余
|
||||||
|
if pos1 <= j1 {
|
||||||
|
for ; pos1 <= j1; pos1++ {
|
||||||
|
tmp[tmppos] = arr[pos1]
|
||||||
|
tmppos++
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for ; pos2 <= j2; pos2++ {
|
||||||
|
tmp[tmppos] = arr[pos2]
|
||||||
|
tmppos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//copy
|
||||||
|
tmppos = 0
|
||||||
|
for i := i1; i <= j2; i++ {
|
||||||
|
arr[i] = tmp[tmppos]
|
||||||
|
tmppos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func qsort(arr []int) []int {
|
||||||
|
ret := make([]int, len(arr))
|
||||||
|
copy(ret, arr)
|
||||||
|
return qsort_c(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func qsort_c(arr []int) []int {
|
||||||
|
n := len(arr)
|
||||||
|
if n == 2 {
|
||||||
|
if arr[0] > arr[1] {
|
||||||
|
arr[0], arr[1] = arr[1], arr[0]
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
} else if n <= 1 {
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
val := arr[n/2]
|
||||||
|
arr[0], arr[n/2] = arr[n/2], arr[0]
|
||||||
|
j := 1
|
||||||
|
for i := 1; i < n; i++ {
|
||||||
|
if arr[i] < val {
|
||||||
|
arr[i], arr[j] = arr[j], arr[i]
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qsort_c(arr[:j])
|
||||||
|
qsort_c(arr[j:])
|
||||||
|
return arr
|
||||||
|
}
|
159
archive/algorithm/geektime/sort_test.go
Normal file
159
archive/algorithm/geektime/sort_test.go
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
package geektime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var sortArr100, sortArr1000 []int
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
sortArr100 = randIntSlice(100)
|
||||||
|
sortArr1000 = randIntSlice(1000)
|
||||||
|
m.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_bubble(t *testing.T) {
|
||||||
|
arr := []int{5, 3, 4, 1, 2}
|
||||||
|
ret := bubble_1(arr)
|
||||||
|
assert.Equal(t, []int{1, 2, 3, 4, 5}, ret)
|
||||||
|
ret = bubble_2(arr)
|
||||||
|
assert.Equal(t, []int{1, 2, 3, 4, 5}, ret)
|
||||||
|
arr = randIntSlice(10)
|
||||||
|
ret = bubble_1(arr)
|
||||||
|
ret2 := bubble_2(arr)
|
||||||
|
assert.Equal(t, ret2, ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func randIntSlice(n int) []int {
|
||||||
|
ret := make([]int, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
ret[i] = rand.Int()
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkBubble1100(b *testing.B) {
|
||||||
|
arr := sortArr100
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
bubble_1(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkBubble11000(b *testing.B) {
|
||||||
|
arr := sortArr1000
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
bubble_1(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkBubble2100(b *testing.B) {
|
||||||
|
arr := sortArr100
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
bubble_2(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkBubble21000(b *testing.B) {
|
||||||
|
arr := sortArr1000
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
bubble_2(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_insert(t *testing.T) {
|
||||||
|
arr := []int{5, 3, 4, 1, 2}
|
||||||
|
ret := insert(arr)
|
||||||
|
assert.Equal(t, []int{1, 2, 3, 4, 5}, ret)
|
||||||
|
arr = randIntSlice(10)
|
||||||
|
ret = bubble_1(arr)
|
||||||
|
ret2 := insert(arr)
|
||||||
|
assert.Equal(t, ret, ret2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkInsert100(b *testing.B) {
|
||||||
|
arr := sortArr100
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
insert(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkInsert1000(b *testing.B) {
|
||||||
|
arr := sortArr1000
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
insert(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_selection(t *testing.T) {
|
||||||
|
arr := []int{5, 3, 4, 1, 2}
|
||||||
|
ret := selection(arr)
|
||||||
|
assert.Equal(t, []int{1, 2, 3, 4, 5}, ret)
|
||||||
|
arr = randIntSlice(10)
|
||||||
|
ret = bubble_1(arr)
|
||||||
|
ret2 := selection(arr)
|
||||||
|
assert.Equal(t, ret, ret2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSelect100(b *testing.B) {
|
||||||
|
arr := sortArr100
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
selection(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSelect1000(b *testing.B) {
|
||||||
|
arr := sortArr1000
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
selection(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_merge_sort(t *testing.T) {
|
||||||
|
arr := []int{5, 3, 4, 1, 2, 6}
|
||||||
|
ret := merge_sort(arr)
|
||||||
|
assert.Equal(t, []int{1, 2, 3, 4, 5, 6}, ret)
|
||||||
|
arr = randIntSlice(10)
|
||||||
|
ret = bubble_1(arr)
|
||||||
|
ret2 := merge_sort(arr)
|
||||||
|
assert.Equal(t, ret, ret2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMerge100(b *testing.B) {
|
||||||
|
arr := sortArr100
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
merge_sort(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMerge1000(b *testing.B) {
|
||||||
|
arr := sortArr1000
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
merge_sort(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_qsort(t *testing.T) {
|
||||||
|
arr := []int{5, 3, 4, 1, 2, 6}
|
||||||
|
ret := qsort(arr)
|
||||||
|
assert.Equal(t, []int{1, 2, 3, 4, 5, 6}, ret)
|
||||||
|
arr = randIntSlice(10)
|
||||||
|
ret = bubble_1(arr)
|
||||||
|
ret2 := qsort(arr)
|
||||||
|
assert.Equal(t, ret, ret2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkQuickSort100(b *testing.B) {
|
||||||
|
arr := sortArr100
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
qsort(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkQuickSort1000(b *testing.B) {
|
||||||
|
arr := sortArr1000
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
qsort(arr)
|
||||||
|
}
|
||||||
|
}
|
21
archive/algorithm/leetcode/1.c
Normal file
21
archive/algorithm/leetcode/1.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/22/
|
||||||
|
* 买卖股票的最佳时机 II
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int val[] = {2, 1, 2, 0, 1};
|
||||||
|
|
||||||
|
int max = 0;
|
||||||
|
for (int i = 1; i < sizeof(val) / 4; i++)
|
||||||
|
{
|
||||||
|
int d = val[i] - val[i - 1];
|
||||||
|
if (d > 0)
|
||||||
|
{
|
||||||
|
max += d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
23
archive/algorithm/leetcode/11.go
Normal file
23
archive/algorithm/leetcode/11.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func maxArea(height []int) int {
|
||||||
|
var left, right, max = 0, len(height) - 1, 0
|
||||||
|
for left < right {
|
||||||
|
area := 0
|
||||||
|
if height[left] > height[right] {
|
||||||
|
area = height[right] * (right - left)
|
||||||
|
right--
|
||||||
|
} else {
|
||||||
|
area = height[left] * (right - left)
|
||||||
|
left++
|
||||||
|
}
|
||||||
|
if area > max {
|
||||||
|
max = area
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(maxArea([]int{1, 2, 54, 75, 3, 5, 7, 93, 3}))
|
||||||
|
}
|
46
archive/algorithm/leetcode/12.go
Normal file
46
archive/algorithm/leetcode/12.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var mapping = [10][4]int{
|
||||||
|
{0, 0, 0, 0}, {1, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 1, 0}, {1, 2, 0, 0}, {2, 0, 0, 0}, {2, 1, 0, 0}, {2, 1, 1, 0}, {2, 1, 1, 1}, {1, 3, 0, 0},
|
||||||
|
}
|
||||||
|
|
||||||
|
var roman = []string{
|
||||||
|
"I", "V", "X", "L", "C", "D", "M",
|
||||||
|
}
|
||||||
|
|
||||||
|
func intToRoman(num int) string {
|
||||||
|
var ret string
|
||||||
|
bit := 0
|
||||||
|
for num > 0 {
|
||||||
|
t := num % 10
|
||||||
|
num /= 10
|
||||||
|
tmp := ""
|
||||||
|
for _, val := range mapping[t] {
|
||||||
|
if val == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
tmp += roman[val+(bit*2)-1]
|
||||||
|
}
|
||||||
|
bit++
|
||||||
|
ret = tmp + ret
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(intToRoman(1994))
|
||||||
|
}
|
||||||
|
|
||||||
|
//dalao
|
||||||
|
// func intToRoman(num int) string {
|
||||||
|
// var romanInt = []int{1000, 900, 500 ,400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
|
||||||
|
// var romanStr = []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
|
||||||
|
// var res = ""
|
||||||
|
// for i, n := range romanInt {
|
||||||
|
// for num >= n {
|
||||||
|
// res += romanStr[i]
|
||||||
|
// num -= n
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return res
|
||||||
|
// }
|
60
archive/algorithm/leetcode/120.c
Normal file
60
archive/algorithm/leetcode/120.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#define MIN(a, b) (a > b ? b : a)
|
||||||
|
int minimumTotal(int **triangle, int triangleRowSize, int *triangleColSizes) {
|
||||||
|
int **dp = malloc(sizeof(int *) * triangleRowSize), ret = 0;
|
||||||
|
for (int i = 0; i < triangleRowSize; i++) {
|
||||||
|
dp[i] = malloc(sizeof(int) * triangleRowSize);
|
||||||
|
}
|
||||||
|
dp[0][0] = triangle[0][0];
|
||||||
|
for (int i = 1; i < triangleRowSize; i++) {
|
||||||
|
for (int n = 0; n <= i; n++) {
|
||||||
|
if (n == 0) {
|
||||||
|
dp[i][n] = dp[i - 1][n];
|
||||||
|
} else if (n == i) {
|
||||||
|
dp[i][n] = dp[i - 1][n - 1];
|
||||||
|
} else {
|
||||||
|
dp[i][n] = MIN(dp[i - 1][n], dp[i - 1][n - 1]);
|
||||||
|
}
|
||||||
|
dp[i][n] += triangle[i][n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret = dp[triangleRowSize - 1][0];
|
||||||
|
for (int i = 1; i < triangleRowSize; i++) {
|
||||||
|
ret = MIN(ret, dp[triangleRowSize - 1][i]);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
int minimumSubTotal(int** triangle, int triangleRowSize, int *triangleColSizes, int level, int* minTotal) {
|
||||||
|
int i = 0;
|
||||||
|
int min = INT_MAX;
|
||||||
|
int res = 0;
|
||||||
|
int temp = 0;
|
||||||
|
if(level == triangleRowSize - 1){
|
||||||
|
for(i = 0; i <= level; i++){
|
||||||
|
minTotal[i] = triangle[level][i];
|
||||||
|
min = minTotal[i] < min ? minTotal[i] : min;
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
minimumSubTotal(triangle, triangleRowSize, triangleColSizes, level + 1, minTotal);
|
||||||
|
|
||||||
|
for(i = 0, temp = minTotal[0]; i <= level; i++){
|
||||||
|
res = (temp > minTotal[i + 1] ? minTotal[i + 1] : temp) + triangle[level][i];
|
||||||
|
temp = minTotal[i + 1];
|
||||||
|
minTotal[i] = res;
|
||||||
|
min = minTotal[i] < min ? minTotal[i] : min;
|
||||||
|
}
|
||||||
|
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
int minimumTotal(int** triangle, int triangleRowSize, int *triangleColSizes) {
|
||||||
|
int ret = 0;
|
||||||
|
int* minTotal = (int*)malloc(sizeof(int) * triangleRowSize);
|
||||||
|
ret = minimumSubTotal(triangle, triangleRowSize, triangleColSizes, 0, minTotal);
|
||||||
|
free(minTotal);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
*/
|
35
archive/algorithm/leetcode/121.c
Normal file
35
archive/algorithm/leetcode/121.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int maxProfit(int *prices, int pricesSize) {
|
||||||
|
if (pricesSize <= 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int max = 0, min_buy = prices[0], tmp = 0;
|
||||||
|
for (int i = 1; i < pricesSize; i++) {
|
||||||
|
if (min_buy > prices[i]) {
|
||||||
|
min_buy = prices[i];
|
||||||
|
}
|
||||||
|
tmp = prices[i] - min_buy;
|
||||||
|
if (max < tmp) {
|
||||||
|
max = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
#define max(a,b) ((a) > (b) ? (a) : (b))
|
||||||
|
#define min(a,b) ((a) > (b) ? (b) : (a))
|
||||||
|
int maxProfit(int* prices, int pricesSize) {
|
||||||
|
int low = INT_MAX, profit = 0;
|
||||||
|
for(int i = 0 ; i< pricesSize; i++){
|
||||||
|
low = min(low, prices[i]);
|
||||||
|
profit = max(profit, prices[i] - low);
|
||||||
|
}
|
||||||
|
return profit;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
30
archive/algorithm/leetcode/13.go
Normal file
30
archive/algorithm/leetcode/13.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var mapping = map[string]int{
|
||||||
|
"I": 1,
|
||||||
|
"V": 5,
|
||||||
|
"X": 10,
|
||||||
|
"L": 50,
|
||||||
|
"C": 100,
|
||||||
|
"D": 500,
|
||||||
|
"M": 1000,
|
||||||
|
}
|
||||||
|
|
||||||
|
func romanToInt(s string) int {
|
||||||
|
var ret, last int
|
||||||
|
for _, chr := range s {
|
||||||
|
val := mapping[string(chr)]
|
||||||
|
if last < val {
|
||||||
|
ret = val + ret - (last * 2)
|
||||||
|
} else {
|
||||||
|
ret += val
|
||||||
|
}
|
||||||
|
last = val
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(romanToInt("MCMXCIV")) //5
|
||||||
|
println(romanToInt("IIIV")) //4
|
||||||
|
}
|
40
archive/algorithm/leetcode/14.c
Normal file
40
archive/algorithm/leetcode/14.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/longest-common-prefix/description/
|
||||||
|
* 最长公共前缀
|
||||||
|
*/
|
||||||
|
char *longestCommonPrefix(char strs[3][10], int strsSize)
|
||||||
|
{
|
||||||
|
if (strsSize <= 0)
|
||||||
|
{
|
||||||
|
char *tmp = malloc(1);
|
||||||
|
tmp[0] = 0;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
char *ret = strs[0];
|
||||||
|
for (int i = 1; i < strsSize; i++)
|
||||||
|
{
|
||||||
|
char *tmp = &strs[i][0], *tmp1 = ret;
|
||||||
|
int len = 0;
|
||||||
|
while (*tmp1 == *tmp && *tmp!=0)
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
tmp1++;
|
||||||
|
tmp++;
|
||||||
|
}
|
||||||
|
ret[len]=0;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char a[3][10] = {"aa", "aa", "aa"};
|
||||||
|
longestCommonPrefix(a, 3);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
56
archive/algorithm/leetcode/141.c
Normal file
56
archive/algorithm/leetcode/141.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct ListNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct ListNode *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef int bool;
|
||||||
|
#define true 1;
|
||||||
|
#define false 0;
|
||||||
|
|
||||||
|
bool hasCycle(struct ListNode *head)
|
||||||
|
{
|
||||||
|
if (head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
struct ListNode *k = head->next, *m = head;
|
||||||
|
while (k != m)
|
||||||
|
{
|
||||||
|
if (m == NULL || k == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
m = m->next;
|
||||||
|
k = k->next;
|
||||||
|
if (k == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
k = k->next;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct ListNode *head = malloc(sizeof(struct ListNode)), *next = malloc(sizeof(struct ListNode)), *tmp;
|
||||||
|
head->val = 3;
|
||||||
|
head->next = next;
|
||||||
|
tmp = next;
|
||||||
|
next->val = 2;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 0;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = -4;
|
||||||
|
next->next = NULL;
|
||||||
|
|
||||||
|
hasCycle(head);
|
||||||
|
return 0;
|
||||||
|
}
|
108
archive/algorithm/leetcode/146-LRU.go
Normal file
108
archive/algorithm/leetcode/146-LRU.go
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
//https://leetcode-cn.com/problems/lru-cache/
|
||||||
|
//数组形式实现(O(n)),另外可以用双向链表+哈希表实现(O(1))
|
||||||
|
type LRUCache struct {
|
||||||
|
capacity int
|
||||||
|
cache map[int]int
|
||||||
|
lru []int
|
||||||
|
head int
|
||||||
|
tail int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Constructor(capacity int) LRUCache {
|
||||||
|
return LRUCache{
|
||||||
|
capacity: capacity,
|
||||||
|
cache: make(map[int]int),
|
||||||
|
lru: make([]int, capacity),
|
||||||
|
head: 0, tail: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *LRUCache) Get(key int) int {
|
||||||
|
v, ok := this.cache[key]
|
||||||
|
if ok {
|
||||||
|
this.FloatUp(key)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *LRUCache) Put(key int, value int) {
|
||||||
|
this.cache[key] = value
|
||||||
|
this.FloatUp(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *LRUCache) FloatUp(key int) {
|
||||||
|
//查找位
|
||||||
|
for i := 0; true; i++ {
|
||||||
|
var pos = this.pos(i + this.tail)
|
||||||
|
if key == this.lru[pos] {
|
||||||
|
//提到第一位,前面的后移
|
||||||
|
for ; true; i++ {
|
||||||
|
var pos = this.pos(i + this.tail)
|
||||||
|
if pos == this.head {
|
||||||
|
//到头了,跳出
|
||||||
|
break
|
||||||
|
}
|
||||||
|
this.lru[pos] = this.lru[this.pos(pos+1)]
|
||||||
|
}
|
||||||
|
this.head = this.head - 1
|
||||||
|
if this.head == -1 {
|
||||||
|
this.head = this.capacity - 1
|
||||||
|
}
|
||||||
|
break
|
||||||
|
} else if pos == this.head {
|
||||||
|
//到头了,跳出
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var tmp = this.lru[this.tail]
|
||||||
|
this.head = this.pos(this.head + 1)
|
||||||
|
this.lru[this.head] = key
|
||||||
|
//判断是否已满
|
||||||
|
if this.head == this.tail {
|
||||||
|
//满,删除最后一位
|
||||||
|
delete(this.cache, tmp)
|
||||||
|
this.tail = this.pos(this.tail + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *LRUCache) pos(pos int) int {
|
||||||
|
return pos % this.capacity
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your LRUCache object will be instantiated and called as such:
|
||||||
|
* obj := Constructor(capacity);
|
||||||
|
* param_1 := obj.Get(key);
|
||||||
|
* obj.Put(key,value);
|
||||||
|
*/
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cache := Constructor(3)
|
||||||
|
cache.Put(1, 1)
|
||||||
|
cache.Put(2, 2)
|
||||||
|
cache.Put(3, 3)
|
||||||
|
cache.Put(4, 4)
|
||||||
|
cache.Get(4)
|
||||||
|
cache.Get(3)
|
||||||
|
cache.Get(2)
|
||||||
|
cache.Get(1)
|
||||||
|
cache.Put(5, 5)
|
||||||
|
cache.Get(1)
|
||||||
|
cache.Get(2)
|
||||||
|
cache.Get(3)
|
||||||
|
cache.Get(4)
|
||||||
|
cache.Get(5)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Equal(expected, actual int) {
|
||||||
|
if expected != actual {
|
||||||
|
log.Fatalln(expected, "!=", actual)
|
||||||
|
} else {
|
||||||
|
log.Println("OK")
|
||||||
|
}
|
||||||
|
}
|
160
archive/algorithm/leetcode/15.c
Normal file
160
archive/algorithm/leetcode/15.c
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/3sum/description/
|
||||||
|
* 三数之和
|
||||||
|
*/
|
||||||
|
int **threeSum(int *nums, int numsSize, int *returnSize)
|
||||||
|
{
|
||||||
|
int **ret = malloc(sizeof(int *) * numsSize); //行
|
||||||
|
int len = numsSize; //数组现在的长度
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
{
|
||||||
|
ret[i] = malloc(sizeof(int) * 3);
|
||||||
|
memset(ret[i], 0, sizeof(int) * 3);
|
||||||
|
}
|
||||||
|
*returnSize = 0;
|
||||||
|
//排序
|
||||||
|
int cmp(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return (*(int *)a - *(int *)b);
|
||||||
|
}
|
||||||
|
qsort(nums, numsSize, sizeof(int), &cmp);
|
||||||
|
//双指针的方法
|
||||||
|
//-4,-1,-1,0,1,2
|
||||||
|
for (int i = 0; i < numsSize - 2; i++)
|
||||||
|
{
|
||||||
|
if (nums[i] > 0) //有序的情况下,为正数是不可能=0的,跳出循环
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (i > 0 && nums[i] == nums[i - 1]) //向前检查数字重复
|
||||||
|
continue;
|
||||||
|
int h = i + 1, w = numsSize - 1, target = -nums[i];
|
||||||
|
//向中间移动
|
||||||
|
while (h < w)
|
||||||
|
{
|
||||||
|
if (nums[h] + nums[w] == target)
|
||||||
|
{
|
||||||
|
//判断内存足够么
|
||||||
|
if (len - 1 < *returnSize)
|
||||||
|
{
|
||||||
|
ret = realloc(ret, sizeof(int *) * (len + numsSize));
|
||||||
|
//重新分配内存
|
||||||
|
len += numsSize;
|
||||||
|
for (int i = *returnSize; i < len; i++)
|
||||||
|
{
|
||||||
|
ret[i] = malloc(sizeof(int) * 3);
|
||||||
|
memset(ret[i], 0, sizeof(int) * 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//符合条件
|
||||||
|
ret[*returnSize][0] = nums[i];
|
||||||
|
ret[*returnSize][1] = nums[h];
|
||||||
|
ret[*returnSize][2] = nums[w];
|
||||||
|
*returnSize = *returnSize + 1;
|
||||||
|
//跳过相同的数字
|
||||||
|
while (h < w && nums[h] == nums[h + 1])
|
||||||
|
h++;
|
||||||
|
while (h < w && nums[w] == nums[w - 1])
|
||||||
|
w--;
|
||||||
|
h++;
|
||||||
|
w--;
|
||||||
|
}
|
||||||
|
else if (nums[h] + nums[w] < target)
|
||||||
|
{
|
||||||
|
//小一点,就往右移动
|
||||||
|
h++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//大一点
|
||||||
|
w--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
int cmpfunc(const void * a, const void * b)
|
||||||
|
{
|
||||||
|
return (*(int*)a - *(int*)b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int** threeSum(int* nums, int numsSize, int* returnSize)
|
||||||
|
{
|
||||||
|
int front, back;
|
||||||
|
int i = 0;
|
||||||
|
int **result = 0;
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
qsort(nums, numsSize, sizeof(int), cmpfunc);
|
||||||
|
|
||||||
|
while (i < numsSize)
|
||||||
|
{
|
||||||
|
int target = -nums[i];
|
||||||
|
if (target < 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
front = i + 1;
|
||||||
|
back = numsSize - 1;
|
||||||
|
|
||||||
|
while (front < back)
|
||||||
|
{
|
||||||
|
int sum = nums[front] + nums[back];
|
||||||
|
if (sum < target)
|
||||||
|
{
|
||||||
|
front++;
|
||||||
|
}
|
||||||
|
else if (sum > target)
|
||||||
|
{
|
||||||
|
back--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s++;
|
||||||
|
result = (int **)realloc(result, sizeof(int *)*s);
|
||||||
|
int *temp = (int *)malloc(sizeof(int) * 3);
|
||||||
|
temp[0] = nums[i];
|
||||||
|
temp[1] = nums[front];
|
||||||
|
temp[2] = nums[back];
|
||||||
|
result[s-1] = temp;
|
||||||
|
|
||||||
|
while (front < back && nums[front] == temp[1])
|
||||||
|
{
|
||||||
|
front++;
|
||||||
|
}
|
||||||
|
while (front < back && nums[back] == temp[2])
|
||||||
|
{
|
||||||
|
back--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while ((i + 1) < numsSize && nums[i] == nums[i + 1])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//returnSize = (int *)malloc(sizeof(int));
|
||||||
|
*returnSize = s;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// int a[] = {-7, -4, -6, 6, 4, -6, -9, -10, -7, 5, 3, -1, -5, 8, -1, -2, -8, -1, 5, -3, -5, 4, 2, -5, -4, 4, 7}, size;
|
||||||
|
int a[] = {0, 0, 0, 0}, size;
|
||||||
|
int **ret = threeSum(a, sizeof(a) / sizeof(int), &size);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
137
archive/algorithm/leetcode/155.c
Normal file
137
archive/algorithm/leetcode/155.c
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct Stack Stack;
|
||||||
|
// 大数组
|
||||||
|
// TODO: 优化(骗人的todo)
|
||||||
|
typedef struct Stack
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
Stack *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
Stack *min_stack;
|
||||||
|
Stack *stack;
|
||||||
|
} MinStack;
|
||||||
|
|
||||||
|
/** initialize your data structure here. */
|
||||||
|
|
||||||
|
Stack *NewStack(int val)
|
||||||
|
{
|
||||||
|
Stack *ret = malloc(sizeof(Stack));
|
||||||
|
ret->val = val;
|
||||||
|
ret->next = NULL;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
MinStack *minStackCreate()
|
||||||
|
{
|
||||||
|
MinStack *ret = malloc(sizeof(MinStack));
|
||||||
|
ret->min_stack = NewStack(-1);
|
||||||
|
ret->stack = NewStack(-1);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void minStackPush(MinStack *obj, int x)
|
||||||
|
{
|
||||||
|
Stack *tmp = obj->min_stack->next, *prev = obj->min_stack;
|
||||||
|
while (tmp != NULL)
|
||||||
|
{
|
||||||
|
if (x < tmp->val)
|
||||||
|
{
|
||||||
|
prev->next = NewStack(x);
|
||||||
|
prev->next->next = tmp;
|
||||||
|
|
||||||
|
tmp = obj->stack->next;
|
||||||
|
obj->stack->next = NewStack(x);
|
||||||
|
obj->stack->next->next = tmp;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
prev = prev->next;
|
||||||
|
}
|
||||||
|
//最大
|
||||||
|
prev->next = NewStack(x);
|
||||||
|
|
||||||
|
tmp = obj->stack->next;
|
||||||
|
obj->stack->next = NewStack(x);
|
||||||
|
obj->stack->next->next = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// void
|
||||||
|
|
||||||
|
void minStackPop(MinStack *obj)
|
||||||
|
{
|
||||||
|
int val = obj->stack->next->val;
|
||||||
|
Stack *tmp = obj->stack->next, *prev = obj->min_stack;
|
||||||
|
obj->stack->next = tmp->next;
|
||||||
|
free(tmp);
|
||||||
|
tmp = obj->min_stack->next;
|
||||||
|
while (tmp != NULL)
|
||||||
|
{
|
||||||
|
if (val == tmp->val)
|
||||||
|
{
|
||||||
|
prev->next = tmp->next;
|
||||||
|
free(tmp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
prev = prev->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int minStackTop(MinStack *obj)
|
||||||
|
{
|
||||||
|
return obj->stack->next->val;
|
||||||
|
}
|
||||||
|
|
||||||
|
int minStackGetMin(MinStack *obj)
|
||||||
|
{
|
||||||
|
return obj->min_stack->next->val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void minStackFree(MinStack *obj)
|
||||||
|
{
|
||||||
|
freeStack(obj->stack);
|
||||||
|
freeStack(obj->min_stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeStack(Stack *stack)
|
||||||
|
{
|
||||||
|
Stack *tmp;
|
||||||
|
while (stack != NULL)
|
||||||
|
{
|
||||||
|
tmp = stack;
|
||||||
|
stack = stack->next;
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your MinStack struct will be instantiated and called as such:
|
||||||
|
* MinStack* obj = minStackCreate();
|
||||||
|
* minStackPush(obj, x);
|
||||||
|
|
||||||
|
* minStackPop(obj);
|
||||||
|
|
||||||
|
* int param_3 = minStackTop(obj);
|
||||||
|
|
||||||
|
* int param_4 = minStackGetMin(obj);
|
||||||
|
|
||||||
|
* minStackFree(obj);
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
MinStack *minStack = minStackCreate();
|
||||||
|
minStackPush(minStack, -2);
|
||||||
|
minStackPush(minStack, 0);
|
||||||
|
minStackPush(minStack, -3);
|
||||||
|
minStackGetMin(minStack);
|
||||||
|
minStackPop(minStack);
|
||||||
|
minStackTop(minStack);
|
||||||
|
minStackGetMin(minStack);
|
||||||
|
|
||||||
|
minStackFree(minStack);
|
||||||
|
}
|
41
archive/algorithm/leetcode/16.go
Normal file
41
archive/algorithm/leetcode/16.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func threeSumClosest(nums []int, target int) int {
|
||||||
|
min := int(^uint(0) >> 1)
|
||||||
|
minAbs := min
|
||||||
|
numLen := len(nums)
|
||||||
|
sort.Slice(nums, func(i, j int) bool {
|
||||||
|
return nums[i] < nums[j]
|
||||||
|
})
|
||||||
|
for i := 0; i < numLen-2; i++ {
|
||||||
|
left, right := i+1, numLen-1
|
||||||
|
for left < right {
|
||||||
|
tmp := nums[i] + nums[left] + nums[right]
|
||||||
|
tmpAbs := target - tmp
|
||||||
|
if tmpAbs < 0 {
|
||||||
|
tmpAbs = -tmpAbs
|
||||||
|
}
|
||||||
|
if tmp < target {
|
||||||
|
left++
|
||||||
|
} else {
|
||||||
|
right--
|
||||||
|
}
|
||||||
|
if tmpAbs < minAbs {
|
||||||
|
min = tmp
|
||||||
|
minAbs = tmpAbs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(threeSumClosest([]int{1, 2, 4, 8, 16, 32, 64, 128}, 82))
|
||||||
|
}
|
||||||
|
|
||||||
|
// [1,2,3,4,5,6,7,8,9]
|
||||||
|
// [-1,2,1,-4]
|
39
archive/algorithm/leetcode/17.go
Normal file
39
archive/algorithm/leetcode/17.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var mapping = map[rune][]rune{
|
||||||
|
'2': {'a', 'b', 'c'}, '3': {'d', 'e', 'f'},
|
||||||
|
'4': {'g', 'h', 'i'}, '5': {'j', 'k', 'l'}, '6': {'m', 'n', 'o'},
|
||||||
|
'7': {'p', 'q', 'r', 's'}, '8': {'t', 'u', 'v'}, '9': {'w', 'x', 'y', 'z'},
|
||||||
|
}
|
||||||
|
|
||||||
|
func dfs(digits string, ret []string) []string {
|
||||||
|
if digits == "" {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
chars := mapping[rune(digits[0])]
|
||||||
|
if len(ret) == 0 {
|
||||||
|
for _, val := range chars {
|
||||||
|
ret = append(ret, string(val))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
retLen := len(ret)
|
||||||
|
for key, _ := range ret {
|
||||||
|
for _, val := range chars {
|
||||||
|
ret = append(ret, ret[key]+string(val))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret = ret[retLen:]
|
||||||
|
}
|
||||||
|
ret = dfs(digits[1:], ret)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func letterCombinations(digits string) []string {
|
||||||
|
return dfs(digits, []string{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
for _, val := range letterCombinations("26") {
|
||||||
|
println(val)
|
||||||
|
}
|
||||||
|
}
|
66
archive/algorithm/leetcode/19.c
Normal file
66
archive/algorithm/leetcode/19.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// 删除链表的倒数第N个节点
|
||||||
|
// https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
|
||||||
|
|
||||||
|
// 更优解:双指针
|
||||||
|
struct ListNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct ListNode *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ListNode *removeNthFromEnd(struct ListNode *head, int n)
|
||||||
|
{
|
||||||
|
if (head->next == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
int pos = 0, len = n + 1;
|
||||||
|
struct ListNode **arr = malloc(sizeof(struct ListNode *) * len);
|
||||||
|
struct ListNode *temp = malloc(sizeof(struct ListNode));
|
||||||
|
temp->next = head;
|
||||||
|
head = temp;
|
||||||
|
while (temp != NULL)
|
||||||
|
{
|
||||||
|
arr[pos % len] = temp;
|
||||||
|
pos++;
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
pos = abs((pos - len) % len);
|
||||||
|
if (arr[pos]->next == NULL)
|
||||||
|
{
|
||||||
|
arr[abs((pos - 1) % len)]->next = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
arr[pos]->next = arr[pos]->next->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return head->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct ListNode *head = malloc(sizeof(struct ListNode)), *next = malloc(sizeof(struct ListNode)), *tmp;
|
||||||
|
head->val = 1;
|
||||||
|
head->next = next;
|
||||||
|
|
||||||
|
next->val = 2;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 3;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
next->val = 4;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
next->val = 5;
|
||||||
|
next->next = NULL;
|
||||||
|
|
||||||
|
removeNthFromEnd(head, 1);
|
||||||
|
return 0;
|
||||||
|
}
|
37
archive/algorithm/leetcode/198.c
Normal file
37
archive/algorithm/leetcode/198.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define MAX(a, b) a > b ? a : b;
|
||||||
|
int rob(int* nums, int numsSize) {
|
||||||
|
if (numsSize <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (numsSize == 1) {
|
||||||
|
return nums[0];
|
||||||
|
}
|
||||||
|
int *dp = malloc(sizeof(int) * numsSize), max = 0;
|
||||||
|
dp[0] = nums[0];
|
||||||
|
dp[1] = MAX(nums[0], nums[1]);
|
||||||
|
for (int i = 2; i < numsSize; i++) {
|
||||||
|
dp[i] = MAX(dp[i - 1], dp[i - 2] + nums[i]);
|
||||||
|
}
|
||||||
|
max = MAX(dp[numsSize - 1], dp[numsSize - 2]);
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int rob(int* nums, int numsSize) {
|
||||||
|
int i=0;
|
||||||
|
int *dp=(int*)malloc(sizeof(int)*(numsSize+1));
|
||||||
|
dp[0]=nums[0];
|
||||||
|
dp[1]=nums[0]>nums[1]?nums[0]:nums[1];
|
||||||
|
for(i=2;i<numsSize;++i)
|
||||||
|
{
|
||||||
|
dp[i]=dp[i-1]>dp[i-2]+nums[i]?dp[i-1]:dp[i-2]+nums[i];
|
||||||
|
}
|
||||||
|
return dp[numsSize-1];
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
28
archive/algorithm/leetcode/2.c
Normal file
28
archive/algorithm/leetcode/2.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/23/
|
||||||
|
* 旋转数组
|
||||||
|
*/
|
||||||
|
|
||||||
|
void rotate(int *nums, int numsSize, int k)
|
||||||
|
{
|
||||||
|
int end = 0;
|
||||||
|
for (int i = 0; i < k; i++)
|
||||||
|
{
|
||||||
|
//记录最后一位,数组向后移动一位
|
||||||
|
end = nums[numsSize - 1];
|
||||||
|
for (int n = 0; n < numsSize; n++)
|
||||||
|
{
|
||||||
|
nums[numsSize - n - 1] = nums[numsSize - n - 2];
|
||||||
|
}
|
||||||
|
nums[0]=end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int nums[] = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
rotate(nums, 7, 3);
|
||||||
|
return 0;
|
||||||
|
}
|
39
archive/algorithm/leetcode/20.c
Normal file
39
archive/algorithm/leetcode/20.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef int bool;
|
||||||
|
|
||||||
|
#define false 0
|
||||||
|
#define true 1
|
||||||
|
|
||||||
|
bool isValid(char *s)
|
||||||
|
{
|
||||||
|
int len = strlen(s), stackpos = 0;
|
||||||
|
char stack[len / 2 + 1];
|
||||||
|
int pos = 0;
|
||||||
|
len = len / 2 + 1;
|
||||||
|
while (s[pos] != '\x0')
|
||||||
|
{
|
||||||
|
if (stackpos >= len)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (s[pos] == '(')
|
||||||
|
stack[stackpos++] = ')';
|
||||||
|
else if (s[pos] == '{')
|
||||||
|
stack[stackpos++] = '}';
|
||||||
|
else if (s[pos] == '[')
|
||||||
|
stack[stackpos++] = ']';
|
||||||
|
else if (stackpos <= 0 || s[pos] != stack[--stackpos])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
return stackpos == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
isValid("(([])){}");
|
||||||
|
return 0;
|
||||||
|
}
|
78
archive/algorithm/leetcode/200.c
Normal file
78
archive/algorithm/leetcode/200.c
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
/**
|
||||||
|
* 岛屿的个数
|
||||||
|
* https://lingkou.com/problems/number-of-islands/description/
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记岛屿
|
||||||
|
*/
|
||||||
|
void mark(char **grid, int h, int l, int row, int col)
|
||||||
|
{
|
||||||
|
if (h < 0 || h >= row || l < 0 || l >= col || grid[h][l] != '1')
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
grid[h][l] = '2';
|
||||||
|
//标记旁边的岛 上,下,左,右
|
||||||
|
mark(grid, h + 1, l, row, col);
|
||||||
|
mark(grid, h - 1, l, row, col);
|
||||||
|
mark(grid, h, l + 1, row, col);
|
||||||
|
mark(grid, h, l - 1, row, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
int numIslands(char **grid, int gridRowSize, int gridColSize)
|
||||||
|
{
|
||||||
|
int num = 0;
|
||||||
|
for (int i = 0; i < gridRowSize; i++)
|
||||||
|
{
|
||||||
|
for (int n = 0; n < gridColSize; n++)
|
||||||
|
{
|
||||||
|
if (grid[i][n] == '1')
|
||||||
|
{
|
||||||
|
num++;
|
||||||
|
mark(grid, i, n, gridRowSize, gridColSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
void mark(char** grid, int gridRowSize, int gridColSize, int i, int j) {
|
||||||
|
if(grid[i][j] == '0')
|
||||||
|
return;
|
||||||
|
grid[i][j] = '0';
|
||||||
|
if(i > 0)
|
||||||
|
mark(grid, gridRowSize, gridColSize, i - 1, j);
|
||||||
|
if(j > 0)
|
||||||
|
mark(grid, gridRowSize, gridColSize, i, j - 1);
|
||||||
|
if(i < gridRowSize - 1)
|
||||||
|
mark(grid, gridRowSize, gridColSize, i + 1, j);
|
||||||
|
if(j < gridColSize - 1)
|
||||||
|
mark(grid, gridRowSize, gridColSize, i, j + 1);
|
||||||
|
}
|
||||||
|
int numIslands(char** grid, int gridRowSize, int gridColSize) {
|
||||||
|
int i = 0, j = 0;
|
||||||
|
int result = 0;
|
||||||
|
for(i = 0; i < gridRowSize; i++) {
|
||||||
|
for(j = 0; j < gridColSize; j++) {
|
||||||
|
if(grid[i][j] == '1') {
|
||||||
|
result++;
|
||||||
|
mark(grid, gridRowSize, gridColSize, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char a[4][5] = {{'1', '1', '1', '1', '0'}, {'1', '1', '0', '1', '0'}, {'1', '1', '0', '0', '0'}, {'0', '0', '0', '0', '0'}};
|
||||||
|
printf("%p\n", a);
|
||||||
|
numIslands(a, 4, 5);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
57
archive/algorithm/leetcode/206.c
Normal file
57
archive/algorithm/leetcode/206.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
//https://leetcode-cn.com/problems/reverse-linked-list/
|
||||||
|
//反转链表
|
||||||
|
struct ListNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct ListNode *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ListNode *reverseList(struct ListNode *head)
|
||||||
|
{
|
||||||
|
if (head == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
struct ListNode *tail = head, *next = head->next, *tmp = NULL;
|
||||||
|
while (next != NULL)
|
||||||
|
{
|
||||||
|
tmp = next->next;
|
||||||
|
tail->next = tmp;
|
||||||
|
next->next = head;
|
||||||
|
head = next;
|
||||||
|
next = tmp;
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a->b->c->d->e
|
||||||
|
// b->a->c->d->e
|
||||||
|
// c->b->a->d->e
|
||||||
|
// c->b->a->d->e
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct ListNode *head = malloc(sizeof(struct ListNode)), *next = malloc(sizeof(struct ListNode));
|
||||||
|
head->val = 1;
|
||||||
|
head->next = next;
|
||||||
|
next->val = 2;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 3;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 4;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 5;
|
||||||
|
next->next = NULL;
|
||||||
|
|
||||||
|
reverseList(head);
|
||||||
|
return 0;
|
||||||
|
}
|
102
archive/algorithm/leetcode/21.c
Normal file
102
archive/algorithm/leetcode/21.c
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct ListNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct ListNode *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2)
|
||||||
|
{
|
||||||
|
if (l1 == NULL)
|
||||||
|
{
|
||||||
|
return l2;
|
||||||
|
}
|
||||||
|
if (l2 == NULL)
|
||||||
|
{
|
||||||
|
return l1;
|
||||||
|
}
|
||||||
|
struct ListNode *node = l1, *node2 = l2, *prev = node;
|
||||||
|
while (node != NULL)
|
||||||
|
{
|
||||||
|
if (node2 != NULL && node->val > node2->val)
|
||||||
|
{
|
||||||
|
//第一个链表的值大于第二个链表的值,合并链表直到node2大于node1
|
||||||
|
if (prev == node)
|
||||||
|
{
|
||||||
|
//首节点
|
||||||
|
l1 = node2;
|
||||||
|
while (node2 != NULL && node2->val <= node->val)
|
||||||
|
{
|
||||||
|
prev = node2;
|
||||||
|
node2 = node2->next;
|
||||||
|
}
|
||||||
|
prev->next = node;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (node2 != NULL && node2->val <= node->val)
|
||||||
|
{
|
||||||
|
prev->next = node2;
|
||||||
|
prev = node2;
|
||||||
|
node2 = node2->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
prev->next = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev = node;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
if (node2 != NULL)
|
||||||
|
{
|
||||||
|
prev->next = node2;
|
||||||
|
}
|
||||||
|
return l1;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
|
||||||
|
{
|
||||||
|
struct ListNode *previous = NULL;
|
||||||
|
struct ListNode *t2 = l2;
|
||||||
|
struct ListNode *head = l1;
|
||||||
|
|
||||||
|
while (t2)
|
||||||
|
{
|
||||||
|
if (!l1 || (l1->val > t2->val))
|
||||||
|
{
|
||||||
|
l2 = l2->next;
|
||||||
|
t2->next = l1;
|
||||||
|
|
||||||
|
if (previous)
|
||||||
|
{
|
||||||
|
previous->next = t2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
head = t2;
|
||||||
|
}
|
||||||
|
|
||||||
|
t2 = l2;
|
||||||
|
l1 = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
previous = l1;
|
||||||
|
l1 = l1->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct ListNode *node1 = malloc(sizeof(struct ListNode)), *node2 = malloc(sizeof(struct ListNode));
|
||||||
|
node1->val = 1;
|
||||||
|
node1->next = NULL;
|
||||||
|
node2->val = 3;
|
||||||
|
node2->next = NULL;
|
||||||
|
mergeTwoLists(node1, node2);
|
||||||
|
return 0;
|
||||||
|
}
|
28
archive/algorithm/leetcode/217.c
Normal file
28
archive/algorithm/leetcode/217.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/24/
|
||||||
|
* 数组存在重复
|
||||||
|
*/
|
||||||
|
|
||||||
|
int containsDuplicate(int *nums, int numsSize)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < numsSize - 1; i++)
|
||||||
|
{
|
||||||
|
for (int n = i + 1; n < numsSize; n++)
|
||||||
|
{
|
||||||
|
if (nums[i] == nums[n])
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int nums[] = {1, 2, 3, 4, 5, 6, 7, 7};
|
||||||
|
containsDuplicate(nums, 8);
|
||||||
|
return 0;
|
||||||
|
}
|
48
archive/algorithm/leetcode/22.c
Normal file
48
archive/algorithm/leetcode/22.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void dfs(char** ret, int* num, char* S, int n, int use, int ruse) {
|
||||||
|
if (use >= n && ruse >= n) {
|
||||||
|
//左括号用完了
|
||||||
|
ret[*num] = S;
|
||||||
|
*num += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (use < n) {
|
||||||
|
char* tmp = malloc((n * 2) + 1);
|
||||||
|
memset(tmp, 0, (n * 2) + 1);
|
||||||
|
strcpy(tmp, S);
|
||||||
|
tmp[strlen(S)] = '(';
|
||||||
|
dfs(ret, num, tmp, n, use + 1, ruse);
|
||||||
|
}
|
||||||
|
if (ruse < use) {
|
||||||
|
char* tmp = malloc((n * 2) + 1);
|
||||||
|
memset(tmp, 0, (n * 2) + 1);
|
||||||
|
strcpy(tmp, S);
|
||||||
|
S[strlen(S)] = ')';
|
||||||
|
dfs(ret, num, S, n, use, ruse + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Note: The returned array must be malloced, assume caller calls free().
|
||||||
|
*/
|
||||||
|
char** generateParenthesis(int n, int* returnSize) {
|
||||||
|
char** ret = malloc(pow(n, n) * sizeof(char*));
|
||||||
|
char* S = malloc((n * 2) + 1);
|
||||||
|
memset(S, 0, (n * 2) + 1);
|
||||||
|
dfs(ret, returnSize, S, n, 0, 0);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
// 1 2 5 14 42 132
|
||||||
|
int main() {
|
||||||
|
int num = 0;
|
||||||
|
char** ret = generateParenthesis(10, &num);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
printf("%s\n", ret[i]);
|
||||||
|
}
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
27
archive/algorithm/leetcode/22.go
Normal file
27
archive/algorithm/leetcode/22.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func dfs(str string, ret *[]string, n, l, r int) {
|
||||||
|
if r >= n {
|
||||||
|
*ret = append(*ret, str)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if l < n {
|
||||||
|
dfs(str+"(", ret, n, l+1, r)
|
||||||
|
}
|
||||||
|
if r < l {
|
||||||
|
dfs(str+")", ret, n, l, r+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateParenthesis(n int) []string {
|
||||||
|
ret := []string{}
|
||||||
|
dfs("", &ret, n, 0, 0)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ret := generateParenthesis(3)
|
||||||
|
for _, val := range ret {
|
||||||
|
println(val + " ")
|
||||||
|
}
|
||||||
|
}
|
27
archive/algorithm/leetcode/231.c
Normal file
27
archive/algorithm/leetcode/231.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define bool int
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
bool isPowerOfTwo(int num)
|
||||||
|
{
|
||||||
|
while (num % 2 == 0)
|
||||||
|
{
|
||||||
|
num /= 2;
|
||||||
|
if (num == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return num == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
isPowerOfTwo(27);
|
||||||
|
isPowerOfTwo(4);
|
||||||
|
isPowerOfTwo(16);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
44
archive/algorithm/leetcode/242.c
Normal file
44
archive/algorithm/leetcode/242.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/valid-anagram/description/
|
||||||
|
* 有效的字母异位词
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool isAnagram(char *s, char *t)
|
||||||
|
{
|
||||||
|
int hash[26] = {0}, hash2[26] = {0}, i = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if (s[i] == '\x0')
|
||||||
|
{
|
||||||
|
if (t[i] == '\x0')
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (t[i] == '\x0')
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
hash[s[i] - 'a']++;
|
||||||
|
hash2[t[i] - 'a']++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 26; i++)
|
||||||
|
{
|
||||||
|
if (hash[i] != hash2[i])
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
14
archive/algorithm/leetcode/268.go
Normal file
14
archive/algorithm/leetcode/268.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func missingNumber(nums []int) int {
|
||||||
|
ret := 0
|
||||||
|
len := len(nums)
|
||||||
|
for i := 0; i < len; i++ {
|
||||||
|
ret = ret ^ i ^ nums[i]
|
||||||
|
}
|
||||||
|
return ret ^ len
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(missingNumber([]int{9, 6, 4, 2, 3, 5, 7, 0, 1}))
|
||||||
|
}
|
40
archive/algorithm/leetcode/27.c
Normal file
40
archive/algorithm/leetcode/27.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
//01223042
|
||||||
|
//01230422
|
||||||
|
int removeElement(int *nums, int numsSize, int val)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
for (int i = 0; i < numsSize - ret; i++)
|
||||||
|
{
|
||||||
|
if (nums[i] == val)
|
||||||
|
{
|
||||||
|
for (int n = i; n < numsSize - ret - 1; n++)
|
||||||
|
{
|
||||||
|
nums[n] = nums[n + 1];
|
||||||
|
}
|
||||||
|
ret++;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return numsSize - ret;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int removeElement(int* nums, int numsSize, int val) {
|
||||||
|
int pos = 0;
|
||||||
|
for(int i=0; i<numsSize; i++) {
|
||||||
|
if(nums[i] != val) {
|
||||||
|
nums[pos++] = nums[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a[] = {3, 2, 2, 3};
|
||||||
|
|
||||||
|
removeElement(a, 4, 3);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
73
archive/algorithm/leetcode/28.c
Normal file
73
archive/algorithm/leetcode/28.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/implement-strstr/description/
|
||||||
|
* 实现strStr()
|
||||||
|
*/
|
||||||
|
//a
|
||||||
|
int strStr(char *haystack, char *needle)
|
||||||
|
{
|
||||||
|
if (*needle == '\x0')
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int pos = 0;
|
||||||
|
int sublen = strlen(needle);
|
||||||
|
while (*haystack != '\x0')
|
||||||
|
{
|
||||||
|
if (*haystack == *needle)
|
||||||
|
{
|
||||||
|
char *sub = haystack;
|
||||||
|
char *tmp = needle;
|
||||||
|
int len = 0;
|
||||||
|
while (*sub == *tmp && len < sublen)
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
sub++;
|
||||||
|
tmp++;
|
||||||
|
}
|
||||||
|
if (len == sublen)
|
||||||
|
{
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
haystack++;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int strStr(char* haystack, char* needle) {
|
||||||
|
int i=0, j=0, temp;
|
||||||
|
|
||||||
|
if( *needle == 0 ) return 0;
|
||||||
|
if( strlen(haystack) < strlen(needle) ) return -1;
|
||||||
|
|
||||||
|
while( haystack[i] != 0) {
|
||||||
|
while( haystack[i] != 0 && haystack[i] != needle[j] ) i++;
|
||||||
|
if( haystack[i] == 0 ) return -1;
|
||||||
|
i++; j++;
|
||||||
|
temp = i;
|
||||||
|
while( haystack[i] != 0 && needle[j] != 0 ) {
|
||||||
|
if( haystack[i] != needle[j] ) {
|
||||||
|
j = 0;
|
||||||
|
i = temp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++; j++;
|
||||||
|
}
|
||||||
|
// printf("haystack = %d, sizeof(needle) = %d, haystackTmp = %d", haystack, strlen(needleTmp), haystackTmp);
|
||||||
|
if( needle[j] == 0 ) return (i - j);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", strStr("hello", "lll"));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
58
archive/algorithm/leetcode/283.c
Normal file
58
archive/algorithm/leetcode/283.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移动零
|
||||||
|
* https://leetcode-cn.com/problems/move-zeroes/description/
|
||||||
|
*/
|
||||||
|
void moveZeroes(int *nums, int numsSize)
|
||||||
|
{
|
||||||
|
int zeroNumber = 0;
|
||||||
|
for (int i = numsSize - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (nums[i] == 0) //等于0时,往后移动
|
||||||
|
{
|
||||||
|
for (int n = i; n < numsSize - zeroNumber - 1; n++) //后面的数字直接的往前赋值
|
||||||
|
{
|
||||||
|
nums[n] = nums[n + 1];
|
||||||
|
}
|
||||||
|
nums[numsSize - zeroNumber - 1] = 0;
|
||||||
|
zeroNumber++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
void moveZeroes(int* nums, int numsSize) {
|
||||||
|
int zeroNum = 0;
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < numsSize; i++) {
|
||||||
|
if (nums[i] == 0) {
|
||||||
|
zeroNum++;
|
||||||
|
} else {
|
||||||
|
nums[i-zeroNum] = nums[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i = numsSize-zeroNum; i < numsSize; i++) {
|
||||||
|
nums[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int tmp[] = {0, 1, 0, 3, 12};
|
||||||
|
moveZeroes(tmp, sizeof(tmp) / sizeof(int));
|
||||||
|
print_r(tmp, sizeof(tmp) / sizeof(int));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
127
archive/algorithm/leetcode/29.c
Normal file
127
archive/algorithm/leetcode/29.c
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/divide-two-integers/description/
|
||||||
|
* 两数相除,不能直接使用乘法除法
|
||||||
|
*/
|
||||||
|
|
||||||
|
int add(int num1, int num2)
|
||||||
|
{
|
||||||
|
while (num2)
|
||||||
|
{
|
||||||
|
int tmp = num1 ^ num2;
|
||||||
|
num2 = (num1 & num2) << 1;
|
||||||
|
num1 = tmp;
|
||||||
|
}
|
||||||
|
return num1;
|
||||||
|
}
|
||||||
|
int sub(int num1, int num2)
|
||||||
|
{
|
||||||
|
return add(num1, add(~num2, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int negative(int num) //判断负数
|
||||||
|
{
|
||||||
|
return (num >> 31) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int abs_(int num)
|
||||||
|
{
|
||||||
|
if (negative(num)) //右移31位判断正负
|
||||||
|
{
|
||||||
|
return add(~num, 1); //负数
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
int divide(int dividend, int divisor)
|
||||||
|
{
|
||||||
|
int flag = 1;
|
||||||
|
if (negative(dividend) == negative(divisor))
|
||||||
|
{
|
||||||
|
flag = 0;
|
||||||
|
}
|
||||||
|
unsigned int x = abs_(dividend);
|
||||||
|
unsigned int y = abs_(divisor);
|
||||||
|
unsigned int result = 0;
|
||||||
|
for (int i = 31; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if ((x >> i) >= y)
|
||||||
|
{
|
||||||
|
result = add(result, 1 << i);
|
||||||
|
x = sub(x, y << i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
//负数
|
||||||
|
if (result > 2147483648)
|
||||||
|
{
|
||||||
|
return 2147483647;
|
||||||
|
}
|
||||||
|
return add(~result,1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//正数
|
||||||
|
if (result > 2147483647)
|
||||||
|
{
|
||||||
|
return 2147483647;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
对于int a = 0x80000000;
|
||||||
|
long long int b = -a;
|
||||||
|
而言,b依然是-2147483648,只不过是长长整型的-2147483648
|
||||||
|
可能是右边的-a运算,由于预期的结果溢出,所以就没有做负号运算
|
||||||
|
原先认为的是-a会导致溢出从而结果为0,实际测试发现不是
|
||||||
|
long long int d = ((long long int)dividend > 0) ? (long long int)dividend : -(long long int)dividend;
|
||||||
|
long long int div = ((long long int)divisor > 0) ? (long long int)divisor : -(long long int)divisor;
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
int divide(int dividend, int divisor) {
|
||||||
|
if (divisor == 1)
|
||||||
|
return dividend;
|
||||||
|
|
||||||
|
if (divisor == -1)
|
||||||
|
{
|
||||||
|
if (dividend == (int)0x80000000)
|
||||||
|
return 0x7FFFFFFF;
|
||||||
|
else
|
||||||
|
return -dividend;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cnt = 0, factor = 0, i = 0;
|
||||||
|
long long int d = dividend;
|
||||||
|
long long int div = divisor;
|
||||||
|
|
||||||
|
d = (d > 0) ? d : -d;
|
||||||
|
div = (div > 0) ? div : -div;
|
||||||
|
|
||||||
|
if ((dividend ^ divisor) & 0x80000000)
|
||||||
|
factor = -1;
|
||||||
|
else
|
||||||
|
factor = 1;
|
||||||
|
|
||||||
|
while (d >= div)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (d >= (div << i))
|
||||||
|
{
|
||||||
|
d -= (div << i);
|
||||||
|
cnt += (1 << i++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return factor*cnt;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", divide(-2147483648, -1));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
46
archive/algorithm/leetcode/3.c
Normal file
46
archive/algorithm/leetcode/3.c
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/description/
|
||||||
|
* 无重复字符的最长子串
|
||||||
|
*/
|
||||||
|
|
||||||
|
int lengthOfLongestSubstring(char *s)
|
||||||
|
{
|
||||||
|
//初始化变量
|
||||||
|
int len = strlen(s);
|
||||||
|
int *hash = malloc(128 * sizeof(int));
|
||||||
|
memset(hash, -1, sizeof(int) * 128);
|
||||||
|
int maxlen = 0, nowlen = 0;
|
||||||
|
for (int i = 0; i < len; i++, nowlen++)
|
||||||
|
{
|
||||||
|
if (hash[s[i]] != -1 && hash[s[i]] >= i - nowlen) //利用一个数组增加来判断是否重复
|
||||||
|
{
|
||||||
|
if (nowlen > maxlen)
|
||||||
|
{
|
||||||
|
maxlen = nowlen;
|
||||||
|
}
|
||||||
|
//上一次位置:hash[s[i],现在这一次位置:i
|
||||||
|
//重新计算长度 nowlen=i-hash[s[i]]-1
|
||||||
|
nowlen = i - hash[s[i]] - 1;
|
||||||
|
}
|
||||||
|
hash[s[i]] = i;//记录位置
|
||||||
|
}
|
||||||
|
if (nowlen > maxlen) //最后的没有判断
|
||||||
|
{
|
||||||
|
maxlen = nowlen;
|
||||||
|
}
|
||||||
|
return maxlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", lengthOfLongestSubstring("abcabcbb"));
|
||||||
|
printf("%d\n", lengthOfLongestSubstring("aab"));
|
||||||
|
printf("%d\n", lengthOfLongestSubstring("dvdf"));
|
||||||
|
printf("%d\n", lengthOfLongestSubstring("bbbbb"));
|
||||||
|
printf("%d\n", lengthOfLongestSubstring("pwwkew"));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
63
archive/algorithm/leetcode/303.c
Normal file
63
archive/algorithm/leetcode/303.c
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int* nums;
|
||||||
|
int* sum;
|
||||||
|
} NumArray;
|
||||||
|
|
||||||
|
NumArray* numArrayCreate(int* nums, int numsSize) {
|
||||||
|
NumArray* obj = malloc(sizeof(NumArray));
|
||||||
|
obj->nums = nums;
|
||||||
|
obj->sum = malloc(sizeof(int) * numsSize);
|
||||||
|
obj->sum[0] = nums[0];
|
||||||
|
for (int i = 1; i < numsSize; i++) {
|
||||||
|
obj->sum[i] = obj->sum[i - 1] + nums[i];
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
int numArraySumRange(NumArray* obj, int i, int j) {
|
||||||
|
return (obj->sum[j] - obj->sum[i])+obj->nums[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
void numArrayFree(NumArray* obj) { free(obj); }
|
||||||
|
/*
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int *nums;
|
||||||
|
int numsSize;
|
||||||
|
|
||||||
|
} NumArray;
|
||||||
|
|
||||||
|
NumArray *numArrayCreate(int *nums, int numsSize) {
|
||||||
|
|
||||||
|
|
||||||
|
int *a = malloc(numsSize * sizeof(int) + 1);
|
||||||
|
a[0] = 0;
|
||||||
|
for (int k = 0; k < numsSize; k++) {
|
||||||
|
a[k + 1] = a[k] + nums[k];
|
||||||
|
}
|
||||||
|
|
||||||
|
NumArray *arr = malloc(sizeof(NumArray));
|
||||||
|
arr->nums = a;
|
||||||
|
arr->numsSize = numsSize;
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int numArraySumRange(NumArray *obj, int i, int j) {
|
||||||
|
return obj->nums[j + 1] - obj->nums[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
void numArrayFree(NumArray *obj) {
|
||||||
|
free(obj->nums);
|
||||||
|
free(obj);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your NumArray struct will be instantiated and called as such:
|
||||||
|
* struct NumArray* obj = numArrayCreate(nums, numsSize);
|
||||||
|
* int param_1 = numArraySumRange(obj, i, j);
|
||||||
|
* numArrayFree(obj);
|
||||||
|
*/
|
36
archive/algorithm/leetcode/326.c
Normal file
36
archive/algorithm/leetcode/326.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define bool int
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
|
||||||
|
bool isPowerOfThree(int n)
|
||||||
|
{
|
||||||
|
double a = log10l(n) / log10l(3);
|
||||||
|
if (a - ((int)a) == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
第二种不用循环的解法
|
||||||
|
bool isPowerOfThree(int n) {
|
||||||
|
if(n <= 0) return false;
|
||||||
|
while ( n % 3 == 0) {
|
||||||
|
n /= 3;
|
||||||
|
if( n==0 ) return false;
|
||||||
|
}
|
||||||
|
return n == 1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
isPowerOfThree(27);
|
||||||
|
isPowerOfThree(243);
|
||||||
|
isPowerOfThree(4782968);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
53
archive/algorithm/leetcode/33.c
Normal file
53
archive/algorithm/leetcode/33.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// https://leetcode-cn.com/problems/search-in-rotated-sorted-array/
|
||||||
|
// 搜索旋转排序数组
|
||||||
|
|
||||||
|
int search(int *nums, int numsSize, int target)
|
||||||
|
{
|
||||||
|
int start = 0, mid = 0, end = numsSize - 1;
|
||||||
|
while (start <= end)
|
||||||
|
{
|
||||||
|
mid = start + ((end - start) >> 1);
|
||||||
|
if (nums[mid] == target)
|
||||||
|
{
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
if (nums[start] <= nums[mid])
|
||||||
|
{
|
||||||
|
//前半部分有序
|
||||||
|
if (target < nums[mid] && nums[start] <= target)
|
||||||
|
{
|
||||||
|
//目标数在前半部分中
|
||||||
|
end = mid - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//在无序的后半部分
|
||||||
|
start = mid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//后半部分有序
|
||||||
|
if (target <= nums[end] && nums[mid] < target)
|
||||||
|
{
|
||||||
|
//目标数在后半部分中
|
||||||
|
start = mid + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//在无序的前半部分
|
||||||
|
end = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a[] = {4, 5, 6, 7, 0, 1, 2};
|
||||||
|
search(a, sizeof(a) / sizeof(int), 0);
|
||||||
|
return 0;
|
||||||
|
}
|
36
archive/algorithm/leetcode/338.c
Normal file
36
archive/algorithm/leetcode/338.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
// 0 1
|
||||||
|
/**
|
||||||
|
* Return an array of size *returnSize.
|
||||||
|
* Note: The returned array must be malloced, assume caller calls free().
|
||||||
|
*/
|
||||||
|
int* countBits(int num, int* returnSize) {
|
||||||
|
int *dp = malloc(sizeof(int) * (num + 1)), count = 2, pos = 0;
|
||||||
|
dp[0] = 0;
|
||||||
|
dp[1] = 1;
|
||||||
|
for (int i = 2; i <= num; i++) {
|
||||||
|
pos = i % count;
|
||||||
|
if (!pos && count != i) {
|
||||||
|
count = count * 2;
|
||||||
|
}
|
||||||
|
dp[i] = dp[pos] + 1;
|
||||||
|
}
|
||||||
|
*returnSize = num + 1;
|
||||||
|
return dp;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int* countBits(int num, int* rS) {
|
||||||
|
*rS = num+1;
|
||||||
|
int *res = calloc(num+2, sizeof(int));
|
||||||
|
res[0] = 0; res[1] = 1;
|
||||||
|
int i=2, j;
|
||||||
|
while(i<=num) {
|
||||||
|
for(j=i; j<=num&&j<(i<<1); j++) {
|
||||||
|
res[j] = 1+res[j-i];
|
||||||
|
}
|
||||||
|
i <<= 1;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
*/
|
27
archive/algorithm/leetcode/342.c
Normal file
27
archive/algorithm/leetcode/342.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define bool int
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
bool isPowerOfFour(int num)
|
||||||
|
{
|
||||||
|
while (num % 4 == 0)
|
||||||
|
{
|
||||||
|
num /= 4;
|
||||||
|
if (num == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return num == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
isPowerOfFour(27);
|
||||||
|
isPowerOfFour(4);
|
||||||
|
isPowerOfFour(16);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
36
archive/algorithm/leetcode/344.c
Normal file
36
archive/algorithm/leetcode/344.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/reverse-string/description/
|
||||||
|
* 反转字符串
|
||||||
|
*/
|
||||||
|
char *reverseString(char *s)
|
||||||
|
{
|
||||||
|
int len = strlen(s);
|
||||||
|
for (int i = 0; i < len/2; i++)
|
||||||
|
{
|
||||||
|
s[i] ^= s[len - 1 - i];
|
||||||
|
s[len - 1 - i] ^= s[i];
|
||||||
|
s[i] ^= s[len - 1 - i];
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char s[] = "hello";
|
||||||
|
printf("%s\n", reverseString(s));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
86
archive/algorithm/leetcode/350.c
Normal file
86
archive/algorithm/leetcode/350.c
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/description/
|
||||||
|
* 两个数组的交集 II
|
||||||
|
*/
|
||||||
|
|
||||||
|
int *intersect(int *nums1, int nums1Size, int *nums2, int nums2Size, int *returnSize)
|
||||||
|
{
|
||||||
|
int *ret = 0, len = nums1Size;
|
||||||
|
if (nums1Size > nums2Size) //num1换为长的
|
||||||
|
{
|
||||||
|
// int *tmp = nums1;
|
||||||
|
// nums1 = nums2;
|
||||||
|
// nums2 = tmp;
|
||||||
|
len = nums2Size;
|
||||||
|
}
|
||||||
|
ret = malloc(sizeof(int) * len);
|
||||||
|
len = 0;
|
||||||
|
for (int i = 0; i < nums1Size; i++)
|
||||||
|
{
|
||||||
|
for (int n = len; n < nums2Size; n++) //从len开始
|
||||||
|
{
|
||||||
|
if (nums1[i] == nums2[n]) //每当相等时,将这个位置的数字和len位置的对调
|
||||||
|
{
|
||||||
|
nums2[n] = nums2[len];
|
||||||
|
nums2[len] = nums1[i];
|
||||||
|
ret[len++] = nums1[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*returnSize = len;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
//大佬的写法
|
||||||
|
int cmp(const void * a, const void * b)
|
||||||
|
{
|
||||||
|
if(*(int *)a == INT_MIN)
|
||||||
|
return (*(int *)a);
|
||||||
|
else if (*(int *)b == INT_MIN)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return (*(int*)a - *(int*)b);
|
||||||
|
}
|
||||||
|
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
|
||||||
|
qsort(nums1,nums1Size,sizeof(int),cmp);
|
||||||
|
qsort(nums2,nums2Size,sizeof(int),cmp);
|
||||||
|
int len = nums1Size < nums2Size ? nums1Size : nums2Size;
|
||||||
|
int * res = malloc(sizeof(int)*len);
|
||||||
|
int length = 0;
|
||||||
|
int i = 0 ,j = 0;
|
||||||
|
while(i<nums1Size && j<nums2Size)
|
||||||
|
{
|
||||||
|
if(nums1[i]>nums2[j])
|
||||||
|
j++;
|
||||||
|
else if(nums1[i]<nums2[j])
|
||||||
|
i++;
|
||||||
|
else if(nums1[i] == nums2[j])
|
||||||
|
{
|
||||||
|
res[length++] = nums1[i];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*returnSize = length;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// int a1[] = {3, 1, 2};
|
||||||
|
// int a2[] = {1};
|
||||||
|
int a1[] = {61, 24, 20, 58, 95, 53, 17, 32, 45, 85, 70, 20, 83, 62, 35, 89, 5, 95, 12, 86, 58, 77, 30, 64, 46, 13, 5, 92, 67, 40, 20, 38, 31, 18, 89, 85, 7, 30, 67, 34, 62, 35, 47, 98, 3, 41, 53, 26, 66, 40, 54, 44, 57, 46, 70, 60, 4, 63, 82, 42, 65, 59, 17, 98, 29, 72, 1, 96, 82, 66, 98, 6, 92, 31, 43, 81, 88, 60, 10, 55, 66, 82, 0, 79, 11, 81};
|
||||||
|
int a2[] = {5, 25, 4, 39, 57, 49, 93, 79, 7, 8, 49, 89, 2, 7, 73, 88, 45, 15, 34, 92, 84, 38, 85, 34, 16, 6, 99, 0, 2, 36, 68, 52, 73, 50, 77, 44, 61, 48};
|
||||||
|
int len = 0;
|
||||||
|
intersect(a1, sizeof(a1) / sizeof(int), a2, sizeof(a2) / sizeof(int), &len);
|
||||||
|
printf("%d\n", len);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
118
archive/algorithm/leetcode/36.c
Normal file
118
archive/algorithm/leetcode/36.c
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/valid-sudoku/description/
|
||||||
|
* 有效的数独
|
||||||
|
*/
|
||||||
|
|
||||||
|
int isValidSudoku(char **board, int boardRowSize, int boardColSize)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 9; i++)
|
||||||
|
{
|
||||||
|
int hash[9] = {0};
|
||||||
|
for (int n = 0; n < 9; n++)
|
||||||
|
{
|
||||||
|
if (board[i][n] != '.')
|
||||||
|
{
|
||||||
|
hash[board[i][n] - 49]++;
|
||||||
|
if (hash[board[i][n] - 49] > 1)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 9; i++)
|
||||||
|
{
|
||||||
|
int hash[9] = {0};
|
||||||
|
for (int n = 0; n < 9; n++)
|
||||||
|
{
|
||||||
|
if (board[n][i] != '.')
|
||||||
|
{
|
||||||
|
hash[board[n][i] - 49]++;
|
||||||
|
if (hash[board[n][i] - 49] > 1)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//对小块进行校验
|
||||||
|
for (int p1 = 0; p1 < 3; p1++)
|
||||||
|
{
|
||||||
|
for (int p = 0; p < 3; p++)
|
||||||
|
{
|
||||||
|
int hash[9] = {0};
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
for (int n = 0; n < 3; n++)
|
||||||
|
{
|
||||||
|
int x = p1 * 3 + i, y = p * 3 + n;
|
||||||
|
if (board[x][y] != '.')
|
||||||
|
{
|
||||||
|
hash[board[x][y] - 49]++;
|
||||||
|
if (hash[board[x][y] - 49] > 1)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
|
||||||
|
int i, j, tmp, l;
|
||||||
|
int flag[27][9];
|
||||||
|
for (i=0; i<27; i++) {
|
||||||
|
for (j=0; j<9; j++) {
|
||||||
|
flag[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i=0; i<boardRowSize; i++ ) {
|
||||||
|
for (j=0; j<boardColSize; j++) {
|
||||||
|
if (board[i][j] != '.') {
|
||||||
|
tmp = board[i][j] - 48 -1;
|
||||||
|
if ( flag[i][tmp] != 0 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
flag[i][tmp] = 1;
|
||||||
|
if ( flag[j+9][tmp] != 0 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
flag[j+9][tmp] = 1;
|
||||||
|
l = (i/3)*3+j/3+18;
|
||||||
|
if ( flag[l][tmp] != 0 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
flag[l][tmp] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", 9 / 9);
|
||||||
|
char a[9][9] = {
|
||||||
|
{'5', '3', '.', '.', '7', '.', '.', '.', '.'}, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
|
||||||
|
isValidSudoku(a, 9, 9);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
93
archive/algorithm/leetcode/38.c
Normal file
93
archive/algorithm/leetcode/38.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/count-and-say/description/
|
||||||
|
* 报数
|
||||||
|
*/
|
||||||
|
//111221
|
||||||
|
//312211
|
||||||
|
//13112221
|
||||||
|
char *countAndSay(int n)
|
||||||
|
{
|
||||||
|
char *s = malloc(n*n*n), *tmp_ret = malloc(n*n*n);
|
||||||
|
int len = 1;
|
||||||
|
memset(s, 0, n*n*n);
|
||||||
|
memset(tmp_ret, 0, n*n*n);
|
||||||
|
strcpy(s, "1");
|
||||||
|
for (int i = 1; i < n; i++)
|
||||||
|
{
|
||||||
|
char last_chr = s[0];
|
||||||
|
int last_num=1;
|
||||||
|
for (int n = 1; n < len; n++)
|
||||||
|
{
|
||||||
|
if (s[n] != last_chr)
|
||||||
|
{
|
||||||
|
char tmp[3] = {0};
|
||||||
|
tmp[0] = '0' + last_num;
|
||||||
|
tmp[1] = last_chr;
|
||||||
|
strcat(tmp_ret, tmp);
|
||||||
|
last_chr = s[n];
|
||||||
|
last_num = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
last_num++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char tmp[3] = {0};
|
||||||
|
tmp[0] = '0' + last_num;
|
||||||
|
tmp[1] = last_chr;
|
||||||
|
strcat(tmp_ret, tmp);
|
||||||
|
strcpy(s, tmp_ret);
|
||||||
|
memset(tmp_ret, 0, n*n*n);
|
||||||
|
len = strlen(s);
|
||||||
|
};
|
||||||
|
free(tmp_ret);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
char* countAndSay(int n) {
|
||||||
|
char *num = (char*)malloc(sizeof(char)*2);
|
||||||
|
char *num1, *p, *q;
|
||||||
|
int index;
|
||||||
|
// char c;
|
||||||
|
|
||||||
|
num[0] = '1';
|
||||||
|
num[1] = '\0';
|
||||||
|
while(n>1){
|
||||||
|
index = 0;
|
||||||
|
p = q = num;
|
||||||
|
num1 = (char*)malloc(sizeof(char)*strlen(num)*2+1);
|
||||||
|
|
||||||
|
while(*q){
|
||||||
|
if(*p != (*(q + 1))){
|
||||||
|
num1[index++] = q - p + '1';
|
||||||
|
num1[index++] = *p;
|
||||||
|
p = q + 1;
|
||||||
|
}
|
||||||
|
q++;
|
||||||
|
}
|
||||||
|
num1[index] = '\0';
|
||||||
|
|
||||||
|
free(num);
|
||||||
|
num = num1;
|
||||||
|
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
countAndSay(40);
|
||||||
|
countAndSay(20);
|
||||||
|
countAndSay(10);
|
||||||
|
countAndSay(12);
|
||||||
|
countAndSay(14);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
71
archive/algorithm/leetcode/387.c
Normal file
71
archive/algorithm/leetcode/387.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/
|
||||||
|
* 字符串中的第一个唯一字符
|
||||||
|
*/
|
||||||
|
|
||||||
|
int firstUniqChar(char *s)
|
||||||
|
{
|
||||||
|
struct hash_pos
|
||||||
|
{
|
||||||
|
char num;
|
||||||
|
short pos;
|
||||||
|
};
|
||||||
|
struct hash_pos hash[26] = {0};
|
||||||
|
int pos = -1, i = 0;
|
||||||
|
while (s[i] != '\x0')
|
||||||
|
{
|
||||||
|
hash[s[i] - 97].num += 1;
|
||||||
|
hash[s[i] - 97].pos = i++;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 26; i++)
|
||||||
|
{
|
||||||
|
if (hash[i].num == 1 && (pos > hash[i].pos || pos == -1))
|
||||||
|
{
|
||||||
|
pos = hash[i].pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int firstUniqChar(char* s) {
|
||||||
|
int i,j;
|
||||||
|
int len=strlen(s);
|
||||||
|
int b[26];
|
||||||
|
for(int k=0;k<26;k++)
|
||||||
|
{
|
||||||
|
b[k]=0;
|
||||||
|
}
|
||||||
|
for(i=0;i<len;i++)
|
||||||
|
{
|
||||||
|
b[s[i]-'a']+=1;
|
||||||
|
}
|
||||||
|
for(j=0;j<len;j++)
|
||||||
|
{
|
||||||
|
if(b[s[j]-'a']==1)
|
||||||
|
{
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char s[] = "qq";
|
||||||
|
firstUniqChar(s);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
23
archive/algorithm/leetcode/4.c
Normal file
23
archive/algorithm/leetcode/4.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/25/
|
||||||
|
* 只出现一次的数字
|
||||||
|
*/
|
||||||
|
int containsDuplicate(int *nums, int numsSize)
|
||||||
|
{
|
||||||
|
int ret=0;
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
{
|
||||||
|
ret^=nums[i];
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int nums[] = {1, 2,3,3,8,2,1,5,9,4,9,5,4};
|
||||||
|
int b = containsDuplicate(nums, sizeof(nums) / 4);
|
||||||
|
printf("%d\n", b);
|
||||||
|
return 0;
|
||||||
|
}
|
34
archive/algorithm/leetcode/413.c
Normal file
34
archive/algorithm/leetcode/413.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
int numberOfArithmeticSlices(int* A, int ASize) {
|
||||||
|
int *dp = malloc(sizeof(int) * ASize), ret = 0;
|
||||||
|
memset(dp, 0, sizeof(int) * ASize);
|
||||||
|
dp[0] = 0;
|
||||||
|
dp[1] = 0;
|
||||||
|
for (int i = 2; i < ASize; i++) {
|
||||||
|
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
|
||||||
|
dp[i] = dp[i - 1]+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int i=2;i<ASize;i++){
|
||||||
|
ret+=dp[i];
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int numberOfArithmeticSlices(int* A, int ASize) {
|
||||||
|
if(ASize<3) return 0;
|
||||||
|
int pred = A[1]-A[0];
|
||||||
|
int i;
|
||||||
|
int sum = 0;
|
||||||
|
int count = 2;
|
||||||
|
for(i=2; i<ASize; i++) {
|
||||||
|
if(A[i]-A[i-1] == pred) count++;
|
||||||
|
else {
|
||||||
|
if(count>2) sum += count*(count-3)/2+1;
|
||||||
|
count = 2;
|
||||||
|
pred = A[i]-A[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(count>2) sum += count*(count-3)/2+1;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
*/
|
94
archive/algorithm/leetcode/48.c
Normal file
94
archive/algorithm/leetcode/48.c
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/rotate-image/description/
|
||||||
|
* 旋转图像
|
||||||
|
*/
|
||||||
|
|
||||||
|
//123
|
||||||
|
//456
|
||||||
|
//789
|
||||||
|
|
||||||
|
//963
|
||||||
|
//852
|
||||||
|
//741
|
||||||
|
|
||||||
|
//741
|
||||||
|
//852
|
||||||
|
//963
|
||||||
|
|
||||||
|
//1,0
|
||||||
|
//2,1
|
||||||
|
|
||||||
|
//0,0 2,2
|
||||||
|
//2,0 2,0
|
||||||
|
//1,0 2,1
|
||||||
|
//0,1 1,2
|
||||||
|
|
||||||
|
void rotate(int matrix[3][3], int matrixRowSize, int *matrixColSizes)
|
||||||
|
{
|
||||||
|
//先换对角
|
||||||
|
int count = matrixRowSize;
|
||||||
|
for (int i = 0; i < count; i++) //对角线交换元素
|
||||||
|
{
|
||||||
|
for (int n = 0; n < count - i; n++)
|
||||||
|
{
|
||||||
|
if (i == count - 1 - n &&n == count - 1 - i)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
matrix[i][n] ^= matrix[count - 1 - n][count - 1 - i];
|
||||||
|
matrix[count - 1 - n][count - 1 - i] ^= matrix[i][n];
|
||||||
|
matrix[i][n] ^= matrix[count - 1 - n][count - 1 - i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//以中间为轴换
|
||||||
|
for (int i = 0; i < count / 2; i++)
|
||||||
|
{
|
||||||
|
for (int n = 0; n < count; n++)
|
||||||
|
{
|
||||||
|
matrix[i][n] ^= matrix[count-1 - i][n];
|
||||||
|
matrix[count-1 - i][n] ^= matrix[i][n];
|
||||||
|
matrix[i][n] ^= matrix[count-1 - i][n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
void rotate(int** matrix, int matrixRowSize, int *matrixColSizes) {
|
||||||
|
int i = 0, j = 0, key;
|
||||||
|
int n =matrixRowSize;
|
||||||
|
for(i = 0; i < n / 2; i++){
|
||||||
|
for(j = i; j < n-1-i; j++){
|
||||||
|
key = matrix[i][j];
|
||||||
|
matrix[i][j] = matrix[n - 1 - j][i];
|
||||||
|
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
|
||||||
|
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
|
||||||
|
matrix[j][n - 1 - i] = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a[3][3] = {{1, 2, 3},
|
||||||
|
{4, 5, 6},
|
||||||
|
{7, 8, 9}};
|
||||||
|
int *b = malloc(4);
|
||||||
|
rotate(a, 3, b);
|
||||||
|
printf("%d\n", 6 ^ 6);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
136
archive/algorithm/leetcode/5.c
Normal file
136
archive/algorithm/leetcode/5.c
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最长回文子串
|
||||||
|
* https://leetcode-cn.com/problems/longest-palindromic-substring/description/
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 看网上的Manacher算法
|
||||||
|
*/
|
||||||
|
char *longestPalindrome(char *s)
|
||||||
|
{
|
||||||
|
if (strlen(s) <= 1)
|
||||||
|
{
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
char new_s[2010] = {0};
|
||||||
|
int p[2010] = {0};
|
||||||
|
// memset(p, 0, sizeof(int) * (strlen(s) * 2 + 1)); //创建p
|
||||||
|
// memset(new_s, 0, strlen(s) * 2 + 1); //构建一个新的字符串
|
||||||
|
//给字符串填充#分割
|
||||||
|
int len = strlen(s);
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
new_s[i * 2] = '#';
|
||||||
|
new_s[(i * 2) + 1] = s[i];
|
||||||
|
}
|
||||||
|
new_s[len * 2] = '#';
|
||||||
|
p[0] = 1;
|
||||||
|
int id = 0, mx = 0, max_pos = 0, max_len = 0;
|
||||||
|
for (int i = 1; i < strlen(new_s); i++)
|
||||||
|
{
|
||||||
|
if (i < mx)
|
||||||
|
{
|
||||||
|
p[i] = p[2 * id - i] > mx - i ? mx - i : p[2 * id - i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p[i] = 1;
|
||||||
|
}
|
||||||
|
while (i - p[i] >= 0 && new_s[i - p[i]] == new_s[i + p[i]])
|
||||||
|
{
|
||||||
|
p[i]++;
|
||||||
|
}
|
||||||
|
if (mx < i + p[i])
|
||||||
|
{
|
||||||
|
id = i;
|
||||||
|
mx = i + p[i];
|
||||||
|
}
|
||||||
|
if (max_len < p[i] - 1)
|
||||||
|
{
|
||||||
|
max_pos = i;
|
||||||
|
max_len = p[i] - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char *ret = malloc(max_len + 1); //返回的字符串
|
||||||
|
memset(ret, 0, max_len);
|
||||||
|
int pos = max_pos / 2 + max_pos % 2 - max_len / 2 - max_len % 2;
|
||||||
|
memcpy(ret, &s[pos], max_len);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
char* longestPalindrome(char* s) {
|
||||||
|
int max = 1, len;
|
||||||
|
int left, right;
|
||||||
|
int maxLeft = 0;
|
||||||
|
int totalLen = strlen(s);
|
||||||
|
int i,j;
|
||||||
|
char *res = (char *) calloc(1, 1001);
|
||||||
|
if(totalLen == 1) {
|
||||||
|
res[0] = s[0];
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
for(i = 0; i < totalLen - 1; /*i++) {
|
||||||
|
if(totalLen-i<=max/2)
|
||||||
|
break;
|
||||||
|
//first, pass consecutive numbers equal to each other
|
||||||
|
right = i;
|
||||||
|
left = i;
|
||||||
|
while((right < totalLen - 1) && (s[right] == s[right + 1])) {
|
||||||
|
right ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//second, set i = r + 1, for there is no len larger when start from i to r
|
||||||
|
i = right + 1;
|
||||||
|
|
||||||
|
//third, count those s[right] == s[left]
|
||||||
|
while((left > 0) && (right < totalLen - 1) && (s[left - 1] == s[right + 1])) {
|
||||||
|
right++;
|
||||||
|
left--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(right - left + 1 > max) {
|
||||||
|
maxLeft = left;
|
||||||
|
max = right - left + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
strncpy(res, s + maxLeft, max);
|
||||||
|
return res;
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
void print_r_c(char *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%c\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%s\n", longestPalindrome("caba"));
|
||||||
|
printf("%s\n", longestPalindrome("dddddddd"));
|
||||||
|
printf("%s\n", longestPalindrome("aca"));
|
||||||
|
printf("%s\n", longestPalindrome("feef"));
|
||||||
|
printf("%s\n", longestPalindrome("abcba"));
|
||||||
|
printf("%s\n", longestPalindrome("daomdukomcayjwgmmetypncdeixarhbectjcwftjjtwjrctixtrsehegwlfotpljeeqhntacfihecdjysgfmxxbjfeskvvfqdoedfxriujnoeteleftsjgdsagqvcgcdjbxgmguunhbjxyajutohgbdwqtjghdftpvidkbftqbpeahxbfyamonazvubzirhqseetaneptnpjbtrtitttxsyjckjvwtrmuwgidkofvobrwrffzrpnxbectsydbvswstfiqranjzhsebjnmprjoirqkgttahrivkcjtitdcpohwwerwgrdivqbltfnigavydxpmitttjjzyrmpaptkczzgnsovebvxezkkovgqegctxacvjfqwtiycnhartzczcgosiquhmdbljjzyrnmffcwnkyzytnsvyzayrieqyrfpxintbbiyrawxlguzaisedwabpzvevlejadztuclcpwvonehkhknicdksdcnjfaoeaqhcdkdtywilwewadcnaprcasccdcnvdgjdqirrsqwzhqqorlhbgpelpupdvuynzybcqkffnnpocidkkigimsa"));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
35
archive/algorithm/leetcode/50.c
Normal file
35
archive/algorithm/leetcode/50.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
double myPow(double x, int n) {
|
||||||
|
double ret = 1;
|
||||||
|
for (int i = n; i != 0; i = i / 2) {
|
||||||
|
if (i % 2 != 0) ret *= x;
|
||||||
|
x *= x;
|
||||||
|
}
|
||||||
|
return n > 0 ? ret : 1 / ret;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
double myPow(double x, int n) {
|
||||||
|
//如果n为零,表明幂数为0,直接返回1
|
||||||
|
if(n == 0)
|
||||||
|
return 1;
|
||||||
|
//如果n为零,表明幂数为0,直接返回x
|
||||||
|
if(n == 1)
|
||||||
|
return x;
|
||||||
|
//p值相当于将幂数折半,减少重复计算
|
||||||
|
int p=n/2;
|
||||||
|
//如果n为负数,那么将x值取其倒数,然后再将p的值乘以-1,这样就相当于
|
||||||
|
//求x倒数的n次幂,你为正数
|
||||||
|
if(n < 0) {
|
||||||
|
//p值改为正数
|
||||||
|
p=-p;
|
||||||
|
//x值取其倒数
|
||||||
|
x = 1 / x;
|
||||||
|
}
|
||||||
|
double res = myPow(x, p);
|
||||||
|
//为了提高时间复杂度,避免重复计算,可以将n不断除二来减少计算量
|
||||||
|
//当n为32时,除二就为16,那么只需要将x^16与自身相乘即可,若n为奇数
|
||||||
|
//那么只需要再乘以x即可
|
||||||
|
if(n % 2 == 0)
|
||||||
|
return res * res;
|
||||||
|
return res * res * x;
|
||||||
|
}
|
||||||
|
*/
|
45
archive/algorithm/leetcode/53.c
Normal file
45
archive/algorithm/leetcode/53.c
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int maxSubArray(int *nums, int numsSize) {
|
||||||
|
if (numsSize <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int *result = malloc(sizeof(int) * numsSize), max;
|
||||||
|
memset(result, 0, sizeof(int) * numsSize);
|
||||||
|
result[0] = nums[0];
|
||||||
|
max = result[0];
|
||||||
|
for (int i = 1; i < numsSize; i++) {
|
||||||
|
result[i] = result[i - 1] + nums[i];
|
||||||
|
if (result[i] < nums[i]) {
|
||||||
|
result[i] = nums[i];
|
||||||
|
}
|
||||||
|
if (max < result[i]) {
|
||||||
|
max = result[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(result);
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
#define MAX(a,b) a > b ? a:b
|
||||||
|
|
||||||
|
int maxSubArray(int* nums, int numsSize) {
|
||||||
|
int* dp = malloc(sizeof(int) * numsSize);
|
||||||
|
int i;
|
||||||
|
int result;
|
||||||
|
dp[0] = nums[0];
|
||||||
|
for(i = 1; i < numsSize; i++)
|
||||||
|
dp[i] = MAX(nums[i], dp[i-1] + nums[i]);
|
||||||
|
result = dp[0];
|
||||||
|
for(i = 1; i < numsSize; i++)
|
||||||
|
if(result < dp[i])
|
||||||
|
result = dp[i];
|
||||||
|
free(dp);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
39
archive/algorithm/leetcode/535.c
Normal file
39
archive/algorithm/leetcode/535.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char* hash[1024];
|
||||||
|
/** Encodes a URL to a shortened URL. */
|
||||||
|
char* encode(char* longUrl) {
|
||||||
|
char* ret = malloc(30);
|
||||||
|
int x = 0;
|
||||||
|
for (int i = 0; i < strlen(longUrl); i++) {
|
||||||
|
x += longUrl[i];
|
||||||
|
}
|
||||||
|
x %= 1024;
|
||||||
|
hash[x] = malloc(1024);
|
||||||
|
strcpy(hash[x], longUrl);
|
||||||
|
// if(strlen(hahs[x])>=3){
|
||||||
|
|
||||||
|
// }
|
||||||
|
sprintf(ret, "http://tinyurl.com/%d", x);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Decodes a shortened URL to its original URL. */
|
||||||
|
char* decode(char* shortUrl) {
|
||||||
|
const char* s = strrchr(shortUrl, '/');
|
||||||
|
int x = atoi(&s[1]);
|
||||||
|
return hash[x];
|
||||||
|
}
|
||||||
|
//直接返回原字符leetcode也是可以通过
|
||||||
|
// Your functions will be called as such:
|
||||||
|
// char* s = encode(s);
|
||||||
|
// decode(s);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char* s = encode("https://leetcode.com/problems/design-tinyurl");
|
||||||
|
s = decode(s);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
31
archive/algorithm/leetcode/611.go
Normal file
31
archive/algorithm/leetcode/611.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func triangleNumber(nums []int) int {
|
||||||
|
sort.Sort(sort.IntSlice(nums))
|
||||||
|
ret := 0
|
||||||
|
for i := range nums {
|
||||||
|
i2 := len(nums) - 2
|
||||||
|
if i2 > i {
|
||||||
|
num := nums[i] + nums[i2]
|
||||||
|
for i3 := len(nums) - 1; i3 > i2; i3-- {
|
||||||
|
if num > nums[i3] {
|
||||||
|
ret += (i3 - i2) + (i2 - i - 1)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(triangleNumber([]int{2, 3, 4, 4})) //4
|
||||||
|
|
||||||
|
fmt.Println(triangleNumber([]int{2, 2, 3, 4})) //3
|
||||||
|
}
|
21
archive/algorithm/leetcode/64.c
Normal file
21
archive/algorithm/leetcode/64.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#define MIN(a, b) (a > b ? b : a)
|
||||||
|
int minPathSum(int** grid, int gridRowSize, int gridColSize) {
|
||||||
|
int** dp = malloc(sizeof(int*) * gridRowSize);
|
||||||
|
for (int i = 0; i < gridRowSize; i++) {
|
||||||
|
dp[i] = malloc(sizeof(int) * gridColSize);
|
||||||
|
}
|
||||||
|
for (int m = 0; m < gridRowSize; m++) {
|
||||||
|
for (int n = 0; n < gridColSize; n++) {
|
||||||
|
if (m == 0 && n == 0) {
|
||||||
|
dp[m][n] = grid[m][n];
|
||||||
|
} else if (m == 0) {
|
||||||
|
dp[m][n] = dp[m][n - 1] + grid[m][n];
|
||||||
|
} else if (n == 0) {
|
||||||
|
dp[m][n] = dp[m - 1][n] + grid[m][n];
|
||||||
|
} else {
|
||||||
|
dp[m][n] = MIN(dp[m - 1][n], dp[m][n - 1]) + grid[m][n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[gridRowSize - 1][gridColSize - 1];
|
||||||
|
}
|
51
archive/algorithm/leetcode/66.c
Normal file
51
archive/algorithm/leetcode/66.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/plus-one/description/
|
||||||
|
* +1(s)
|
||||||
|
* 不断优化,达到了0ms。。。。虽然是道简单题
|
||||||
|
*/
|
||||||
|
int *plusOne(int *digits, int digitsSize, int *returnSize)
|
||||||
|
{
|
||||||
|
int *ret = malloc(sizeof(int) * (digitsSize + 1));
|
||||||
|
*returnSize = digitsSize;
|
||||||
|
int next = 1, pos;
|
||||||
|
for (int i = digitsSize; i > 0; i--)
|
||||||
|
{
|
||||||
|
ret[i] = digits[i - 1] + next;
|
||||||
|
if (ret[i] == 10)
|
||||||
|
{
|
||||||
|
ret[i] = 0;
|
||||||
|
next = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
next = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (next)
|
||||||
|
{
|
||||||
|
ret[0] = 1;
|
||||||
|
(*returnSize)++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = &ret[1];
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a[] = {9, 8}, ret = 0;
|
||||||
|
int *retArray = plusOne(a, 2, &ret);
|
||||||
|
for (int i = 0; i < ret; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", retArray[i]);
|
||||||
|
}
|
||||||
|
printf("%d\n", ret);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
52
archive/algorithm/leetcode/7.c
Normal file
52
archive/algorithm/leetcode/7.c
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/reverse-integer/description/
|
||||||
|
* 反转整数
|
||||||
|
*/
|
||||||
|
int reverse(int x)
|
||||||
|
{
|
||||||
|
long long ret = 0;
|
||||||
|
while (x)
|
||||||
|
{
|
||||||
|
ret = x % 10 + ret * 10;
|
||||||
|
x /= 10;
|
||||||
|
}
|
||||||
|
if (ret <= 2147483647 && ret >= -2147483648)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
最快的dalao解法
|
||||||
|
int reverse(int x) {
|
||||||
|
int y=0;
|
||||||
|
while(x)
|
||||||
|
{
|
||||||
|
int temp=y;
|
||||||
|
y=y*10+x%10;
|
||||||
|
if((y-x%10)/10!=temp)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
x/=10;
|
||||||
|
}
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a = pow(2, 31);
|
||||||
|
printf("%d\n", reverse(1534236469));
|
||||||
|
printf("%d\n", reverse(-321));
|
||||||
|
printf("%d\n", reverse(-2147483648));
|
||||||
|
printf("%d\n", reverse(32006));
|
||||||
|
printf("%d\n", reverse(-1563847412));
|
||||||
|
printf("%d\n", pow(2, 31));
|
||||||
|
printf("%d\n", a);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
22
archive/algorithm/leetcode/70.c
Normal file
22
archive/algorithm/leetcode/70.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int climbStairs(int n)
|
||||||
|
{
|
||||||
|
int *tmp = malloc((n + 1) * sizeof(int));
|
||||||
|
memset(tmp, sizeof(int) * (n + 1), 0);
|
||||||
|
tmp[0] = 1;
|
||||||
|
tmp[1] = 1;
|
||||||
|
for (int i = 2; i <= n; i++)
|
||||||
|
{
|
||||||
|
tmp[n] = tmp[n - 1] + tmp[n - 2];
|
||||||
|
}
|
||||||
|
return tmp[n];
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
109
archive/algorithm/leetcode/73.c
Normal file
109
archive/algorithm/leetcode/73.c
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define bool char
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
void setZeroes(int matrix[7][1], int matrixRowSize, int matrixColSize)
|
||||||
|
{
|
||||||
|
bool h = false, l = false;
|
||||||
|
//扫描首行有没有0
|
||||||
|
for (int i = 0; i < matrixColSize; i++)
|
||||||
|
{
|
||||||
|
if (matrix[0][i] == 0)
|
||||||
|
{
|
||||||
|
h = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//扫描首列有没有0
|
||||||
|
for (int i = 0; i < matrixRowSize; i++)
|
||||||
|
{
|
||||||
|
if (matrix[i][0] == 0)
|
||||||
|
{
|
||||||
|
l = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//扫描0
|
||||||
|
for (int i = 1; i < matrixRowSize; i++)
|
||||||
|
{
|
||||||
|
for (int n = 1; n < matrixColSize; n++)
|
||||||
|
{
|
||||||
|
if (matrix[i][n] == 0)
|
||||||
|
{
|
||||||
|
matrix[0][n] = 0; //首行置0
|
||||||
|
matrix[i][0] = 0; //首列置0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//开始填0,扫描首行
|
||||||
|
for (int i = 0; i < matrixColSize; i++)
|
||||||
|
{
|
||||||
|
if (matrix[0][i] == 0)
|
||||||
|
{
|
||||||
|
for (int n = 1; i > 0 && n < matrixRowSize; n++)
|
||||||
|
{
|
||||||
|
matrix[n][i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//开始填0,扫描首列
|
||||||
|
for (int i = 0; i < matrixRowSize; i++)
|
||||||
|
{
|
||||||
|
if (matrix[i][0] == 0)
|
||||||
|
{
|
||||||
|
for (int n = 1; i > 0 && n < matrixColSize; n++)
|
||||||
|
{
|
||||||
|
matrix[i][n] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//对首行/列判断
|
||||||
|
for (int i = 0; h && i < matrixColSize; i++)
|
||||||
|
{
|
||||||
|
matrix[0][i] = 0;
|
||||||
|
}
|
||||||
|
for (int i = 0; l && i < matrixRowSize; i++)
|
||||||
|
{
|
||||||
|
matrix[i][0] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
void solution(int** matrix, int i, int j, int *row, int*col, int r, int c);//每个元素的操作
|
||||||
|
void setZeroes(int** matrix, int matrixRowSize, int matrixColSize) {
|
||||||
|
int r = matrixRowSize, c = matrixColSize, i, j;
|
||||||
|
int*row = (int*)malloc(sizeof(int)*r), *col = (int*)malloc(sizeof(int)*c);//标记某行、某列是否已经已经有过0,用于减枝所用空间O(m+n),不减枝则所用额外空间为常数级
|
||||||
|
for (i = 0; i < r; i++) {
|
||||||
|
for (j = 0; j < c; j++) {
|
||||||
|
solution(matrix, i, j, row, col, r, c);//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i = 0; i < r; i++)
|
||||||
|
for (j = 0; j < c; j++)
|
||||||
|
matrix[i][j] = matrix[i][j] == -20 ? 0 : matrix[i][j];//更新标记值-20为0
|
||||||
|
}
|
||||||
|
void solution(int** matrix, int i, int j, int *row, int*col, int r, int c) {
|
||||||
|
if (matrix[i][j] == 0) {//满足操作条件的元素
|
||||||
|
if (col[j] != 20) {
|
||||||
|
col[j] = 20;//列标记
|
||||||
|
for (int q = 0; q < r; q++)
|
||||||
|
matrix[q][j] = matrix[q][j] == 0 ? matrix[q][j] : -20;//该列上0保持不变,其余改为标记值-20
|
||||||
|
}
|
||||||
|
if (row[i] != 20) {
|
||||||
|
row[i] = 20;//行标记
|
||||||
|
for (int q = 0; q<c; q++)
|
||||||
|
matrix[i][q] = (matrix[i][q] == -20 || matrix[i][q] == 0) ? matrix[i][q] : -20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int x[7][1] = {{5}, {2}, {0}, {3}, {6}, {7}, {2}};
|
||||||
|
setZeroes(x, 7, 1);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
30
archive/algorithm/leetcode/746.c
Normal file
30
archive/algorithm/leetcode/746.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define MIN(a, b) a > b ? b : a;
|
||||||
|
int minCostClimbingStairs(int* cost, int costSize) {
|
||||||
|
int *dp = malloc(sizeof(int) * costSize), max = 0;
|
||||||
|
memset(dp, 0, sizeof(int) * costSize);
|
||||||
|
for (int i = 2; i < costSize; i++) {
|
||||||
|
dp[i] = MIN(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
|
||||||
|
}
|
||||||
|
max = MIN(dp[costSize - 1] + cost[costSize - 1],
|
||||||
|
dp[costSize - 2] + cost[costSize - 2]);
|
||||||
|
free(dp);
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int minCostClimbingStairs(int* cost, int costSize) {
|
||||||
|
int sum[costSize];
|
||||||
|
sum[costSize-1]=cost[costSize-1];
|
||||||
|
sum[costSize-2]=cost[costSize-2];
|
||||||
|
for(int i=costSize-3;i>=0;i--){
|
||||||
|
sum[i]=(sum[i+1]>sum[i+2]?sum[i+2]:sum[i+1])+cost[i];
|
||||||
|
}
|
||||||
|
return sum[0]>sum[1]?sum[1]:sum[0];
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
37
archive/algorithm/leetcode/784.c
Normal file
37
archive/algorithm/leetcode/784.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void dfs(char** ret, int* num, char* S, int pos) {
|
||||||
|
if (pos > strlen(S)) {
|
||||||
|
ret[*num] = S;
|
||||||
|
*num += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dfs(ret, num, S, pos + 1);
|
||||||
|
if ((S[pos] >= 'a' && S[pos] <= 'z') || (S[pos] >= 'A' && S[pos] <= 'Z')) {
|
||||||
|
//是字母
|
||||||
|
char* b = malloc(strlen(S));
|
||||||
|
strcpy(b, S);
|
||||||
|
b[pos] ^= 32;
|
||||||
|
dfs(ret, num, b, pos + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char** letterCasePermutation(char* S, int* returnSize) {
|
||||||
|
char** ret = malloc(pow(2, strlen(S)) * sizeof(char*));
|
||||||
|
dfs(ret, returnSize, S, 0);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char s[] = "qwertyuiopas";
|
||||||
|
int num = 0;
|
||||||
|
char** ret = letterCasePermutation(s, &num);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
printf("%s\n", ret[i]);
|
||||||
|
}
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
108
archive/algorithm/leetcode/8.c
Normal file
108
archive/algorithm/leetcode/8.c
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/5/strings/37/
|
||||||
|
* 字符串转整数(atoi)
|
||||||
|
*/
|
||||||
|
#define INT_MIN -2147483648
|
||||||
|
#define INT_MAX 0x7fffffff
|
||||||
|
int myAtoi(char *str)
|
||||||
|
{
|
||||||
|
long long ret = 0;
|
||||||
|
char flag = 1, len = 0;
|
||||||
|
while (*str == ' ') //删除空
|
||||||
|
{
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
if (*str == '-')
|
||||||
|
{
|
||||||
|
flag = -1;
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
else if (*str == '+')
|
||||||
|
{
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
else if (*str < '0' || *str > '9')
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
while (*str >= '0' && *str <= '9' && len < 11)
|
||||||
|
{
|
||||||
|
ret = ret * 10 + (*str - '0');
|
||||||
|
str++;
|
||||||
|
if (ret != 0)
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret *= flag;
|
||||||
|
if (ret < INT_MIN)
|
||||||
|
{
|
||||||
|
return INT_MIN;
|
||||||
|
}
|
||||||
|
if (ret > INT_MAX)
|
||||||
|
{
|
||||||
|
return INT_MAX;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int myAtoi(char* str) {
|
||||||
|
if(strlen(str)==0)
|
||||||
|
return 0;
|
||||||
|
char* xianzai= str;
|
||||||
|
long long fanhui = 0;
|
||||||
|
int flag = 1;//记录符号位正负
|
||||||
|
long long max = pow(2,31)-1;
|
||||||
|
long long min = pow(2,31);//溢出的上下界不相同
|
||||||
|
while (*xianzai == ' ')
|
||||||
|
{
|
||||||
|
xianzai++;//跳过空格位置
|
||||||
|
}
|
||||||
|
if (*xianzai == '-')
|
||||||
|
{
|
||||||
|
flag = -1;
|
||||||
|
xianzai++;
|
||||||
|
}
|
||||||
|
else if(*xianzai == '+')
|
||||||
|
{
|
||||||
|
xianzai++;
|
||||||
|
}
|
||||||
|
else if (*xianzai < '0' || *xianzai > '9')
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
while (*xianzai >= '0' && *xianzai <= '9')
|
||||||
|
{
|
||||||
|
fanhui= fanhui*10;
|
||||||
|
fanhui=fanhui+*xianzai-'0';//利用符号本身的ascii码直接转换成数字
|
||||||
|
if(flag==1 && fanhui>max)
|
||||||
|
{
|
||||||
|
//正数溢出,返回INT_MAX
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
else if(flag==-1&&fanhui>min)
|
||||||
|
{
|
||||||
|
return min;//注意int类型上下界数值上相差1
|
||||||
|
}
|
||||||
|
xianzai++;
|
||||||
|
}
|
||||||
|
return flag*fanhui;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d\n", myAtoi("-1"));
|
||||||
|
printf("%d\n", myAtoi("456"));
|
||||||
|
printf("%d\n", myAtoi(" -42"));
|
||||||
|
printf("%d\n", myAtoi("a65r"));
|
||||||
|
printf("%d\n", myAtoi("9223372036854775808"));
|
||||||
|
printf("%d\n", myAtoi("-91283472332"));
|
||||||
|
printf("%d\n", myAtoi("4193 with words"));
|
||||||
|
printf("%ld,%ld\n", -pow(2, 31), pow(2, 31) - 1);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
30
archive/algorithm/leetcode/876.c
Normal file
30
archive/algorithm/leetcode/876.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// 链表的中间结点
|
||||||
|
// https://leetcode-cn.com/problems/middle-of-the-linked-list/
|
||||||
|
|
||||||
|
struct ListNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct ListNode *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ListNode *middleNode(struct ListNode *head)
|
||||||
|
{
|
||||||
|
int len = 0;
|
||||||
|
struct ListNode *p1 = head, *p2 = head->next;
|
||||||
|
while (p2 != NULL)
|
||||||
|
{
|
||||||
|
p1 = p1->next;
|
||||||
|
p2 = p2->next;
|
||||||
|
len++;
|
||||||
|
if (p2 == NULL)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
p2 = p2->next;
|
||||||
|
}
|
||||||
|
return p1;
|
||||||
|
}
|
4
archive/algorithm/leetcode/877.c
Normal file
4
archive/algorithm/leetcode/877.c
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
bool stoneGame(int* piles, int pilesSize) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//只要是我先,我就必胜,我拿最多的不就好了
|
48
archive/algorithm/leetcode/884.c
Normal file
48
archive/algorithm/leetcode/884.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef int bool;
|
||||||
|
|
||||||
|
#define false 0
|
||||||
|
#define true 1
|
||||||
|
|
||||||
|
#define MAX_NUM 500
|
||||||
|
char *backspace(char *s)
|
||||||
|
{
|
||||||
|
char stack[MAX_NUM];
|
||||||
|
int len = 0, pos = 0;
|
||||||
|
while (s[pos] != '\x0')
|
||||||
|
{
|
||||||
|
if (s[pos] == '#')
|
||||||
|
{
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
stack[len--] = '\x0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stack[len++] = s[pos];
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
char *ret = malloc(sizeof(char) * (len + 1));
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
ret[i] = stack[i];
|
||||||
|
}
|
||||||
|
ret[len] = '\x0';
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool backspaceCompare(char *S, char *T)
|
||||||
|
{
|
||||||
|
char *s1 = backspace(S), *s2 = backspace(T);
|
||||||
|
return strcmp(s1, s2) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
bool a = backspaceCompare("y#f#o##f", "y#fo##f");
|
||||||
|
}
|
74
archive/algorithm/leetcode/9.c
Normal file
74
archive/algorithm/leetcode/9.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/palindrome-number/description/
|
||||||
|
* 回文数
|
||||||
|
*/
|
||||||
|
void atoi_(int x, char *s);
|
||||||
|
int isPalindrome(int x)
|
||||||
|
{
|
||||||
|
if (x < 0) //负数直接返回false
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
char s[16]={0};
|
||||||
|
atoi_(x, s);
|
||||||
|
int len = strlen(s), n = len / 2;
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
if (s[i] != s[len - i - 1])
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
void atoi_(int x, char *s)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
while (x > 0)
|
||||||
|
{
|
||||||
|
s[i++] = 48 + x % 10;
|
||||||
|
x /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
bool isPalindrome(int x) {
|
||||||
|
int y=0;
|
||||||
|
int z=x;
|
||||||
|
int p=0;
|
||||||
|
if(z<0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
while(z>0){
|
||||||
|
p=z%10;
|
||||||
|
y=y*10+p;
|
||||||
|
z=z/10;
|
||||||
|
}
|
||||||
|
if(y==x){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("%d", isPalindrome(12321));
|
||||||
|
printf("%d", isPalindrome(-121));
|
||||||
|
printf("%d", isPalindrome(1024));
|
||||||
|
printf("%d", isPalindrome(1));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
17
archive/algorithm/leetcode/902.go
Normal file
17
archive/algorithm/leetcode/902.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func eventualSafeNodes(graph [][]int) []int {
|
||||||
|
ret := make([]int, 0)
|
||||||
|
m := make(map[int]bool)
|
||||||
|
for k, v := range graph {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return []int{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(eventualSafeNodes([][]int{[]int{1, 2}, []int{2, 3}, []int{5}, []int{0}, []int{5}, {}, {}}))
|
||||||
|
}
|
74
archive/algorithm/leetcode/92.c
Normal file
74
archive/algorithm/leetcode/92.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
//https://leetcode-cn.com/problems/reverse-linked-list-ii/
|
||||||
|
//反转链表 II
|
||||||
|
|
||||||
|
struct ListNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct ListNode *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
// b->a->c->d->e
|
||||||
|
// c->b->a->d->e
|
||||||
|
|
||||||
|
struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
|
||||||
|
{
|
||||||
|
int pos = 1;
|
||||||
|
if (head == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (m == n)
|
||||||
|
{
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
struct ListNode *tmp = malloc(sizeof(struct ListNode));
|
||||||
|
tmp->next = head;
|
||||||
|
head = tmp;
|
||||||
|
while (pos <= n)
|
||||||
|
{
|
||||||
|
if (pos >= m)
|
||||||
|
{
|
||||||
|
struct ListNode *head1 = tmp->next, *tail = tmp->next, *next = tmp->next, *tmp2 = NULL;
|
||||||
|
while (pos <= n)
|
||||||
|
{
|
||||||
|
tmp2 = next->next;
|
||||||
|
tail->next = tmp2;
|
||||||
|
next->next = head1;
|
||||||
|
head1 = next;
|
||||||
|
next = tmp2;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
tmp->next = head1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
return head->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct ListNode *head = malloc(sizeof(struct ListNode)), *next = malloc(sizeof(struct ListNode));
|
||||||
|
head->val = 1;
|
||||||
|
head->next = next;
|
||||||
|
next->val = 2;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 3;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 4;
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
|
||||||
|
next->val = 5;
|
||||||
|
next->next = NULL;
|
||||||
|
|
||||||
|
reverseBetween(head, 2, 4);
|
||||||
|
return 0;
|
||||||
|
}
|
14
archive/algorithm/leetcode/96.c
Normal file
14
archive/algorithm/leetcode/96.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
int numTrees(int n) {
|
||||||
|
int *dp = malloc(sizeof(int) * (n + 1));
|
||||||
|
dp[0] = 1;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
for (int n = 1; n <= i; n++) {
|
||||||
|
dp[i] += (dp[n - 1] * dp[i - n]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
// 1 2 5
|
||||||
|
// 2 1
|
||||||
|
// 1 2
|
||||||
|
//
|
111
archive/algorithm/leetcode/add-two-numbers.c
Normal file
111
archive/algorithm/leetcode/add-two-numbers.c
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/add-two-numbers/description/
|
||||||
|
* 两数相加
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct ListNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct ListNode *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
|
||||||
|
{
|
||||||
|
struct ListNode *head = malloc(sizeof(struct ListNode));
|
||||||
|
memset(head, 0, sizeof(struct ListNode));
|
||||||
|
struct ListNode *next = head;
|
||||||
|
int j = 0;
|
||||||
|
if (l1->val == 0 && l1->next == NULL)
|
||||||
|
{
|
||||||
|
l1 = NULL;
|
||||||
|
}
|
||||||
|
if (l2->val == 0 && l2->next == NULL)
|
||||||
|
{
|
||||||
|
l2 = NULL;
|
||||||
|
}
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int num1 = 0, num2 = 0;
|
||||||
|
if (l1 != NULL)
|
||||||
|
{
|
||||||
|
num1 = l1->val;
|
||||||
|
l1 = l1->next;
|
||||||
|
}
|
||||||
|
if (l2 != NULL)
|
||||||
|
{
|
||||||
|
num2 = l2->val;
|
||||||
|
l2 = l2->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
num1 += num2 + j;
|
||||||
|
if (num1 >= 10)
|
||||||
|
{
|
||||||
|
num1 %= 10;
|
||||||
|
j = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
next->val = num1;
|
||||||
|
if (l1 == NULL && l2 == NULL)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
memset(next, 0, sizeof(struct ListNode));
|
||||||
|
}
|
||||||
|
} while (1);
|
||||||
|
if (j == 1)
|
||||||
|
{
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
memset(next, 0, sizeof(struct ListNode));
|
||||||
|
next->val = j;
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ListNode *zh(long long num)
|
||||||
|
{
|
||||||
|
struct ListNode *head = malloc(sizeof(struct ListNode));
|
||||||
|
memset(head, 0, sizeof(struct ListNode));
|
||||||
|
struct ListNode *next = head;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
next->val = num % 10;
|
||||||
|
num = num / 10;
|
||||||
|
if (num > 0)
|
||||||
|
{
|
||||||
|
next->next = malloc(sizeof(struct ListNode));
|
||||||
|
next = next->next;
|
||||||
|
memset(next, 0, sizeof(struct ListNode));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (1);
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
void listNumber(struct ListNode *a)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
printf("%d", a->val);
|
||||||
|
} while ((a = a->next) != NULL);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct ListNode *a = addTwoNumbers(zh(0), zh(1));
|
||||||
|
listNumber(a);
|
||||||
|
return 0;
|
||||||
|
}
|
66
archive/algorithm/leetcode/go/347.go
Normal file
66
archive/algorithm/leetcode/go/347.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
//数字表
|
||||||
|
type NumberTable struct {
|
||||||
|
key int
|
||||||
|
val int
|
||||||
|
}
|
||||||
|
|
||||||
|
//构建最小堆
|
||||||
|
func minHeap(heap []NumberTable, k int) {
|
||||||
|
for i := k/2 - 1; i >= 0; i-- {
|
||||||
|
if heap[i].val > heap[(i+1)*2-1].val {
|
||||||
|
tmp := heap[i]
|
||||||
|
heap[i] = heap[(i+1)*2-1]
|
||||||
|
heap[(i+1)*2-1] = tmp
|
||||||
|
//0123
|
||||||
|
//1234
|
||||||
|
}
|
||||||
|
if k > (i+1)*2 && heap[i].val > heap[(i+1)*2].val {
|
||||||
|
tmp := heap[i]
|
||||||
|
heap[i] = heap[(i+1)*2]
|
||||||
|
heap[(i+1)*2] = tmp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func topKFrequent(nums []int, k int) []int {
|
||||||
|
//hashmap计算各个数字次数
|
||||||
|
var numMap = make(map[int]int)
|
||||||
|
for _, val := range nums {
|
||||||
|
numMap[val]++
|
||||||
|
}
|
||||||
|
//构建优先队列
|
||||||
|
var heap = make([]NumberTable, k)
|
||||||
|
var i = 0
|
||||||
|
for key, val := range numMap {
|
||||||
|
if i >= k {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
heap[i] = NumberTable{key, val}
|
||||||
|
i++
|
||||||
|
numMap[key] = 0
|
||||||
|
}
|
||||||
|
//构建一次最小堆
|
||||||
|
minHeap(heap, k)
|
||||||
|
for key, val := range numMap {
|
||||||
|
if val == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
//后面的继续进入堆,对比第一个位置
|
||||||
|
if heap[0].val < val {
|
||||||
|
heap[0] = NumberTable{key, val}
|
||||||
|
minHeap(heap, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []int
|
||||||
|
for _, val := range heap {
|
||||||
|
ret = append(ret, val.key)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
topKFrequent([]int{5, -3, 9, 1, 7, 7, 9, 10, 2, 2, 10, 10, 3, -1, 3, 7, -9, -1, 3, 3}, 3)
|
||||||
|
}
|
70
archive/algorithm/leetcode/go/692.go
Normal file
70
archive/algorithm/leetcode/go/692.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
//一模一样的题目
|
||||||
|
//347 刷一下
|
||||||
|
|
||||||
|
//数字表
|
||||||
|
type StringTable struct {
|
||||||
|
key string
|
||||||
|
val int
|
||||||
|
}
|
||||||
|
|
||||||
|
//构建最小堆
|
||||||
|
func minHeap(heap []StringTable, k int) {
|
||||||
|
for i := k/2 - 1; i >= 0; i-- {
|
||||||
|
if heap[i].val > heap[(i+1)*2-1].val {
|
||||||
|
tmp := heap[i]
|
||||||
|
heap[i] = heap[(i+1)*2-1]
|
||||||
|
heap[(i+1)*2-1] = tmp
|
||||||
|
//0123
|
||||||
|
//1234
|
||||||
|
}
|
||||||
|
if k > (i+1)*2 && heap[i].val > heap[(i+1)*2].val {
|
||||||
|
tmp := heap[i]
|
||||||
|
heap[i] = heap[(i+1)*2]
|
||||||
|
heap[(i+1)*2] = tmp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func topKFrequent(words []string, k int) []string {
|
||||||
|
//hashmap计算各个数字次数
|
||||||
|
var numMap = make(map[string]int)
|
||||||
|
for _, val := range words {
|
||||||
|
numMap[val]++
|
||||||
|
}
|
||||||
|
//构建优先队列
|
||||||
|
var heap = make([]StringTable, k)
|
||||||
|
var i = 0
|
||||||
|
for key, val := range numMap {
|
||||||
|
if i >= k {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
heap[i] = StringTable{key, val}
|
||||||
|
i++
|
||||||
|
numMap[key] = 0
|
||||||
|
}
|
||||||
|
//构建一次最小堆
|
||||||
|
minHeap(heap, k)
|
||||||
|
for key, val := range numMap {
|
||||||
|
if val == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
//后面的继续进入堆,对比第一个位置
|
||||||
|
if heap[0].val < val {
|
||||||
|
heap[0] = StringTable{key, val}
|
||||||
|
minHeap(heap, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//比那个多了一个排序,来次快排
|
||||||
|
//TODO:快排
|
||||||
|
var ret []string
|
||||||
|
for i = k - 1; i >= 0; i-- {
|
||||||
|
ret = append(ret, heap[i].key)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
topKFrequent([]string{"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"}, 4)
|
||||||
|
}
|
82
archive/algorithm/leetcode/ms.17.14.c
Normal file
82
archive/algorithm/leetcode/ms.17.14.c
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
//面试题 17.14. 最小K个数
|
||||||
|
//https://leetcode-cn.com/problems/smallest-k-lcci/
|
||||||
|
|
||||||
|
void swap(int *array, int pos, int pos2)
|
||||||
|
{
|
||||||
|
int tmp = array[pos];
|
||||||
|
array[pos] = array[pos2];
|
||||||
|
array[pos2] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int *topk(int *arr, int start, int end, int k, int *returnSize)
|
||||||
|
{
|
||||||
|
int q = start, p = end + 1, mid = (end - start) / 2 + start, flag = 0;
|
||||||
|
if (arr[start] > arr[end])
|
||||||
|
{
|
||||||
|
swap(arr, start, end);
|
||||||
|
}
|
||||||
|
if (arr[mid] > arr[end])
|
||||||
|
{
|
||||||
|
swap(arr, mid, end);
|
||||||
|
}
|
||||||
|
if (arr[start] > arr[mid])
|
||||||
|
{
|
||||||
|
swap(arr, mid, end);
|
||||||
|
}
|
||||||
|
flag = arr[mid];
|
||||||
|
swap(arr, start, mid);
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
while (arr[++q] < flag)
|
||||||
|
;
|
||||||
|
while (arr[--p] > flag)
|
||||||
|
;
|
||||||
|
if (q > p)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
swap(arr, q, p);
|
||||||
|
}
|
||||||
|
swap(arr, start, p);
|
||||||
|
if (p > k)
|
||||||
|
{
|
||||||
|
//大于k,搜索左边
|
||||||
|
topk(arr, start, p - 1, k, returnSize);
|
||||||
|
}
|
||||||
|
else if (p < k)
|
||||||
|
{
|
||||||
|
//小于k,搜索右边
|
||||||
|
topk(arr, p + 1, end, k, returnSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//等于k,返回
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Note: The returned array must be malloced, assume caller calls free().
|
||||||
|
*/
|
||||||
|
int *smallestK(int *arr, int arrSize, int k, int *returnSize)
|
||||||
|
{
|
||||||
|
if (k == arrSize)
|
||||||
|
{
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
*returnSize = k;
|
||||||
|
topk(arr, 0, arrSize - 1, k, returnSize);
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int arr[] = {4, -5051, 6, 10156, 1, -39670, 2, -686, -19101, 42719, 8216, -56, -30716, -6, 0, -14, 97, -37023, 0, -28, 5094, 884, -508, 36504, 47475, 8, 37603, 2, -5021, -65, 0, 3229, 6068, 7, 5534, -577, -184, 92, 4, -797, -126, 460};
|
||||||
|
int ret = 0;
|
||||||
|
smallestK(arr, sizeof(arr) / sizeof(int), 34, &ret);
|
||||||
|
}
|
23
archive/algorithm/leetcode/template.c
Normal file
23
archive/algorithm/leetcode/template.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define bool int
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
34
archive/algorithm/leetcode/two-sum.c
Normal file
34
archive/algorithm/leetcode/two-sum.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://leetcode-cn.com/problems/two-sum/description/
|
||||||
|
* 两数之和
|
||||||
|
*/
|
||||||
|
int *twoSum(int *nums, int numsSize, int target)
|
||||||
|
{
|
||||||
|
int *s = malloc(sizeof(int) * 2);
|
||||||
|
memset(s, 0, sizeof(int) * 2);
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
{
|
||||||
|
int tmp = target - nums[i];
|
||||||
|
for (int n = i + 1; n < numsSize; n++)
|
||||||
|
{
|
||||||
|
if (nums[n] == tmp)
|
||||||
|
{
|
||||||
|
s[0] = i;
|
||||||
|
s[1] = n;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int nums[] = {2, 7, 11, 15};
|
||||||
|
int *s = twoSum(nums, sizeof(nums) / 4, 9);
|
||||||
|
printf("%d,%d\n", s[0], s[1]);
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
144
archive/algorithm/leetcode/vhw.c
Normal file
144
archive/algorithm/leetcode/vhw.c
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
/**
|
||||||
|
* 验证回文字符串
|
||||||
|
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/5/strings/36/
|
||||||
|
*/
|
||||||
|
#define bool int
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
bool isPalindrome(char *s)
|
||||||
|
{
|
||||||
|
int len = strlen(s);
|
||||||
|
if (len < 1)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
for (int start = 0, end = len - 1; start < end; start++, end--)
|
||||||
|
{
|
||||||
|
while (start < end) //遇到我们要判断数字字母的停止
|
||||||
|
{
|
||||||
|
if ((s[start] >= 48 && s[start] <= 57) ||
|
||||||
|
(s[start] >= 97 && s[start] <= 122))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if (s[start] >= 65 && s[start] <= 90)
|
||||||
|
{
|
||||||
|
s[start] |= 32; //转换为小写
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
start++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
while (start < end) //结束的同上
|
||||||
|
{
|
||||||
|
if ((s[end] >= 48 && s[end] <= 57) ||
|
||||||
|
(s[end] >= 97 && s[end] <= 122))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if (s[end] >= 65 && s[end] <= 90)
|
||||||
|
{
|
||||||
|
s[end] |= 32; //转换为小写
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
end--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (start >= end)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (s[start] != s[end])
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int is_char(char c)
|
||||||
|
{
|
||||||
|
return ( (c>='a' && c<='z') || (c>='A' && c<='Z') );
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_num(char c)
|
||||||
|
{
|
||||||
|
return (c>='0'&&c<='9');
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_equal(char c1,char c2){
|
||||||
|
if(is_num(c1))
|
||||||
|
return c1 == c2;
|
||||||
|
else//c1 is charactrer
|
||||||
|
{
|
||||||
|
if(is_num(c2))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
c1 = c1 > 96 ? c1-32:c1;
|
||||||
|
c2 = c2 > 96 ? c2-32:c2;
|
||||||
|
|
||||||
|
return c1 == c2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int isPalindrome(char* s) {
|
||||||
|
int len;
|
||||||
|
if(s == NULL || (len = strlen(s)) == 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
char *p = s;
|
||||||
|
char *q = s + len-1;
|
||||||
|
|
||||||
|
while(p < q)
|
||||||
|
{
|
||||||
|
if((!is_num(*p)) && (!is_char(*p)))
|
||||||
|
{
|
||||||
|
p = p + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((!is_num(*q)) && (!is_char(*q)))
|
||||||
|
{
|
||||||
|
q = q - 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(is_equal(*p,*q) == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
p = p + 1;
|
||||||
|
q = q - 1;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
void print_r(int *a, int len)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
printf("%d\t", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char a[] = "A man, a plan, a canal: Panama";
|
||||||
|
printf("%d\n", isPalindrome("qwewq"));
|
||||||
|
printf("%d\n", isPalindrome(a));
|
||||||
|
printf("%d\n", isPalindrome("race a car"));
|
||||||
|
char b[] = "A man\r\na plan\r\na canal: Panama";
|
||||||
|
printf("%d\n", isPalindrome(b));
|
||||||
|
printf("%d\n", isPalindrome(".,"));
|
||||||
|
getchar();
|
||||||
|
return 0;
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user