0
点赞
收藏
分享

微信扫一扫

LeetCode刷题day14


算法打卡第十四天,今天你刷题了吗

😄😄😄😄😄😄😄😄😄😄

😃😃😃😃😃😃😃😃😃😃

💓💓💓大家一起来刷题!💓💓💓

😍😍😍😍😍😍😍😍😍😍

😘😘😘😘😘😘😘😘😘😘

LeetCode刷题day14_leetcode

​​206. 反转链表​​

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

LeetCode刷题day14_c++_02

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

LeetCode刷题day14_c++_03

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

题目分析

链表的基本使用

AC代码

#include<bits/stdc++.h>
using namespace std;

struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};

ListNode* reverseList(ListNode* l) {
// ListNode *head = new ListNode(-1,l);
ListNode *head2 = new ListNode(-1);
ListNode *p = l,*q;
while(p!=NULL){
q = p;
p = p->next;//往后移动,进入下一个节点.
q->next = head2->next;//头插法,插在前面
head2->next = q;
}
return head2->next;

}

ListNode* reverseList2(ListNode* head) {
ListNode* pre = NULL,*cur= head;//当前节点以及前一个节点. 每次只需要当前节点指向前一个节点即可.
while(cur!=NULL){
ListNode *t = cur->next;//保留下一个位置,防止指针改变反向时丢失位置.
cur->next = pre;
pre = cur;
cur = t;
}
return pre;
}

int main() {
return 0;
}

LeetCode刷题day14_链表_04

​​83. 删除排序链表中的重复元素​​

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 。

返回同样按升序排列的结果链表。

示例 1:

LeetCode刷题day14_链表_05

输入:head = [1,1,2]
输出:[1,2]

示例 2:

LeetCode刷题day14_leetcode_06

输入:head = [1,1,2,3,3]
输出:[1,2,3]

参考代码:

//再来一波传统的做法 
ListNode* deleteDuplicates(ListNode* l) {
ListNode *head = new ListNode(-500,l);
ListNode *pre = head,*cur = head->next;
while(cur!=NULL){
if(pre->val==cur->val){//删除当前节点
cur = cur->next;
pre->next = cur;

}else{//不相等则pre,cur都进行后移
pre = cur;
cur = cur->next;
}
}
return head->next;

}

LeetCode刷题day14_链表_04

​​20. 有效的括号​​

给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

示例 1:

输入:s = "()"
输出:true

示例 2:

输入:s = "()[]{}"
输出:true

示例 3:

输入:s = "(]"
输出:false

示例 4:

输入:s = "([)]"
输出:false

示例 5:

输入:s = "{[]}"
输出:true

题目分析:

栈的基本使用

参考代码

#include<bits/stdc++.h>
using namespace std

bool isValid(string s) {
stack<char> tab;
for(int i = 0;i < s.size();i++){
if(tab.empty()){
tab.push(s[i]);
}else{
char temp = tab.top();
if((temp=='('&&s[i]==')') || (temp=='['&&s[i]==']')||(temp=='{'&&s[i]=='}')){
tab.pop();
}else{
tab.push(s[i]);
}
}
}
if(tab.empty()){
return true;
}else{
return false;
}
}

int main()
{
return 0;
}

LeetCode刷题day14_leetcode_08

​​232. 用栈实现队列​​

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false

说明:

你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

参考代码

#include<bist/stdc++.h>
using namespace std;
class MyQueue {
private:
stack<int> s1,s2;

public:
MyQueue() {

}

void push(int x) {
//由于栈是先进后出,队列是先进先出;思路:把s1中的元素装到s2,再放入要插入的元素,再放回s1中,
//这样出栈的顺序就和队列的顺序一样了.
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
s2.push(x);
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
}

int pop() {
int temp = s1.top();
s1.pop();
return temp;
}

int peek() {
return s1.top();
}

bool empty() {
return s1.empty();
}
};

😜😜😜😜😜 大家卷起来! 😝😝😝😝😝

LeetCode刷题day14_字符串_09


举报

相关推荐

Leetcode刷题Day14

每日刷题 Day14

上好的刷题Day14

day14

Day14 IO

牛客真题编程——day14

0 条评论