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. ActiveMQ Advisory Message

    http://activemq.apache.org/advisory-message.html ActiveMQ broker 内部维持了一些 topic,保存了一些系统信息,客户端可以订阅这些 t ...

  2. Windows添加用户和组命令

    查看当前存在用户: net user 查看当前用户组: net localgroup 添加用户(以添加用户ls密码abcd1234为例): net user ls abcd1234 /add 添加用户 ...

  3. git rebase commit 信息处理

    pick:正常选中 reword:选中,并且修改提交信息: edit:选中,rebase时会暂停,允许你修改这个commit(参考这里) squash:选中,会将当前commit与上一个commit合 ...

  4. koa学习

    http://www.ruanyifeng.com/blog/2017/08/koa.html

  5. linux系统管理 vi编辑器

    Vim是vi improved的缩写是vi的改进版本,vi被认为是事实上的标准编辑器 所有版本的Linux都带有vi编辑器 占用的资源少 与ed,ex等其他编辑器相比,vi对用户更加友好 进入vi编辑 ...

  6. day12-python的类

    类的一般形式: 创建类我们一般用class关键字来创建一个类,class后面跟类名字,可以自定义,最后以冒号结尾,如下所示: class ClassName: '''类的说明''' 类的内容 类的内容 ...

  7. jvm常用参数

    -Xms512m:初始堆内存 -Xmx512m:最大堆内存 -XX:PermSize=256m:初始永久代内存(方法区,非堆) -XX:MaxPermSize=256m:最大永久代内存(方法区,非堆) ...

  8. [Leetcode 101]判断对称树 Symmetric Tree

    [题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  9. SQL-14 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。 注意对于重复的emp_no进行忽略。

    题目描述 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t.注意对于重复的emp_no进行忽略.CREATE TABLE IF NOT EXISTS &q ...

  10. 使用DLL在进程间共享数据

    0x01 DLL在进程间共享数据理论 1.可以在Dll中使用#pragma data_seg建立共享类型的数据段将需要共享的数据分离出来,放置在一个独立的数据段里,并把该段的属性设置为共享,从而实现不 ...