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. 《转》Win7 IIS7.5 安装

    一.进入Win7的 控制面板,选择左侧的 打开或关闭Windows功能. 二.现在出现了安装Windows功能的选项菜单,注意选择的项目,我们需要手动选择需要的功能,下面这张图片把需要安装的服务都已经 ...

  2. asp.net 上传XML,txt 直接读取文件内容

    if (GetUploadFileContent.PostedFile.InputStream.Length < 1) { Msg.Text = "请选择文件"; retur ...

  3. SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)

    (六)高效合理的使用LINQ 1.DataContext中的两个属性 为了能够使用DataContext进行数据提交,在DataContext进行数据查询和操作的过程中,内部会进行数据状态的保持和追踪 ...

  4. POJ 2253 Frogger【最短路变形——路径上最小的最大权】

    链接: http://poj.org/problem?id=2253 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. Powershell Exchange Message Per Day Sent and Reveive

    Powershell Exchange Message Per Day Sent and Reveive # Initialize some variables used for counting a ...

  6. Howto: Performance Benchmarks a Webserver

    Howto: Performance Benchmarks a Webserver last updated June 9, 2006 in CategoriesApache, FreeBSD, Ho ...

  7. Commit message 的写法规范。本文介绍Angular 规范(

    Commit message 的写法规范.本文介绍Angular 规范( http://www.ruanyifeng.com/blog/2016/01/commit_message_change_lo ...

  8. LinkedList基本用法

    https://blog.csdn.net/i_lovefish/article/details/8042883

  9. MySQL 乐观锁与悲观锁

    悲观锁 悲观锁(Pessimistic Lock),顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁. 悲观锁: ...

  10. 8.Query Documents-官方文档摘录

    总结 1 先插入数据 db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom ...