mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Update
This commit is contained in:
14
README.md
14
README.md
@ -1,19 +1,21 @@
|
||||
# leetcode
|
||||
LeetCode 最强题解
|
||||
> 笔者介绍
|
||||
> ACM亚洲区域赛铜牌获得者,哈工大计算机硕士毕业后,先后在腾讯和百度工作多年,对算法和后端技术有一定的见解,利用工作之余重新刷leetcode
|
||||
> 欢迎关注微信公众号:「代码随想录」,这里将持续分享自己对互联网以及技术的想法与思考
|
||||
|
||||
LeetCode 最强题解(持续更新中):
|
||||
|
||||
|题目 | 类型 | 解题方法 |
|
||||
|---|---| ---|
|
||||
|[0000.两数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0000.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.md) | 数组|**暴力** **哈希**|
|
||||
|[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md) |链表 |**模拟** |
|
||||
|[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md) |数组 |**暴力** **快慢指针** |
|
||||
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 | **暴力**、**快慢指针**|
|
||||
|[0035.搜索插入位置](https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md) |数组 | **暴力**、**二分**|
|
||||
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 | **暴力** **快慢指针**|
|
||||
|[0035.搜索插入位置](https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md) |数组 | **暴力** **二分**|
|
||||
|[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |**暴力** **贪心** 动态规划 分治|
|
||||
|[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md) |数组 |**模拟**|
|
||||
|[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md) |数组 | **暴力** **滑动窗口**|
|
||||
|[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 | **暴力**|
|
||||
|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |**暴力** **字典计数**|
|
||||
|
||||
> 笔者介绍
|
||||
> ACM亚洲区域赛铜牌获得者,哈工大计算机硕士毕业后,先后在腾讯和百度工作多年,对算法和后端技术有一定的见解
|
||||
> 欢迎关注公众号:「代码随想录」这里将持续分享自己对互联网以及技术的想法与思考
|
||||
|
||||
|
@ -1,65 +0,0 @@
|
||||
/* ************************************************************************
|
||||
> File Name: algorithm_2.cpp
|
||||
> Author: sunxiuyang
|
||||
> Mail: sunxiuyang04@gmail.com
|
||||
> Created Time: Thu Jan 30 16:09:35 2020
|
||||
> Description:
|
||||
************************************************************************/
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
using namespace std;
|
||||
using namespace chrono;
|
||||
|
||||
// O(n)
|
||||
void function1(long long n) {
|
||||
long long k = 0;
|
||||
for (long long i = 0; i < n; i++) {
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
// O(n^2)
|
||||
void function2(long long n) {
|
||||
long long k = 0;
|
||||
for (long long i = 0; i < n; i++) {
|
||||
for (long j = 0; j < n; j++) {
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// O(nlogn)
|
||||
void function3(long long n) {
|
||||
long long k = 0;
|
||||
for (long long i = 0; i < n; i++) {
|
||||
for (long long j = 1; j < n; j = j*2) { // 注意这里j=1
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void time_test() {
|
||||
long long n; // 数据规模
|
||||
while (cin>>n) {
|
||||
milliseconds start_time = duration_cast<milliseconds >(
|
||||
system_clock::now().time_since_epoch()
|
||||
);
|
||||
|
||||
function3(n);
|
||||
|
||||
milliseconds end_time = duration_cast<milliseconds >(
|
||||
system_clock::now().time_since_epoch()
|
||||
);
|
||||
cout << milliseconds(end_time).count() - milliseconds(start_time).count()
|
||||
<<" ms"<< endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
time_test();
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
/* ************************************************************************
|
||||
> File Name: section_3.cpp
|
||||
> Author: sunxiuyang
|
||||
> Mail: sunxiuyang04@gmail.com
|
||||
> Created Time: Sat Feb 8 22:00:32 2020
|
||||
> Description:
|
||||
************************************************************************/
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int function1(int x, int n) {
|
||||
int result = 1; // 注意 任何数的0次方等于1
|
||||
for (int i = 0; i < n; i++) {
|
||||
result = result * x;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int function2(int x, int n) {
|
||||
if (n == 0) {
|
||||
return 1; // return 1 同样是因为0次方是等于1的
|
||||
}
|
||||
return function2(x, n - 1) * x;
|
||||
}
|
||||
|
||||
int function3(int x, int n) {
|
||||
if (n == 0) {
|
||||
return 1;
|
||||
}
|
||||
// if (n == 1) { // 这里如果不作处理就会陷入死循环
|
||||
// return x;
|
||||
// }
|
||||
if (n % 2 == 1) {
|
||||
return function3(x, n/2) * function3(x, n/2) * x;
|
||||
}
|
||||
return function3(x, n/2) * function3(x, n/2);
|
||||
}
|
||||
|
||||
int function4(int x, int n) {
|
||||
if (n == 0) {
|
||||
return 1;
|
||||
}
|
||||
int t = function4(x, n/2);
|
||||
if (n % 2 == 1) {
|
||||
return t*t*x;
|
||||
}
|
||||
return t*t;
|
||||
}
|
||||
int main() {
|
||||
int x, n;
|
||||
while (cin >> x >> n) {
|
||||
cout << function3(x, n) << endl;
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
/* ************************************************************************
|
||||
> File Name: section_4.cpp
|
||||
> Author: sunxiuyang
|
||||
> Mail: sunxiuyang04@gmail.com
|
||||
> Created Time: Tue Feb 18 23:34:08 2020
|
||||
> Description:
|
||||
************************************************************************/
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node{
|
||||
int num;
|
||||
char cha;
|
||||
}st;
|
||||
|
||||
int main() {
|
||||
int a[100];
|
||||
char b[100];
|
||||
cout << sizeof(int) << endl;
|
||||
cout << sizeof(char) << endl;
|
||||
cout << sizeof(a) << endl;
|
||||
cout << sizeof(b) << endl;
|
||||
cout << sizeof(st) << endl;
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
/* ************************************************************************
|
||||
> File Name: section_5.cpp
|
||||
> Author: sunxiuyang
|
||||
> Mail: sunxiuyang04@gmail.com
|
||||
> Created Time: Mon Feb 24 18:44:56 2020
|
||||
> Description:
|
||||
************************************************************************/
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
using namespace std;
|
||||
using namespace chrono;
|
||||
|
||||
int fibonacci_1(int n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
}
|
||||
int first = 1, second = 1;
|
||||
int sum = first + second;
|
||||
for (int i = 2; i < n; i++) {
|
||||
sum = first + second;
|
||||
first = second;
|
||||
second = sum;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
int fibonacci_2(int i) {
|
||||
if(i <= 0) return 0;
|
||||
if(i == 1) return 1;
|
||||
return fibonacci_2(i - 1) + fibonacci_2(i - 2);
|
||||
}
|
||||
|
||||
int fibonacci_3(int first, int second, int n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (n < 3) {
|
||||
return 1;
|
||||
}
|
||||
else if (n == 3) {
|
||||
return first + second;
|
||||
}
|
||||
else {
|
||||
return fibonacci_3(second, first + second, n - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void test_fibonacci() {
|
||||
int n;
|
||||
while(cin >> n) {
|
||||
cout << fibonacci_1(n) << endl;
|
||||
cout << fibonacci_2(n) << endl;
|
||||
cout << fibonacci_3(1, 1, n) << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int binary_search(int arr[], int l, int r, int n) {
|
||||
if (r >= l) {
|
||||
int mid = l + (r - l) / 2;
|
||||
if (arr[mid] == n)
|
||||
return mid;
|
||||
if (arr[mid] > n)
|
||||
return binary_search(arr, l, mid - 1, n);
|
||||
return binary_search(arr, mid + 1, r, n);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int arr[] = {2, 3, 4, 5, 8, 10, 15, 17, 20};
|
||||
int binary_search(int l, int r, int n) {
|
||||
if (r >= l) {
|
||||
int mid = l + (r - l) / 2;
|
||||
if (arr[mid] == n)
|
||||
return mid;
|
||||
if (arr[mid] > n)
|
||||
return binary_search(l, mid - 1, n);
|
||||
return binary_search(mid + 1, r, n);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void test_binary_search(void) {
|
||||
int arr[] = {2, 3, 4, 5, 8, 10, 15, 17, 20};
|
||||
int x = 17;
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
// int result = binary_search(arr, 0, n - 1, x);
|
||||
int result = binary_search(0, n - 1, x);
|
||||
(result == -1) ? cout << "Element is not present in array"
|
||||
: cout << "Element is present at index " << result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// test_binary_search();
|
||||
// test_fibonacci();
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,33 +0,0 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
using namespace std;
|
||||
using namespace chrono;
|
||||
int fibonacci_2(int i) {
|
||||
if(i <= 0) return 0;
|
||||
if(i == 1) return 1;
|
||||
return fibonacci_2(i - 1) + fibonacci_2(i - 2);
|
||||
}
|
||||
void time_consumption() {
|
||||
int n;
|
||||
while (cin >> n) {
|
||||
milliseconds start_time = duration_cast<milliseconds >(
|
||||
system_clock::now().time_since_epoch()
|
||||
);
|
||||
|
||||
fibonacci_2(n);
|
||||
|
||||
milliseconds end_time = duration_cast<milliseconds >(
|
||||
system_clock::now().time_since_epoch()
|
||||
);
|
||||
cout << milliseconds(end_time).count() - milliseconds(start_time).count()
|
||||
<<" ms"<< endl;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
time_consumption();
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user