LeetCode Weekly Contest 23
2017-03-12 11:54
302 查看
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 之 541. Reverse String II
- LeetCode Weekly Contest 23 之 536.Construct Binary Tree from String
- LeetCode Weekly Contest 23 之 539. Minimum Time Difference
- LeetCode Weekly Contest 24 之 543. Diameter of Binary Tree
- LeetCode Weekly Contest 24 之 542.01 Matrix
- LeetCode Weekly Contest 40(1) - Average of Levels in Binary Tree
- LeetCode Weekly Contest 43【总结】
- LeetCode Weekly Contest 33解题思路
- LeetCode Weekly Contest 43解题思路
- LeetCode Weekly Contest 37解题思路
- LeetCode Weekly Contest 25
- LeetCode Weekly Contest 51
- LeetCode Weekly Contest 27解题思路
- (dp)LeetCode Weekly Contest 34 -Non-negative Integers without Consecutive Ones
- LeetCode Weekly Contest 36解题思路
- LeetCode Weekly Contest 40(2) - Solve the Equation
- LeetCode Weekly Contest 28
- LeetCode Weekly Contest 28解题思路
- LeetCode Weekly Contest 48解题思路
- 73th LeetCode Weekly Contest Escape The Ghosts