This commit is contained in:
youngyangyang04
2020-03-01 23:45:22 +08:00
parent d6caa500a4
commit 0038609239
6 changed files with 352 additions and 0 deletions

View File

@ -0,0 +1,65 @@
/* ************************************************************************
> 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();
}

View File

@ -0,0 +1,55 @@
/* ************************************************************************
> 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;
}
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************
> 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;
}

View File

@ -0,0 +1,104 @@
/* ************************************************************************
> 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;
}

View File

@ -0,0 +1,33 @@
#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;
}

70
problems/0000.TwoSum.md Normal file
View File

@ -0,0 +1,70 @@
## 题目地址
https://leetcode-cn.com/problems/two-sum/
## 思路
## 一般解法
代码:
```C++
```
## 优化解法
```C++
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i = 0; i < nums.size(); i ++) {
for (int j = i + 1; j < nums.size(); j++) {
if (nums[i] + nums[j] == target) {
return {i, j};
}
}
}
return {};
}
};
```
```
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::map <int,int> map;
for(int i = 0; i < nums.size(); i++) {
auto iter = map.find(target - nums[i]);
if(iter != map.end()) {
return {iter->second,i};
}
map[nums[i]] = i;
}
return {};
}
};
```
```
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::unordered_map <int,int> map;
for(int i = 0; i < nums.size(); i++) {
auto iter = map.find(target - nums[i]);
if(iter != map.end()) {
return {iter->second, i};
break;
}
map.emplace(nums[i], i);
}
return {};
}
};
```
## 代码
```C++
```