Skip to content

Commit 962eed3

Browse files
committed
upd
1 parent de43f19 commit 962eed3

File tree

1,055 files changed

+1156643
-270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,055 files changed

+1156643
-270
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"title": "两数之和",
3+
"tags": [
4+
],
5+
"memory": 128,
6+
"time": 1000,
7+
"source": "https://www.leetcode.cn/problems/two-sum"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## 题目
2+
3+
给定一个整数数组 `nums` 和一个整数目标值 `target`,请你在该数组中找出 **和为目标值** *`target`*  的那 **两个** 整数,并返回它们的数组下标。
4+
5+
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
6+
7+
你可以按任意顺序返回答案。
8+
9+
**示例 1:**
10+
11+
**输入:**nums = \[2,7,11,15\], target = 9
12+
**输出:**\[0,1\]
13+
**解释:**因为 nums\[0\] + nums\[1\] == 9 ,返回 \[0, 1\]
14+
15+
**示例 2:**
16+
17+
**输入:**nums = \[3,2,4\], target = 6
18+
**输出:**\[1,2\]
19+
20+
**示例 3:**
21+
22+
**输入:**nums = \[3,3\], target = 6
23+
**输出:**\[0,1\]
24+
25+
**提示:**
26+
27+
- `2 <= nums.length <= 10^4`
28+
- `-10^9 <= nums[i] <= 10^9`
29+
- `-10^9 <= target <= 10^9`
30+
- **只会存在一个有效答案**
31+
32+
**进阶:**你可以想出一个时间复杂度小于 `O(n^2)` 的算法吗?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
vector<int> a;
5+
int size = nums.size();
6+
int i,j;
7+
for(i=0;i<size;i++)
8+
for(j=i+1;j<size;j++){
9+
if(nums[i]+nums[j] == target){
10+
a.push_back(i);
11+
a.push_back(j);
12+
return a;
13+
}
14+
}
15+
return a;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// struct Solution;
2+
impl Solution {
3+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
4+
for i in 1..nums.len(){
5+
// println!("{} {}",i,nums[i]);
6+
for j in 0..i {
7+
// println!("{} {} ",nums[i],nums[j] );
8+
if nums[j] + nums[i] == target {
9+
return vec![j as i32,i as i32];
10+
}
11+
}
12+
}
13+
vec![]
14+
}
15+
}
16+
17+
// fn test() {
18+
// let nums = vec![2, 7, 11, 15];
19+
// let target = 9;
20+
// assert_eq!(Solution::two_sum(nums, target), vec![0, 1]);
21+
// }
22+
23+
// fn main() {
24+
// test()
25+
// }
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: "rainboy的解析"
3+
date: "2024-02-09 11:08"
4+
update: "2024-02-09 11:08"
5+
author: rainboy
6+
home: https://github.com/rainboylvx
7+
top: true
8+
language:
9+
- cpp
10+
- rust
11+
---
12+
13+
## 解析
14+
15+
16+
题目意思:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
17+
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
18+
19+
20+
示例:
21+
22+
- 给定 nums = [2, 7, 11, 15], target = 9
23+
- 因为 nums[0] + nums[1] = 2 + 7 = 9
24+
- 所以返回 [0, 1]
25+
26+
具体看代码
27+
28+
```cpp
29+
<%- include("./rainboy.cpp") _%>
30+
```
31+
32+
## rust
33+
34+
35+
```rust
36+
<%- include("./rainboy.rs") _%>
37+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"title": "有效的括号",
3+
"tags": [
4+
],
5+
"memory": 128,
6+
"time": 1000,
7+
"source": "https://www.leetcode.cn/problems/valid-parentheses"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## 题目
2+
3+
给定一个只包括 `'('``')'``'{'``'}'``'['``']'` 的字符串 `s` ,判断字符串是否有效。
4+
5+
有效字符串需满足:
6+
7+
1. 左括号必须用相同类型的右括号闭合。
8+
2. 左括号必须以正确的顺序闭合。
9+
3. 每个右括号都有一个对应的相同类型的左括号。
10+
11+
**示例 1:**
12+
13+
**输入:**s = "()"
14+
**输出:**true
15+
16+
**示例 2:**
17+
18+
**输入:**s = "()\[\]{}"
19+
**输出:**true
20+
21+
**示例 3:**
22+
23+
**输入:**s = "(\]"
24+
**输出:**false
25+
26+
**提示:**
27+
28+
- `1 <= s.length <= 10^4`
29+
- `s` 仅由括号 `'()[]{}'` 组成
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const int maxn = 1e5+5;
2+
template<typename T = int,int siz = maxn>
3+
struct mystack{
4+
T sta[siz+5];
5+
int head = 0;
6+
7+
void clear() { head = 0;}
8+
9+
void push(T a) { sta[head++] = a;}
10+
11+
void pop(){head--;}
12+
13+
T top() { return sta[head-1];}
14+
15+
bool empty() { return head == 0;}
16+
17+
int size() { return head;}
18+
};
19+
20+
class Solution {
21+
public:
22+
mystack<char> sta;
23+
bool isValid(string s) {
24+
25+
for(int i = 0 ;i<s.length();i++){
26+
char c = s[i];
27+
28+
if( c == '(' || c == '[' || c == '{')
29+
sta.push(c);
30+
else {
31+
if( sta.empty()) return 0;
32+
char t = sta.top();
33+
if( t == '(' && c == ')')
34+
{
35+
sta.pop();
36+
continue;
37+
}
38+
if( t == '[' && c == ']')
39+
{
40+
sta.pop();
41+
continue;
42+
}
43+
if( t == '{' && c == '}')
44+
{
45+
sta.pop();
46+
continue;
47+
}
48+
return 0;
49+
50+
}
51+
}
52+
return sta.empty();
53+
54+
}
55+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: "rainboy的解析"
3+
date: "2024-02-09 11:08"
4+
update: "2023-02-09 11:08"
5+
author: rainboy
6+
home: https://github.com/rainboylvx
7+
top: true
8+
---
9+
10+
## 解析
11+
12+
使用栈来验证括号序列,是经典代码
13+
14+
15+
```cpp
16+
<%- include("./rainboy.cpp") _%>
17+
```
18+
19+
## 参考
20+
21+
- [algorithm-baseanimation-simulation栈和队列leetcode20有效的括号.md at main · chefyuanalgorithm-base](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%A0%88%E5%92%8C%E9%98%9F%E5%88%97/leetcode20%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.md)
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"title": "图书整理 II",
3+
"tags": [
4+
"","队列"
5+
],
6+
"memory": 128,
7+
"time": 1000,
8+
"source": "https://www.leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## 题目
2+
3+
读者来到图书馆排队借还书,图书管理员使用两个书车来完成整理借还书的任务。书车中的书从下往上叠加存放,图书管理员每次只能拿取书车顶部的书。排队的读者会有两种操作:
4+
5+
- `push(bookID)`:把借阅的书籍还到图书馆。
6+
- `pop()`:从图书馆中借出书籍。
7+
8+
为了保持图书的顺序,图书管理员每次取出供读者借阅的书籍是 **最早** 归还到图书馆的书籍。你需要返回 **每次读者借出书的值**
9+
10+
如果没有归还的书可以取出,返回 `-1`
11+
12+
**示例 1:**
13+
14+
**输入:**
15+
16+
\["BookQueue", "push", "push", "pop"\]
17+
\[\[\], \[1\], \[2\], \[\]\]
18+
**输出:**\[null,null,null,1\]
19+
**解释:**
20+
MyQueue myQueue = new MyQueue();
21+
myQueue.push(1); // queue is: \[1\]
22+
myQueue.push(2); // queue is: \[1, 2\] (leftmost is front of the queue)
23+
myQueue.pop(); // return 1, queue is \[2\]
24+
25+
**提示:**
26+
27+
- `1 <= bookID <= 10000`
28+
- 最多会对 `push``pop` 进行 `10000` 次调用
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stack>
2+
class CQueue {
3+
public:
4+
std::stack<int> s1,s2;
5+
CQueue() {
6+
7+
}
8+
9+
void appendTail(int value) {
10+
s1.push(value);
11+
}
12+
13+
int deleteHead() {
14+
if( s2.empty()) {
15+
16+
while( !s1.empty()){
17+
s2.push(s1.top());
18+
s1.pop();
19+
}
20+
}
21+
22+
if( s2.empty())
23+
return -1;
24+
else {
25+
int t= s2.top();
26+
s2.pop();
27+
return t;
28+
}
29+
}
30+
};
31+
32+
/**
33+
* Your CQueue object will be instantiated and called as such:
34+
* CQueue* obj = new CQueue();
35+
* obj->appendTail(value);
36+
* int param_2 = obj->deleteHead();
37+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: "rainboy的解析"
3+
date: "2024-02-09 11:08"
4+
update: "2023-02-09 11:08"
5+
author: rainboy
6+
home: https://github.com/rainboylvx
7+
top: true
8+
---
9+
10+
## 解析
11+
12+
题目的意思,是有个栈,每一次只能从最上面取
13+
14+
15+
如果不严格按题目的意思一写,直接写一个队列就可以了
16+
17+
18+
如果严格的按题目的要求来写,就需要:**利用两个栈,来实现,每一次都能取到最早的还的书(最早加入队列的元素)**
19+
20+
21+
```
22+
3
23+
2
24+
1
25+
stack 1 : stack 2
26+
```
27+
28+
`stack 1`按顺序放了`1,2,3`3个元素,现在想要取出最开始的`1`,怎么做呢?
29+
30+
```
31+
1
32+
2
33+
3
34+
stack 1 : stack 2
35+
```
36+
37+
把元素从`stack 1`取出,然后放入到`stack 2`里面,然后再从`stack 2`的栈顶取,就可以取到最早的元素
38+
39+
40+
想到方法:
41+
42+
43+
```
44+
- push, 只push 到stack 1
45+
- 取最新的元素pop,
46+
- 如果stack 2不空,就取
47+
- 否则,把stack 1中的所有元素弹出到 stack 2
48+
```
49+
50+
如果证明这种方法是对的呢:
51+
52+
可以想到,总体上`s1`的元素都是`old``s2`的,且`s2`的元素的栈顶一定是`new`于下面的元素,
53+
无论任何时候了,上面的条件都一定成立.
54+
55+
所以`stack 2`栈顶的元素就是最新的
56+
57+
具体看代码
58+
59+
```cpp
60+
<%- include("./rainboy.cpp") _%>
61+
```
62+
63+
## 参考
64+
- [algorithm-baseanimation-simulation栈和队列剑指Offer09用两个栈实现队列.md at main · chefyuanalgorithm-base](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%A0%88%E5%92%8C%E9%98%9F%E5%88%97/%E5%89%91%E6%8C%87Offer09%E7%94%A8%E4%B8%A4%E4%B8%AA%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"title": "用队列实现栈",
3+
"tags": [
4+
"队列"
5+
],
6+
"memory": 128,
7+
"time": 1000,
8+
"source": "https://www.leetcode.cn/problems/implement-stack-using-queues"
9+
}

0 commit comments

Comments
 (0)