【leetcode_easy】541. Reverse String II
problem
题意:
给定一个字符串,每隔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的更多相关文章
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【leetcode】344. Reverse String
problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...
- 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 ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- [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 ...
随机推荐
- 标准库类型之vector
上篇中遗留了一个小作业,就是用string中的find_first_not_of和find_last_not_of函数来实现字符串左右空格的去除,先来完成它,实现的思路是先来编写去除左空格,然后再编写 ...
- [唐胡璐]Selenium技巧 - 处理Windows程序(进程)
Selenium WebDriver java 提供了一个专门的WindowsUtils类去和Windows操作系统交互。 就像我们之前说过有时候跑完脚本后,IEDriverServer.exe进程没 ...
- 使用JXL组件操作Excel和导出文件
这段时间参与的项目要求做几张Excel报表,由于项目框架使用了jxl组件,所以把jxl组件的详细用法归纳总结一下.本文主要讲述了以下内容: JXL及相关工具简介 如何安装JXL JXL的基本操作 创建 ...
- 生日礼物 HYSBZ - 1293 【单调队列】【求最短区间的长度,区间需要满足包含所有颜色种类】
生日礼物 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩珠也可以出现在 ...
- Java_hutool 发起请求
//执行接口 String realUrl = "http://localhost:8091/SzeportCodeService/MSGService/encryptAES"; ...
- 007_STM32程序移植之_多通道ADC转换
1. 测试环境:STM32C8T6 2. 测试:使用DMA进行多通道ADC转换 3. 描述:用 ADC 连续采集 12 路模拟信号,并由 DMA 传输到内存.ADC 配置为扫描 并且连续转换模式,AD ...
- Test 1 T2 B 线段树合并
模拟赛的T2,多敲了两行成功爆掉~ 写线段树合并的时候一定要注意一下不能随意新开节点. code: #include <bits/stdc++.h> #define N 100009 #d ...
- Domain Socket本地进程间通信
socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络socket也可用于同一台主机的进程间通讯(通过loop ...
- 【概率论】1-1:概率定义(Definition of Probability)
title: [概率论]1-1:概率定义(Definition of Probability) categories: Mathematic Probability keywords: Sample ...
- sqlserver 存储过程的新建与执行
if Exists(select * from sysobjects where NAME = 'insert_custominfo' and type='P') drop procedure ins ...