Skip to content

Commit 0038609

Browse files
Update
1 parent d6caa50 commit 0038609

File tree

6 files changed

+352
-0
lines changed

6 files changed

+352
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* ************************************************************************
2+
> File Name: algorithm_2.cpp
3+
> Author: sunxiuyang
4+
5+
> Created Time: Thu Jan 30 16:09:35 2020
6+
> Description:
7+
************************************************************************/
8+
9+
#include <iostream>
10+
#include <chrono>
11+
#include <thread>
12+
using namespace std;
13+
using namespace chrono;
14+
15+
// O(n)
16+
void function1(long long n) {
17+
long long k = 0;
18+
for (long long i = 0; i < n; i++) {
19+
k++;
20+
}
21+
}
22+
23+
// O(n^2)
24+
void function2(long long n) {
25+
long long k = 0;
26+
for (long long i = 0; i < n; i++) {
27+
for (long j = 0; j < n; j++) {
28+
k++;
29+
}
30+
}
31+
32+
}
33+
34+
// O(nlogn)
35+
void function3(long long n) {
36+
long long k = 0;
37+
for (long long i = 0; i < n; i++) {
38+
for (long long j = 1; j < n; j = j*2) { // 注意这里j=1
39+
k++;
40+
}
41+
}
42+
}
43+
44+
45+
void time_test() {
46+
long long n; // 数据规模
47+
while (cin>>n) {
48+
milliseconds start_time = duration_cast<milliseconds >(
49+
system_clock::now().time_since_epoch()
50+
);
51+
52+
function3(n);
53+
54+
milliseconds end_time = duration_cast<milliseconds >(
55+
system_clock::now().time_since_epoch()
56+
);
57+
cout << milliseconds(end_time).count() - milliseconds(start_time).count()
58+
<<" ms"<< endl;
59+
}
60+
}
61+
62+
int main() {
63+
time_test();
64+
}
65+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* ************************************************************************
2+
> File Name: section_3.cpp
3+
> Author: sunxiuyang
4+
5+
> Created Time: Sat Feb 8 22:00:32 2020
6+
> Description:
7+
************************************************************************/
8+
9+
#include<iostream>
10+
using namespace std;
11+
12+
int function1(int x, int n) {
13+
int result = 1; // 注意 任何数的0次方等于1
14+
for (int i = 0; i < n; i++) {
15+
result = result * x;
16+
}
17+
return result;
18+
}
19+
20+
int function2(int x, int n) {
21+
if (n == 0) {
22+
return 1; // return 1 同样是因为0次方是等于1的
23+
}
24+
return function2(x, n - 1) * x;
25+
}
26+
27+
int function3(int x, int n) {
28+
if (n == 0) {
29+
return 1;
30+
}
31+
// if (n == 1) { // 这里如果不作处理就会陷入死循环
32+
// return x;
33+
// }
34+
if (n % 2 == 1) {
35+
return function3(x, n/2) * function3(x, n/2) * x;
36+
}
37+
return function3(x, n/2) * function3(x, n/2);
38+
}
39+
40+
int function4(int x, int n) {
41+
if (n == 0) {
42+
return 1;
43+
}
44+
int t = function4(x, n/2);
45+
if (n % 2 == 1) {
46+
return t*t*x;
47+
}
48+
return t*t;
49+
}
50+
int main() {
51+
int x, n;
52+
while (cin >> x >> n) {
53+
cout << function3(x, n) << endl;
54+
}
55+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* ************************************************************************
2+
> File Name: section_4.cpp
3+
> Author: sunxiuyang
4+
5+
> Created Time: Tue Feb 18 23:34:08 2020
6+
> Description:
7+
************************************************************************/
8+
9+
#include<iostream>
10+
using namespace std;
11+
12+
struct node{
13+
int num;
14+
char cha;
15+
}st;
16+
17+
int main() {
18+
int a[100];
19+
char b[100];
20+
cout << sizeof(int) << endl;
21+
cout << sizeof(char) << endl;
22+
cout << sizeof(a) << endl;
23+
cout << sizeof(b) << endl;
24+
cout << sizeof(st) << endl;
25+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* ************************************************************************
2+
> File Name: section_5.cpp
3+
> Author: sunxiuyang
4+
5+
> Created Time: Mon Feb 24 18:44:56 2020
6+
> Description:
7+
************************************************************************/
8+
9+
#include <iostream>
10+
#include <chrono>
11+
#include <thread>
12+
using namespace std;
13+
using namespace chrono;
14+
15+
int fibonacci_1(int n) {
16+
if (n <= 0) {
17+
return 0;
18+
}
19+
if (n == 1 || n == 2) {
20+
return 1;
21+
}
22+
int first = 1, second = 1;
23+
int sum = first + second;
24+
for (int i = 2; i < n; i++) {
25+
sum = first + second;
26+
first = second;
27+
second = sum;
28+
}
29+
return sum;
30+
}
31+
32+
33+
int fibonacci_2(int i) {
34+
if(i <= 0) return 0;
35+
if(i == 1) return 1;
36+
return fibonacci_2(i - 1) + fibonacci_2(i - 2);
37+
}
38+
39+
int fibonacci_3(int first, int second, int n) {
40+
if (n <= 0) {
41+
return 0;
42+
}
43+
if (n < 3) {
44+
return 1;
45+
}
46+
else if (n == 3) {
47+
return first + second;
48+
}
49+
else {
50+
return fibonacci_3(second, first + second, n - 1);
51+
}
52+
}
53+
54+
55+
void test_fibonacci() {
56+
int n;
57+
while(cin >> n) {
58+
cout << fibonacci_1(n) << endl;
59+
cout << fibonacci_2(n) << endl;
60+
cout << fibonacci_3(1, 1, n) << endl;
61+
}
62+
}
63+
64+
int binary_search(int arr[], int l, int r, int n) {
65+
if (r >= l) {
66+
int mid = l + (r - l) / 2;
67+
if (arr[mid] == n)
68+
return mid;
69+
if (arr[mid] > n)
70+
return binary_search(arr, l, mid - 1, n);
71+
return binary_search(arr, mid + 1, r, n);
72+
}
73+
return -1;
74+
}
75+
int arr[] = {2, 3, 4, 5, 8, 10, 15, 17, 20};
76+
int binary_search(int l, int r, int n) {
77+
if (r >= l) {
78+
int mid = l + (r - l) / 2;
79+
if (arr[mid] == n)
80+
return mid;
81+
if (arr[mid] > n)
82+
return binary_search(l, mid - 1, n);
83+
return binary_search(mid + 1, r, n);
84+
}
85+
return -1;
86+
}
87+
88+
void test_binary_search(void) {
89+
int arr[] = {2, 3, 4, 5, 8, 10, 15, 17, 20};
90+
int x = 17;
91+
int n = sizeof(arr) / sizeof(arr[0]);
92+
// int result = binary_search(arr, 0, n - 1, x);
93+
int result = binary_search(0, n - 1, x);
94+
(result == -1) ? cout << "Element is not present in array"
95+
: cout << "Element is present at index " << result;
96+
}
97+
98+
int main()
99+
{
100+
// test_binary_search();
101+
// test_fibonacci();
102+
return 0;
103+
}
104+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#include <iostream>
3+
#include <chrono>
4+
#include <thread>
5+
using namespace std;
6+
using namespace chrono;
7+
int fibonacci_2(int i) {
8+
if(i <= 0) return 0;
9+
if(i == 1) return 1;
10+
return fibonacci_2(i - 1) + fibonacci_2(i - 2);
11+
}
12+
void time_consumption() {
13+
int n;
14+
while (cin >> n) {
15+
milliseconds start_time = duration_cast<milliseconds >(
16+
system_clock::now().time_since_epoch()
17+
);
18+
19+
fibonacci_2(n);
20+
21+
milliseconds end_time = duration_cast<milliseconds >(
22+
system_clock::now().time_since_epoch()
23+
);
24+
cout << milliseconds(end_time).count() - milliseconds(start_time).count()
25+
<<" ms"<< endl;
26+
}
27+
}
28+
int main()
29+
{
30+
time_consumption();
31+
return 0;
32+
}
33+

problems/0000.TwoSum.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/two-sum/
3+
4+
## 思路
5+
6+
## 一般解法
7+
8+
代码:
9+
10+
```C++
11+
```
12+
13+
## 优化解法
14+
15+
```C++
16+
class Solution {
17+
public:
18+
vector<int> twoSum(vector<int>& nums, int target) {
19+
for (int i = 0; i < nums.size(); i ++) {
20+
for (int j = i + 1; j < nums.size(); j++) {
21+
if (nums[i] + nums[j] == target) {
22+
return {i, j};
23+
}
24+
}
25+
}
26+
return {};
27+
}
28+
};
29+
30+
```
31+
32+
```
33+
class Solution {
34+
public:
35+
vector<int> twoSum(vector<int>& nums, int target) {
36+
std::map <int,int> map;
37+
for(int i = 0; i < nums.size(); i++) {
38+
auto iter = map.find(target - nums[i]);
39+
if(iter != map.end()) {
40+
return {iter->second,i};
41+
}
42+
map[nums[i]] = i;
43+
}
44+
return {};
45+
}
46+
};
47+
```
48+
49+
```
50+
class Solution {
51+
public:
52+
vector<int> twoSum(vector<int>& nums, int target) {
53+
std::unordered_map <int,int> map;
54+
for(int i = 0; i < nums.size(); i++) {
55+
auto iter = map.find(target - nums[i]);
56+
if(iter != map.end()) {
57+
return {iter->second, i};
58+
break;
59+
}
60+
map.emplace(nums[i], i);
61+
}
62+
return {};
63+
}
64+
};
65+
```
66+
## 代码
67+
68+
```C++
69+
70+
```

0 commit comments

Comments
 (0)