Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5855b5d

Browse files
committed
feat: add solutions to lc problems: No.0509,1137
1 parent ac8f475 commit 5855b5d

File tree

14 files changed

+353
-40
lines changed

14 files changed

+353
-40
lines changed

‎solution/0500-0599/0509.Fibonacci Number/README.md‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,79 @@ F(n) = F(n - 1) + F(n - 2),其中 n > 1
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```python
64-
64+
class Solution:
65+
def fib(self, n: int) -> int:
66+
a, b = 0, 1
67+
for _ in range(n):
68+
a, b = b, a + b
69+
return a
6570
```
6671

6772
### **Java**
6873

6974
<!-- 这里可写当前语言的特殊实现逻辑 -->
7075

7176
```java
77+
class Solution {
78+
public int fib(int n) {
79+
int a = 0, b = 1;
80+
while (n-- > 0) {
81+
int c = a + b;
82+
a = b;
83+
b = c;
84+
}
85+
return a;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int fib(int n) {
96+
int a = 0, b = 1;
97+
while (n--) {
98+
int c = a + b;
99+
a = b;
100+
b = c;
101+
}
102+
return a;
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func fib(n int) int {
111+
a, b := 0, 1
112+
for i := 0; i < n; i++ {
113+
a, b = b, a+b
114+
}
115+
return a
116+
}
117+
```
72118

119+
### **JavaScript**
120+
121+
122+
```js
123+
/**
124+
* @param {number} n
125+
* @return {number}
126+
*/
127+
var fib = function(n) {
128+
let a = 0;
129+
let b = 1;
130+
while (n--) {
131+
const c = a + b;
132+
a = b;
133+
b = c;
134+
}
135+
return a;
136+
};
73137
```
74138

75139
### **...**

‎solution/0500-0599/0509.Fibonacci Number/README_EN.md‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,77 @@ F(n) = F(n - 1) + F(n - 2), for n &gt; 1.
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def fib(self, n: int) -> int:
58+
a, b = 0, 1
59+
for _ in range(n):
60+
a, b = b, a + b
61+
return a
5762
```
5863

5964
### **Java**
6065

6166
```java
67+
class Solution {
68+
public int fib(int n) {
69+
int a = 0, b = 1;
70+
while (n-- > 0) {
71+
int c = a + b;
72+
a = b;
73+
b = c;
74+
}
75+
return a;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
int fib(int n) {
86+
int a = 0, b = 1;
87+
while (n--) {
88+
int c = a + b;
89+
a = b;
90+
b = c;
91+
}
92+
return a;
93+
}
94+
};
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
func fib(n int) int {
101+
a, b := 0, 1
102+
for i := 0; i < n; i++ {
103+
a, b = b, a+b
104+
}
105+
return a
106+
}
107+
```
62108

109+
### **JavaScript**
110+
111+
112+
```js
113+
/**
114+
* @param {number} n
115+
* @return {number}
116+
*/
117+
var fib = function(n) {
118+
let a = 0;
119+
let b = 1;
120+
while (n--) {
121+
const c = a + b;
122+
a = b;
123+
b = c;
124+
}
125+
return a;
126+
};
63127
```
64128

65129
### **...**
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
class Solution {
22
public:
3-
int fib(int N) {
4-
int a[2] = {0, 1} ;
5-
for (int i = 2; i <= N; ++i)
6-
a[i&1] += a[i&1^1] ;
7-
return a[N&1] ;
3+
int fib(int n) {
4+
int a = 0, b = 1;
5+
while (n--) {
6+
int c = a + b;
7+
a = b;
8+
b = c;
9+
}
10+
return a;
811
}
912
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func fib(n int) int {
2+
a, b := 0, 1
3+
for i := 0; i < n; i++ {
4+
a, b = b, a+b
5+
}
6+
return a
7+
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
class Solution {
2-
public int fib(int N) {
3-
if (N < 2) {
4-
return N;
5-
}
2+
public int fib(int n) {
63
int a = 0, b = 1;
7-
int res = 0;
8-
for (int i = 2; i <= N; ++i) {
9-
res = a + b;
4+
while (n-- > 0) {
5+
int c = a + b;
106
a = b;
11-
b = res;
7+
b = c;
128
}
13-
return res;
9+
return a;
1410
}
15-
}
11+
}
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
/**
2-
* @param {number} N
2+
* @param {number} n
33
* @return {number}
44
*/
5-
6-
/**
7-
* Author: Mcnwork2018
8-
*/
9-
10-
let preResult = {};
11-
var fib = function (N) {
12-
if (N === 0) return 0;
13-
if (N === 1) return 1;
14-
if (preResult[N]) {
15-
return preResult[N];
16-
} else {
17-
preResult[N] = fib(N - 1) + fib(N - 2);
5+
var fib = function(n) {
6+
let a = 0;
7+
let b = 1;
8+
while (n--) {
9+
const c = a + b;
10+
a = b;
11+
b = c;
1812
}
19-
return fib(N-1)+fib(N-2);
20-
};
13+
return a;
14+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def fib(self, n: int) -> int:
3+
a, b = 0, 1
4+
for _ in range(n):
5+
a, b = b, a + b
6+
return a

‎solution/1100-1199/1137.N-th Tribonacci Number/README.md‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,82 @@ T_4 = 1 +たす 1 +たす 2 = 4
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53-
53+
class Solution:
54+
def tribonacci(self, n: int) -> int:
55+
a, b, c = 0, 1, 1
56+
for _ in range(n):
57+
a, b, c = b, c, a + b + c
58+
return a
5459
```
5560

5661
### **Java**
5762

5863
<!-- 这里可写当前语言的特殊实现逻辑 -->
5964

6065
```java
66+
class Solution {
67+
public int tribonacci(int n) {
68+
int a = 0, b = 1, c = 1;
69+
while (n-- > 0) {
70+
int d = a + b + c;
71+
a = b;
72+
b = c;
73+
c = d;
74+
}
75+
return a;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
int tribonacci(int n) {
86+
long long a = 0, b = 1, c = 1;
87+
while (n--) {
88+
long long d = a + b + c;
89+
a = b;
90+
b = c;
91+
c = d;
92+
}
93+
return (int) a;
94+
}
95+
};
96+
```
97+
98+
### **Go**
99+
100+
```go
101+
func tribonacci(n int) int {
102+
a, b, c := 0, 1, 1
103+
for i := 0; i < n; i++ {
104+
a, b, c = b, c, a+b+c
105+
}
106+
return a
107+
}
108+
```
61109

110+
### **JavaScript**
111+
112+
```js
113+
/**
114+
* @param {number} n
115+
* @return {number}
116+
*/
117+
var tribonacci = function(n) {
118+
let a = 0;
119+
let b = 1;
120+
let c = 1;
121+
while (n--) {
122+
let d = a + b + c;
123+
a = b;
124+
b = c;
125+
c = d;
126+
}
127+
return a;
128+
};
62129
```
63130

64131
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /