LeetCode Weekly Contest 23

1. Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

Example
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Restrictions
  1. The string consists of lower English letters only.
  2. Length of the given string and k will in the range [1, 10000]

实现

#include <iostream>
#include <algorithm> using namespace std; class Solution {
public:
string reverseStr(string s, int k) {
int k2 = k * 2;
int counter = 0;
int size = s.length();
string result = ""; while(1) {
if (counter + k > size) {
string tmp(s.begin()+counter, s.end());
reverse(tmp.begin(), tmp.end());
result += tmp;
return result;
} else {
if (counter + k2 <= size) {
string tmp(s.begin()+counter, s.begin()+counter+k);
reverse(tmp.begin(), tmp.end());
result += tmp;
string tmp2(s.begin()+counter+k, s.begin()+counter+k2);
result += tmp2;
counter += k2;
} else {
string tmp(s.begin()+counter, s.begin()+counter+k);
reverse(tmp.begin(), tmp.end());
result += tmp;
counter += k;
string tmp2(s.begin()+counter, s.end());
result += tmp2;
return result;
}
}
}
}
}; int main() {
Solution* solution = new Solution();
cout << solution->reverseStr("abcdefg", 10) << endl;
return 0;
}

2. Minimum Time Difference

3. Construct Binary Tree from String

4. Word Abbreviation

LeetCode Weekly Contest 23的更多相关文章

  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 86

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

  4. LeetCode Weekly Contest

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

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

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

  6. 【LeetCode Weekly Contest 26 Q3】Friend Circles

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

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

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

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

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

  9. LeetCode Weekly Contest 117

    已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 96 ...

随机推荐

  1. Xcode删除Project上层group

    本来想在Project下New Group,结果点了New group from selection, 结果在Project上级新建了一个group,邮件菜单中无删除项…… 解决方法: 1,关闭Xco ...

  2. ZOJ 3941 Kpop Music Party(省赛, 贪心)

    Kpop Music Party Time Limit: 2 Seconds      Memory Limit: 65536 KB Marjar University often hosts Kpo ...

  3. Powershell About LocalGroupMembership

    一: 结合active directory获取本地群组成员信息(包含本地用户和域用户,及域用户的情况 $DBServer = "xxxx" $DBDatabase = " ...

  4. 2015-04-14——css3 @media

    //判断横竖屏 @media screen and (min-aspect-ratio: 13/13) { body {background-color:red;}}  //屏幕宽高比,必须是除数形式 ...

  5. 【sed / awk脚本编写】

    awk awk分为BEGIN部分,正则匹配部分,END部分三部分. 我一般在BEGIN部分定义一些变量,正则部分用于匹配和执行一些解析和统计,END部分用于输出结果. 总体结构: awk 'BEGIN ...

  6. elastic search远程测试

    elastic search远程测试 推荐:elastic官方教程:https://www.elastic.co/guide/en/elasticsearch/reference/6.2/index. ...

  7. 报错:Cannot remove entries from nonexistent file c:\program files\anaconda3\lib\site-packages\easy-install.pth

    Outline 这两天通过“掘金量化终端”跑模型策略,之前装好环境一直ok,可以顺畅的Running~ 下午重装了下 Anaconda,刚才跑的时候提示 缺少“gm”模块 (掘金量化必须包): 就按照 ...

  8. Android Studio 启动时不自动打开项目

    主界面中,选择单击菜单栏 File ,单击 Settings 选择 Appearance & Behavior 选项.选择System Settings选项.取消勾选Reopen last p ...

  9. Elasticsearch中文搜索环境搭建

    Elasticsearch是一个建立在全文搜索引擎 Apache Lucene™ 基础上的搜索引擎,功能强大,最近刚好要研究搜索这一块,简要记录备日后查阅 安装Java JDK,由于Lucene是用J ...

  10. go——标准命令

    Go本身包含大量用户处理Go程序的命令和工具. 1.子命令 go命令的子命令:build:用于编译指定的代码包或Go语言源码文件. 命令源码文件会被编译成可执行文件,并存放到命令执行的目录或指定目录下 ...