problem

541. Reverse String II

题意:

给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符。

solution1:

class Solution {
public:
string reverseStr(string s, int k) {
int n = s.size();
int cnt = n / k;
for(int i=; i<=cnt; i++)
{
if(i%==)
{
if(i*k+k<n) reverse(s.begin()+i*k, s.begin()+i*k+k);
else reverse(s.begin()+i*k, s.end());
}
}
return s;
}
};

solution2: 简洁版

就是每2k个字符来遍历原字符串s,然后进行翻转,翻转的结尾位置是取i+k和末尾位置之间的较小值,非常666。

class Solution {
public:
string reverseStr(string s, int k) {
for(int i=; i<s.size(); i+=*k)
{
reverse(s.begin()+i, min(s.begin()+i+k, s.end()));
}
return s;
}
};

参考

1. Leetcode_easy_541. Reverse String II;

2. Grandyang;

【leetcode_easy】541. Reverse String II的更多相关文章

  1. 【LeetCode】541. Reverse String II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  2. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  3. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  4. leadcode 541. Reverse String II

    package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...

  5. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  6. 【leetcode】344. Reverse String

    problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...

  7. LeetCode 541. Reverse String II (反转字符串 II)

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  8. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

  9. [LeetCode&Python] Problem 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

随机推荐

  1. python_tkinter弹出对话框1

    tkinter提供了三个模块,可以创建弹出对话窗口:(使用必须单独导入模块) 1.messagebox 消息对话框 示例:askokcancel import tkinter # 导入消息对话框子模块 ...

  2. Mongo Backup

    #!/bin/sh # This script is run on every mongo node. However, it checks to see if this node is the pr ...

  3. 2018 开始认真学习点python

    2018 伊始,又是春暖花开.俗语,“一年之计在于春”.又是一年立志时. 决定认真学习一些web. 本来倾向与学习NodeJS的.可是之前买的python的书太多了.就先紧手头的资源看了再说吧. 今天 ...

  4. xhEditor实现ctrl+v粘贴word图片并上传

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  5. OI 常用模板 手写

    线性筛素数 (例题 洛谷P3383) bool p[50000010]; int cnt = 0; int prime[10000010]; inline void init() { int N = ...

  6. CF540D Bad Luck Island

    嘟嘟嘟 看到数据范围很小,就可以暴力\(O(n ^ 3)\)dp啦. 我们令\(dp[i][j][k]\)表示这三种人分别剩\(i, j, k\)个的概率.然后枚举谁挂了就行. 这里的重点在于两个人相 ...

  7. ListCtrl 技巧集

    1. ListCtrl 风格       LVS_ICON: 为每个item显示大图标       LVS_SMALLICON: 为每个item显示小图标       LVS_LIST: 显示一列带有 ...

  8. dubbo——高可用性

    一.zookeeper宕机 zookeeper注册中心宕机,还可以消费dubbo暴露的服务 健壮性: 监控中心宕掉不影响使用,只是丢失部分采样数据 数据库宕掉后,注册中心仍能通过缓存提供服务列表查询, ...

  9. List对象遍历时null判断逻辑梳理

          凡是对集合list,set,map,数组等进行循环一定要判断是否为null,增强代码的健壮性.下面以list为例, 使用for循环遍历list对象,处理其中的元素时,需要对null值判断: ...

  10. 新树莓派系统安装ROS记录

    树莓派系统更新了,作为版本控的我怎么能忍住不更新系统,为了提高系统的速度,买了张170M的告诉sd卡,我要说的是,这个高速SD卡的钱花的最值得了,千万不要觉得树莓派都买了4,还要什么高速SD卡(自行车 ...