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

最新文章

  1. python 打印对象的所有属性值
  2. squid清除缓存
  3. POJ1364 King
  4. grep与find
  5. C++中的static关键字(转)
  6. android SDK Manager更新不了,出现错误提示:&quot;Failed to fetch URL...&quot;!
  7. thinkcmf thinkphp隐藏后台地址
  8. Django初体验
  9. windows server 2008 R2 忘记administrator密码
  10. Best Sequence
  11. 开源cms
  12. 团队作业8----第二次项目冲刺(Beta阶段) 第一天
  13. What’s new in Channels 2 摘译
  14. Spring Cloud的注册中心和服务者,消费者的构建
  15. 深入理解CMA【转】
  16. MapGIS10.3新功能
  17. python---django中url访问方法
  18. SharePreference 注册 registerOnSharedPreferenceChangeListener 无法回调的问题
  19. js类型判断的方法
  20. Grafana安装及配置

热门文章

  1. [Algorithms] Radix Sort
  2. cocos2d-X学习之主要类介绍:精灵角色(CCSprite)
  3. QQ能上,网页打不开
  4. ubuntu jdk 1.7 安装
  5. Python开发【第六章】:面向对象
  6. 23种设计模式UML图
  7. django--个人主页建立练习
  8. CG group
  9. day14生成器
  10. Codeforces Round #468(div2)