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. 解决VS Code保存时候自动格式化

    VS code 保存会自动格式化.以前都是alt+shift+F格式化的,现在一保存就格式化 解决方式:找到你的VScode扩展,把JS-CS-HTML Formatter这个插件禁用就可以解决

  2. rtp rtcp rtsp sip协议了解方法

    vlc有个串流功能 可以流各种推流 方便拉取. 能够方便抓包查看协议. 1.菜单媒体->流 打开对话框 分别可以选择本地文件 关盘文件 网络流 捕获设备做流化处理. 2. 选择 串流->新 ...

  3. !important 的绝对控制样式

    <head> <style type="text/css"> div{background-color: blue !important;} </st ...

  4. error: http://ppa.launchpad.net lucid Release: The following signatures couldn't be verified because

    ubuntu 命令行sudo apt-get update W: GPG error: http://ppa.launchpad.net lucid Release: The following si ...

  5. securecrt远程管理工具连接VM虚拟机

    对桥接,net,host_only网络不理解的,请点击:桥接,net,host_only的区别 我这里以net连接为例: 我们使用securecrt实现net的连接,前提是保证虚拟机的ip和虚拟网卡V ...

  6. 逆袭之旅DAY16.东软实训.Oracle.索引

    2018-07-12 14:44:27 四.索引1.创建索引手动创建:create index 索引名 on 表名(列名,[列名,...])create table employee(pno numb ...

  7. Daily record-June

    June201. Dear, wake up! Seven o'clock now, it's time to get up. Wash your face and to have breakfast ...

  8. NOIP2018复赛获奖名单

    根据<CCF NOIP2018复赛获奖分数线及名额分配办法>,现将NOIP2018复赛获奖名单公告如下: 复赛提高组一等奖获奖名单 复赛提高组二等奖获奖名单 复赛提高组三等奖获奖名单 复赛 ...

  9. IDEA教程之导入maven项目

    通过从网上的开源项目下载源码,一般都是maven管理的项目,此类项目可以通过导入快捷运行项目,如图为下载的一个项目: 2 打开IDEA,点击第二个选项“Import Porject”,然后选择源码根目 ...

  10. dell win10 productkey