当前位置:网站首页>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;
}
};
边栏推荐
- C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
- 方舟:生存进化官服和私服区别
- 关键字搜索:“淘宝商品 API ”
- 位运算简介
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
- Realize the superposition display analysis of DWG drawing with CAD in Cesium
- 小程序--分包
- PX4模块设计之十五:PX4 Log设计
- 365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
- ModuleNotFoundError: No module named ‘yaml‘
猜你喜欢

Realize the superposition display analysis of DWG drawing with CAD in Cesium

JS hoisting: how to break the chain of Promise calls

ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!

with语句和上下文管理器

C语言_联合体共用体引入

NFT的10种实际用途(NFT系统开发)

回收租凭系统100%开源无加密 商城+回收+租赁

Chapter 12, target recognition of digital image processing

File operations of WEB penetration

Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
随机推荐
C专家编程 第1章 C:穿越时空的迷雾 1.3 标准I/O库和C预处理器
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.1 The Prehistoric Phase of the C Language
An online JVM FullGC made it impossible to sleep all night and completely crashed~
如何优雅的性能调优,分享一线大佬性能调优的心路历程
C陷阱与缺陷 附录B Koenig和Moo夫妇访谈
基于php旅游网站管理系统获取(php毕业设计)
牛血清白蛋白刺槐豆胶壳聚糖缓释纳米微球/多西紫杉醇的纳米微球DTX-DHA-BSA-NPs
CS-NP白蛋白包覆壳聚糖纳米颗粒/人血清白蛋白-磷酸钙纳米颗粒无机复合材料
shell编程规范与变量
JSD-2204-Knife4j框架-处理响应结果-Day07
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
C语言_联合体共用体引入
小程序--分包
位运算简介
C陷阱与缺陷 第7章 可移植性缺陷 7.11 可移植性问题的一个例子
Realize the superposition display analysis of DWG drawing with CAD in Cesium
C expert programming
RecycleView的使用
基于php湘西旅游网站管理系统获取(php毕业设计)