541. Reverse String II
来源:互联网 发布:西安软件新城附近楼盘 编辑:程序博客网 时间:2024/04/30 10:13
题目来源【Leetcode】
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]
这道题就是考虑好最后剩下的那部分的转化就没啥问题了
class Solution {public: string reverseStr(string& s, int k) { int l = s.length(); if(l < k) { reverse(&s[0],&s[l]); return s; } int i = 0; while(i < l && l-i >= k){ int end = i+k-1; cout<<i<<" "<<end<<endl; reverse(&s[i],&s[i+k]); i += 2*k; } reverse(&s[i],&s[l]); return s; }};
阅读全文
0 0
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- 541. Reverse String II
- String:541. Reverse String II
- leetcode 541. Reverse String II
- Leetcode-541. Reverse String II
- Ubuntu系统下搭建FTP服务器图解
- JUC原子类-04之 AtomicReference原子类
- PAT (Advanced Level) Practise 1018 Public Bike Management (30)
- 背景建模技术:帧处理(FrameProcessor)模块
- Spring与Angular为主打的简洁轻便的物品管理系统
- 541. Reverse String II
- fluend-elasticsearch日志遇到的问题
- 编写类String的构造函数、析构函数和赋值函数(2)
- Deep Learning学习笔记整理系列之一
- 二叉树前中后遍历互求
- 通畅工程
- 欢迎使用CSDN-markdown编辑器
- C语言攻略
- grub-efi-amd64-signed安装失败