当前位置:网站首页>365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
2022-08-01 21:35:00 【ShowM3TheCode】
1374. 生成每种字符都是奇数个的字符串

代码实现(首刷自解)
class Solution {
public:
string generateTheString(int n) {
string ans = "";
if (n % 2 == 0) {
ans = "z";
for (int i = 0; i < n - 1; i++) {
ans.append("a");
}
}
else {
for (int i = 0; i < n; i++) {
ans.append("a");
}
}
return ans;
}
};
2. 两数相加

代码实现(首刷自解)
/** * Definition for singly-linked list. * 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) {} * }; */
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int flag = 0;
ListNode* pl1 = l1, *pl2 = l2;
ListNode* ans = new ListNode();
ListNode* curNode = ans;
int curVal = 0;
while (pl1 && pl2) {
curVal = pl1->val + pl2->val + flag;
ListNode* tmp = new ListNode(curVal % 10);
curNode->next = tmp;
curNode = tmp;
flag = 0;
if (curVal >= 10) flag = 1;
pl1 = pl1->next;
pl2 = pl2->next;
}
while (pl1) {
curVal = pl1->val + flag;
ListNode* tmp = new ListNode(curVal % 10);
curNode->next = tmp;
curNode = tmp;
flag = 0;
if (curVal >= 10) flag = 1;
pl1 = pl1->next;
}
while (pl2) {
curVal = pl2->val + flag;
ListNode* tmp = new ListNode(curVal % 10);
curNode->next = tmp;
curNode = tmp;
flag = 0;
if (curVal >= 10) flag = 1;
pl2 = pl2->next;
}
if (flag) {
ListNode* tmp = new ListNode(1);
curNode->next = tmp;
}
return ans->next;
}
};
20. 有效的括号

代码实现(自解)
class Solution {
private:
bool match(char l, char r) {
if (l == '(') return r == ')';
if (l == '{') return r == '}';
if (l == '[') return r == ']';
return false;
}
public:
bool isValid(string s) {
set<char> left = {
'(', '{', '[' };
set<char> right = {
')', '}', ']'};
stack<char> _stack;
for (char c : s) {
if (left.count(c)) {
_stack.push(c);
}
else {
if (_stack.empty()) return false;
if (match(_stack.top(), c)) _stack.pop();
else return false;
}
}
if (!_stack.empty()) return false;
return true;
}
};
边栏推荐
- 基于php影视资讯网站管理系统获取(php毕业设计)
- [Chinese tree tags - CTB]
- 2022牛客多校联赛第五场 题解
- Spark shuffle调优
- 附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
- Taobao's API to get the list of shipping addresses
- 线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
- 用户量大,Redis没法缓存响应,数据库宕机?如何排查解决?
- Chapter 12, target recognition of digital image processing
- Image fusion GANMcC study notes
猜你喜欢

ImportError: `save_weights` requires h5py.问题解决

Shell编程条件语句

C语言_typedef和结构体

小程序--独立分包&分包预下载

对C语言结构体内存对齐的理解

An online JVM FullGC made it impossible to sleep all night and completely crashed~

在Cesium中实现与CAD的DWG图叠加显示分析

方舟:生存进化PVE模式和PVP模式

作业8.1 孤儿进程与僵尸进程

How to choose Visibility, Display, and Opacity when interacting or animating
随机推荐
Taobao's API to get the list of shipping addresses
MySQL相关知识
C陷阱与缺陷 第7章 可移植性缺陷 7.10 首先释放,然后重新分配
Suggestions and answer 8.1 C traps and defect chapter 8
C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
分类接口,淘宝分类详情 API
基于php湘西旅游网站管理系统获取(php毕业设计)
C陷阱与缺陷 第7章 可移植性缺陷 7.8 随机数的大小
Realize the superposition display analysis of DWG drawing with CAD in Cesium
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.4 K&R C
磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
Scala practice questions + answers
基于php旅游网站管理系统获取(php毕业设计)
HCIP---多生成树协议相关知识点
C语言_联合体共用体引入
HCIP---企业网的架构
Transplant MQTT source code to STM32F407 development board
空间数据库开源路,超图+openGauss风起禹贡
LeetCode
虚拟内存与物理内存之间的关系