原题地址:

344 Reverse String:

https://leetcode.com/problems/reverse-string/description/

541 Reverse String II:

https://leetcode.com/problems/reverse-string-ii/description/

题目&&解法:

1.Reverse String:

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

这个直接上代码就行了,关于字符串翻转,不管字符数目是奇数还是偶数,都是一样的方法(当然可以调用库函数):

class Solution {
public:
string reverseString(string s) {
int size = s.size();
for (int i = ; i <= (size - ) / ; i++) {
int temp = s[i];
s[i] = s[size - i - ];
s[size - i - ] = temp;
}
return s;
}
};

2. Reverse String II

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]

也是很简单的一道题目,我的做法是这样的:先对前面满足2k的部分进行前k位的翻转,剩余不足的进行讨论,确认有几位需要翻转:

class Solution {
public:
string reverseStr(string s, int k) {
int double_k = * k;
int m = s.size() / double_k;
int n = s.size() % double_k;  //剩余部分
for (int i = ; i < m; i++) {
for (int j = ; j <= (k - ) / ; j++) {
char temp = s[i * double_k + j];
s[i * double_k + j] = s[double_k * i + k - j - ];
s[double_k * i + k - j - ] = temp;
}
}
if (n == ) return s;
int end = n >= k ? k : n;
for (int j = ; j <= (end - ) / ; j++) {
char temp = s[m * double_k + j];
s[m * double_k + j] = s[double_k * m + end - j - ];
s[double_k * m + end - j - ] = temp;
}
return s;
}
};

[LeetCode] 344 Reverse String && 541 Reverse String II的更多相关文章

  1. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  2. LeetCode 541. 反转字符串 II(Reverse String II)

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

  3. [LeetCode] 344. Reverse String 翻转字符串

    Write a function that reverses a string. The input string is given as an array of characters char[]. ...

  4. leadcode 541. Reverse String II

    package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...

  5. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  6. LeetCode 344. Reverse String(反转字符串)

    题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...

  7. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  8. 【leetcode_easy】541. Reverse String II

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

  9. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

随机推荐

  1. 设计模式,Let's “Go”! (中)

    * { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...

  2. 34、锁问题与线程queue

    上一篇随笔我们学了全局解释器锁,前面也学了互斥锁,今天学习一些与锁相关的点,例如递归锁,信号量,Event,还会学习我们已经很熟悉的队列,不过这次的队列是作为一个模块出现的. 一.同步锁 1.join ...

  3. spring文件上传

    Spring文件上传 1,导包: <dependency> <groupId>javax.servlet</groupId> <artifactId>s ...

  4. Java基础---集合

    第一讲    集合框架 先看下面的图: 这就是集合框架的构成.由于数据结构的不同,有不同的集合,也叫容器.下面是集合类的简单介绍. 一.为什么出现集合类? 面向对象语言对事物的体现都是以对象的形式,所 ...

  5. 解析:type t_string is table of varchar2(32767) index by binary_integer

    @ 理解一: table 相当于是数组,这里定义了一个数组类型t_string; INDEX BY BINARY_INTEGER这里是定义数组下标是整数,因为ORACLE中下标可以是字符串. VARC ...

  6. selenium、python、firefox版本配合无敌

     selenium (2.53.6) .python2.7.13. firefox46.0.1 完美

  7. [js高手之路]node js系列课程-图解express+supervisor+ejs用法

    上文通过node js自带的http模块搭建了一个简易的服务器,实际在开发中,一般用的是express框架,本文我们就来讲讲项目开发中必备不可少的几样东西: 服务器( express ) 路由( ex ...

  8. BZOJ 3379: [Usaco2004 Open]Turning in Homework 交作业

    Description     贝茜有C(1≤C≤1000)门科目的作业要上交,之后她要去坐巴士和奶牛同学回家. 每门科目的老师所在的教室排列在一条长为H(1≤H≤1000)的走廊上,他们只在课后接收 ...

  9. 使用Composer安装ThinkPHP5

    1.Windows 系统,下载并运行 Composer-Setup.exe. 2.安装compose.PHP采用的是wampserver集成开发环境(这也之后的问题埋下伏笔). 3.然后在命令行下面, ...

  10. java 实现微博,QQ联合登录

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt313 开发平台 http://connect.qq.com/  http:/ ...