LeetCode Weekly Contest 121
上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力。
984. String Without AAA or BBB
Given two integers A
and B
, return any string S
such that:
S
has lengthA + B
and contains exactlyA
'a'
letters, and exactlyB
'b'
letters;- The substring
'aaa'
does not occur inS
; - The substring
'bbb'
does not occur inS
.
Example 1:
Input: A = 1, B = 2
Output: "abb"
Explanation: "abb", "bab" and "bba" are all correct answers.
Example 2:
Input: A = 4, B = 1
Output: "aabaa"
Note:
0 <= A <= 100
0 <= B <= 100
- It is guaranteed such an
S
exists for the givenA
andB
.
题目意思:给出A代表a的个数,B代表b的个数,让你制造一个长度为A+B的字符串S,且满足"aaa"和"bbb"不是S的子串。
题目很简单,就是坑点多。我把所有的坑都踩了。下面是代码:
class Solution {
public:
string strWithout3a3b(int A, int B) {
string ans = "";
if( B > A ) {
while( B && A ) {
if( B - A >= && A ) {
ans += "bba";
B -= ;
A -- ;
} else if( B-A && A ) {
ans += "b";
ans += "a";
B --;
A --;
}
}
if( B ) while( B -- ) ans += "b";
if( A ) ans += "a";
} else if( A > B ){
while( B && A ) {
if( A - B >= && B ) {
ans += "aab";
A -= ;
B -- ;
} else if( A-B && B ) {
ans += "a";
ans += "b";
B --;
A --;
}
}
if( A ) while( A -- ) ans += "a";
if( B ) ans += "b";
} else {
while( B ) {
ans += "ab";
B --;
}
}
return ans;
}
};
LeetCode Weekly Contest 121的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- Error creating bean with name 'dataSource' defined in class path resource 报错解决办法
在学习spring boot 的数据库操作的时候,报了一串错误
- js_1_基本语法
- iOS 自定义一个常规的TabBar
#import "WJWBaseTabBarViewController.h" #import "WJWTabBarButton.h" #import &quo ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- js获取HTML DOM节点元素方法总结
1. 通过顶层document节点获取: (1)document.getElementById(elementId) :通过ID获得节点,如果页面上含有多个相同id的节点,那么只返回第一个节点. ...
- 第二单元电梯调度作业 By Wazaki
figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...
- API 自动化框架
API 自动化框架 个人认为接口自动化测试使用python语言编写更加简单,但所有接口自动化项目代码的思维都是一样的 1.项目包结构 1.case:存放用例数据的包,将所有用例数据以配置文件形式传入 ...
- layui 将后台传过来的值等价替换
<th lay-data="{field:'opentime',width:'12%' , sort: true, align:'center',templet:'#roleTpl'} ...
- Luogu4587 [FJOI2016]神秘数
题目大意:给定一个长度为$n$的正整数序列$a_i$,$m$次询问,每次询问$[l,r]$,求最小的无法表示成$a_l,a_{l+1},\ldots,a_r$的子集之和的正整数. 数据范围:$1\le ...
- 337A
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 60 int comp_inc(const void *first ...