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]
class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
n=len(s)
i=0
ans=''
while i<n:
if i+k-1>=n:
ans+=(s[i:])[::-1]
break
elif i+2*k-1>=n:
ans+=(s[i:i+k])[::-1]+s[i+k:]
break
else:
ans+=(s[i:i+k])[::-1]+s[i+k:i+2*k]
i=i+2*k
return ans

  

[LeetCode&Python] Problem 541. Reverse String II的更多相关文章

  1. 【leetcode_easy】541. Reverse String II

    problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...

  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 541. Reverse String II (反转字符串 II)

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

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

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

  7. [LeetCode&Python] Problem 557. Reverse Words in a String III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  8. 541 Reverse String II 反转字符串 II

    给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...

  9. [LeetCode&Python] Problem 917. Reverse Only Letters

    Given a string S, return the "reversed" string where all characters that are not a letter  ...

随机推荐

  1. java关键字总结

    static: 用来修饰成员变量和成员方法,也可以形成静态static代码块,可以形成静态内部类,也可以用于静态导包. 1.静态方法中不能用this和super关键字,不能直接访问所属类的实例变量和实 ...

  2. 使用Swagger2构建强大的RESTful API文档(2)(二十三)

    添加文档内容 在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容.如下所示,我们通 ...

  3. SpringBoot的Profile文件

    1.多Profile文件我们在主配置文件编写的时候,文件名可以是application-{profile}.properties/yml默认使用application.properties的配置 2. ...

  4. js--阻止冒泡,捕获,默认行为

    防止冒泡和捕获 w3c的方法是e.stopPropagation(),IE则是使用e.cancelBubble = true· var el = window.document.getElementB ...

  5. unity中让摄像机移动到鼠标点击的位置和鼠标控制平移视角

    private Vector3 targetVector3; private float movespeed=0.5f; private bool IsOver = true; private Gam ...

  6. hdu3518

    题解: 后缀数组 枚举长度为k(1<=k<=len/2)的满足要求的子串个数 代码: #include<cstdio> #include<cmath> #inclu ...

  7. 在命令行中的vim编辑器加上行号

    在使用vim编辑器时运行脚本程序纠察缺少相应的行号,检测起来非常不方便, 所以在vim编辑器每行前面加上相应的行号: 输入命令::set nu 按下回车,完成

  8. 04 复制删除行为IDA反汇编

     (很久以前的学习记录,放到博客上来)   (IDA5.0版的不知道为何反汇编进去每一行被截断的景象,惨不忍睹......明明是个正版的.只好回来用拷过来的破解版,依然有一些叽里呱啦的问题,懒得管了, ...

  9. 浅议APC

    0x01  APC中断请求级别   在Intel x86体系结构中,外部硬件中断是通过处理器上的"中断管脚"或者一个称为"本地APIC(local APIC)" ...

  10. Positioning

      boxPostion.html <html><head> <title>Box Position</title><meta charset=& ...