LeetCode Weekly Contest 23
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
- The string consists of lower English letters only.
- 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的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- LeetCode Weekly Contest 117
已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 96 ...
随机推荐
- IOS控件:分歧解决其(UILabel 和 IBAction)
#import <UIKit/UIKit.h> @interface demo7_dayViewController : UIViewController { // 用来显示程序结果 IB ...
- Android studio Unsupported major.minor version 52.0
从目前以及我从网上搜索到的解决方案来说,出现此问题可以从以下两个方法入手: 1. JDK的版本和class版本不一致,通常是jdk版本过低解决方法: 1)使用Java -version和javac - ...
- resin配置
第一种方式: 在本机调试Resin服务,一般是在Eclipse IDE中进行,这样即可以快速的在开发环境中启动/终止Resin,又可以很方便的Debug程序.跟踪异常栈. 在任何版本的 ...
- Scrapy命令和备注
Scrapy命令和备注 1.创建一个新项目(命令行) project是项目名 scrapy startproject <project_name> 2.调试项目(pycharm) 在pyc ...
- E71自带铃声下载
NOKIA-e71 E71系统自带铃声下载,有几个听着不错~ E71铃声下载
- java上传图片,把图片存到本地
思路:js通过FileReader获取图片的Base64,Java解码用IO存到本地. HTML 代码 <input type="file" ng-model="f ...
- Debug技巧:条件断点和远程Debug
一.断点: 1.条件断点 本以为条件断点很难,其实就这样. 二.远程Debug 1.首先看服务器是不是在Debug模式下工作 不是则进行(tomcat/bin/catalina.sh)配置. -Xde ...
- Android异步处理之AsyncTaskLoader简单使用
简介 不管是在Android应用开发还是Android平台开发中,异步处理通常是最基本的coding要求.如果你还在主线程中写一些数据库,网络请求,读写本地文件等操作的话那说明你还不是一个合格的And ...
- CSRF Cross-site request forgery
w 跨站请求伪造目标站---无知用户---恶意站 http://fallensnow-jack.blogspot.com/2011/08/webgoat-csrf.html https://wiki. ...
- pandas 修改指定列中所有内容
如下图: 读取出来的 DataFrame “code” 列内容格式为:“浪潮信息(000977.XSHE)” 格式,目标效果是:000977.XSHE 代码: df["code"] ...