毕业设计餐饮网站建设,wordpress多平台自动提交,代做网站毕业设计,网上如何卖货1、有效的括号
#xff08;1#xff09;题目描述以及输入输出
(1)题目描述:
给定一个只包括 (#xff0c;)#xff0c;{#xff0c;}#xff0c;[#xff0c;] 的字符串 s #xff0c;判断字符串是否有效。(2)输入输出描述#xff1a;
输入#xff1a;s ()1题目描述以及输入输出
(1)题目描述:
给定一个只包括 (){}[] 的字符串 s 判断字符串是否有效。(2)输入输出描述
输入s ()
输出true关键思路
遍历字符串如果是左括号就将对应的右括号入栈
如果是右括号假如栈为空或者与栈顶元素不匹配则认为不匹配否则出战匹配成功
遍历完栈为空则匹配2代码块
class Solution {
public:bool isValid(string s) {stackint sta;if (s.size() % 2 ! 0) // 有奇数个括号肯定不匹配return false; for(int i 0;i s.size();i){if(s[i] ()sta.push());else if(s[i] [)sta.push(]);else if(s[i] {) sta.push(}); // 左括号匹配完成else if(sta.empty() || s[i] ! sta.top()) // 不匹配的两种情况return false;else // 括号匹配栈顶元素出栈sta.pop();}return sta.empty(); // 括号匹配之后判断栈内是否为空}
};2、字符串解码
1题目描述以及输入输出
(1)题目描述:
给定一个经过编码的字符串返回它解码后的字符串。(2)输入输出描述
输入s 3[a]2[bc]
输出aaabcbc关键思路
(1)碰到数字num记录
(2)碰到字符res记录
(3)碰到‘[’,num和res进栈
(4)碰到‘]’,取出栈顶数字将res以倍数形式追加到栈顶字符串2代码块
class Solution {
public:string decodeString(string s) {int num 0; // 记录每次遍历的数字string res ; // 记录每次遍历的字符stackint nums; // 数字栈stackstring str; // 字符栈for(int i 0;is.size();i){if(s[i] 0 s[i] 9)num s[i] - 0;else if((s[i] a s[i] z) || (s[i] A s[i] Z))res res s[i];else if(s[i] [){nums.push(num);num 0;str.push(res);res ;}else if(s[i] ]){int times nums.top();nums.pop();for(int i 0;itimes;i){str.top() res;}res str.top();str.pop();}}return res;}
};3、每日温度
1题目描述以及输入输出
(1)题目描述:
给定一个整数数组 temperatures 表示每天的温度返回一个数组 answer 其中 answer[i] 是指对于第 i 天下一个更高温度出现在几天后。如果气温在这之后都不会升高请在该位置用 0 来代替。(2)输入输出描述
输入: temperatures [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]关键思路
暴力循环2代码块
#include vectorclass Solution {
public:vectorint dailyTemperatures(vectorint temperatures) {int n temperatures.size();vectorint result(n, 0); // 初始化结果向量大小与输入相同初始值为0for (int i 0; i n; i) {for (int j i 1; j n; j) {if (temperatures[j] temperatures[i]) {// 计算等待的天数result[i] j - i;break; // 找到后可以跳出内层循环}}}return result; // 返回结果向量}
};