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. 云计算和SDN中的开源交换机介绍以及使用

    之前关于SDN的开发工作都是在控制器层面上(以ryu为主),现在开始了新的工程项目,需要同时修改控制器和交换机的源码,如果后续项目需要,还可能需要加中间层——网络虚拟层,这部分的知识已经在前面读过了相 ...

  2. 查找xcode6的沙盒地目录

    开/查找xcode6的沙盒地目录   用以下代码 打开沙盒目录 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirec ...

  3. @RequestMapping中method的默认值是什么?

    @RequestMapping中method的默认值是什么? 没有默认值,如果不配置method, 则以任何请求形式 RequestMethod.GET, RequestMethod.POST, Re ...

  4. jpa关联映射

    参考:http://www.cnblogs.com/printN/p/6408818.html 官方文档:http://docs.jboss.org/hibernate/orm/5.2/usergui ...

  5. LeetCode-Pathcing Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  6. 第5章 IDA Pro

    5.1 加载一个可执行文件 默认情况下IDA Pro的反汇编代码中不包含PE头或资源节,可以手动指定加载. 5.2 IDA Pro接口 5.2.1 反汇编窗口模式 二进制模式/图形模式: 图形模式:红 ...

  7. Linux磁盘管理命令(fdisk,mount,umount,mkfs)

    查看磁盘:fdisk -l 一块磁盘可以分14个分区 [root@wendyhost ~]# fdisk -l Disk /dev/sda: 64.4 GB, 64424509440 bytes 25 ...

  8. Visualizing mathematical functions by generating custom meshes using FireMonkey(很美)

    Abstract: This article discusses how you can generate your own 3-dimensional mesh for visualizing ma ...

  9. NOIP2018酱油记

    考完了,终于有时间来写游记了. 有一种悲伤,叫做知道正解是什么但是就是不会写... 有一种遗憾,叫做能拿到的分考完才意识到... 有一种$NOIP$,叫做$Day1$原题大赛,$Day2AHOI$.. ...

  10. TCP原理

    1.http://coolshell.cn/articles/11564.html 2.http://coolshell.cn/articles/11609.html 3.一站式学习wireshark ...