当前位置: 首页 > news >正文

LeetCode541. Reverse String II

文章目录

    • 一、题目
    • 二、题解

一、题目

541. Reverse String II

Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.

If there are fewer 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 leave the other as original.

Example 1:

Input: s = “abcdefg”, k = 2
Output: “bacdfeg”
Example 2:

Input: s = “abcd”, k = 2
Output: “bacd”

Constraints:

1 <= s.length <= 104
s consists of only lowercase English letters.
1 <= k <= 104

二、题解

class Solution {
public:string reverseStr(string s, int k) {int n = s.length();//循环处理2k个单位for(int i = 0;i < n;i += 2 * k){if(i + k <= n){reverse(s.begin() + i,s.begin() + i + k);}else reverse(s.begin() + i,s.end());}return s;}
};
http://www.lryc.cn/news/217818.html

相关文章:

  • ios原生分享
  • 【Ubuntu】安装chrome之后无法启动
  • 顺序栈练习
  • 安全与HTTP协议:为何明文传输数据成为争议焦点?
  • 【笔记】excel怎么把汉字转换成拼音
  • opencv官网文档学习
  • Android性能优化--Perfetto用SQL性能分析
  • NLP之Bert实现文本分类
  • Pytorch从零开始实战08
  • docker部署Jenkins(Jenkins+Gitlab+Maven实现CI/CD)
  • mapbox使用marker创建html点位信息
  • 项目构建工具maven的基本配置
  • 超详细docker学习笔记
  • Adobe acrobat 11.0版本 pdf阅读器修改背景颜色方法
  • HCIA数据通信——路由协议
  • 十种常见典型算法
  • python-列表推导式、生成器表达式
  • NLP 模型中的偏差和公平性检测
  • YUV图像格式详解
  • 软考高项-质量管理措施
  • Redis那些事儿(一)
  • 【多媒体文件格式】M3U8
  • linux中xargs的实用技巧
  • 【Jmeter】生成html格式接口自动化测试报告
  • 如何将极狐GitLab 漏洞报告导出为 HTML 或 PDF 格式或导出到 Jira
  • uniapp原生插件之安卓文字转拼音原生插件
  • [架构之路-254/创业之路-85]:目标系统 - 横向管理 - 源头:信息系统战略规划的常用方法论,为软件工程的实施指明方向!!!
  • CSP-J 2023真题解析
  • 【Proteus仿真】【51单片机】贪吃蛇游戏
  • Android 原生定位开发(解决个别手机定位失败问题)