上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力。

984. String Without AAA or BBB

Given two integers A and B, return any string S such that:

  • S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters;
  • The substring 'aaa' does not occur in S;
  • The substring 'bbb' does not occur in S.

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:

  1. 0 <= A <= 100
  2. 0 <= B <= 100
  3. It is guaranteed such an S exists for the given A and B.

题目意思:给出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的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. npm 安装 chromedriver 失败的解决办法

    https://segmentfault.com/a/1190000008310875 npm install chromedriver --chromedriver_cdnurl=http://cd ...

  2. oracle 内存不足处理

    alter日志 TNS-12535: TNS:operation timed out ns secondary err code: 12606 nt main err code: 0 nt secon ...

  3. centos安装make

    CentOS 中无法使用make,make install 命令 提示错误:make: command not found make是gcc的编译器,一定要安装 1.安装: yum -y instal ...

  4. JL MTK 安防网关的 wifi 吞吐测试

    基本配置:   删除桥接中的 eth3 : brctl delif  br0 eth3   设置eth3的ip: ifconfig  eth3  192.168.1.100   开启数据转发: ech ...

  5. PHP Xdebug + PhpStorm调试远程服务器代码

    1.服务器(linux centos)安装xdebug pecl install xdebug 注意看安装完成之后会显示 debug.so 的路径,记录下来 2.配置 php.ini如果不知道php. ...

  6. 2018-2019-2 网络对抗技术 20165317 Exp4 恶意代码分析

    2018-2019-2 网络对抗技术 20165317 Exp4 恶意代码分析 实验要求 1.系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间 ...

  7. Linux(CentOs 7)系统重装笔记(一)

    参考文章: https://www.jb51.net/article/95263.htm https://blog.csdn.net/JackLiu16/article/details/7988182 ...

  8. 利用data属性来存json、和取数据还原json

    data属性用JSON.stringify转化为字符串存进去,,,取出来自动会变成json数组的

  9. Oracle 10053

    [10053]alter session set events '10053 trace name context forever,level 1'; <Run your SQL here;&g ...

  10. html-webpack-plugin插件使用时参数配置

    ERROR in multi main Module not found: Error: Cannot resolve 'file' or 'directory' ./public/pages/ind ...