给你两个字符串 haystack
和 needle
,请你在 haystack
字符串中找出 needle
字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle
不是 haystack
的一部分,则返回 -1
。
示例 1:
输入:haystack = "sadbutsad", needle = "sad" 输出:0 解释:"sad" 在下标 0 和 6 处匹配。 第一个匹配项的下标是 0 ,所以返回 0 。
示例 2:
输入:haystack = "leetcode", needle = "leeto" 输出:-1 解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。
可以直接用:
class Solution {
public:
int strStr(string haystack, string needle) {
return haystack.find(needle);
}
};
需要特别注意next数组的构建过程,特别是i的初始值
class Solution {
public:
void getNext(int* next,const string &needle)
{
int j = 0;
next[0] = j;
// for(int i = 0;i < needle.size(); i++)//特别注意,这里是从1开始循环不是从0,因为next中前两必当为0、1
for(int i = 1 ; i < needle.size() ; i++)
{
while(j > 0 && needle[i] != needle[j])
{
j = next[j - 1];//注意这一步回退,是最长公公前缀
}
if(needle[j] == needle[i])
{
j++;
}
next[i] = j;
}
}
int strStr(string haystack, string needle)
{
if(needle.size() == 0) return 0;
vector<int> next(needle.size());
getNext(&next[0],needle);
int j = 0;
for(int i = 0; i < haystack.size(); i++)
{
while( j > 0 && haystack[i] != needle[j])
{
j = next[j - 1];//不相同则向前回退
}
if(needle[j] == haystack[i])
{
j++;
}
if(j == needle.size())//此为判断出口
{
return (i - needle.size() + 1);
}
}
return -1;
}
};
给定一个非空的字符串 s
,检查是否可以通过由它的一个子串重复多次构成。
示例 1:
输入: s = "abab" 输出: true 解释: 可由子串 "ab" 重复两次构成。
示例 2:
输入: s = "aba" 输出: false
示例 3:
输入: s = "abcabcabcabc" 输出: true 解释: 可由子串 "abc" 重复四次构成。 (或子串 "abcabc" 重复两次构成。)
写不出来主要在于我不知道cpp中的那些库函数,所以老是写不出来
class Solution {
public:
bool repeatedSubstringPattern(string s) {
string result = s + s;
result.erase(result.begin());
result.erase(result.end() - 1);
if(result.find(s) == std::string::npos) return false;
return true;
}
};
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
class MyQueue {
public:
stack<int> stackIn;
stack<int> stackOut;
MyQueue() {
}
void push(int x) {
stackIn.push(x);
}
int pop() {
if(stackOut.empty())
{
while(!stackIn.empty())
{
int temp;
temp = stackIn.top();
stackIn.pop();
stackOut.push(temp);
}
}
int temp = stackOut.top();
stackOut.pop();
return temp;
}
int peek() {
int temp = pop();//this是什么意思,不要this也可以
// stackOut.pop();//注意pop是不返回值的
stackOut.push(temp);
return temp;
}
bool empty() {
return stackIn.empty()&&stackOut.empty();
}
};
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push
、top
、pop
和 empty
)。
实现 MyStack
类:
void push(int x)
将元素 x 压入栈顶。int pop()
移除并返回栈顶元素。int top()
返回栈顶元素。boolean empty()
如果栈是空的,返回true
;否则,返回false
。
简单
class MyStack {
public:
queue<int> que1;
queue<int> que2;
MyStack() {
}
void push(int x) {
que1.push(x);
}
int pop() {
int size = que1.size();
size--;
while(size--)
{
int temp = que1.front();
que1.pop();
que2.push(temp);
}
int temp1 = que1.front();
que1.pop();
size = que2.size();
while(size--)
{
int temp = que2.front();
que2.pop();
que1.push(temp);
}
return temp1;
}
int top() {
return que1.back();
}
bool empty() {
return que1.empty();
}
};
给定一个只包括 '('
,')'
,'{'
,'}'
,'['
,']'
的字符串 s
,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
- 每个右括号都有一个对应的相同类型的左括号。
示例 1:
输入:s = "()" 输出:true
示例 2:
输入:s = "()[]{}" 输出:true
示例 3:
输入:s = "(]" 输出:false
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i = 0; i < s.size(); i++)
{
if(s[i] == '(') st.push(')');
else if(s[i] == '{') st.push('}');
else if(s[i] == '[') st.push(']');
else if(st.empty() || s[i] != st.top()) return false;
else st.pop();
}
return st.empty();//注意还要判断一些st中还有没有东西
}
};
给出由小写字母组成的字符串 S
,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
示例:
输入:"abbaca" 输出:"ca" 解释: 例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
使用栈的思想,但是把result直接当作栈来使用,简化了代码,很方便
class Solution {
public:
string removeDuplicates(string s) {
string result;
for(char i : s)
{
if(result.empty() || result.back() != i)
{
result.push_back(i);
}
else
{
result.pop_back();
}
}
return result;
}
};