原题地址:

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".

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

  1. class Solution {
  2. public:
  3. string reverseString(string s) {
  4. int size = s.size();
  5. for (int i = ; i <= (size - ) / ; i++) {
  6. int temp = s[i];
  7. s[i] = s[size - i - ];
  8. s[size - i - ] = temp;
  9. }
  10. return s;
  11. }
  12. };

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:

  1. Input: s = "abcdefg", k = 2
  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位的翻转,剩余不足的进行讨论,确认有几位需要翻转:

  1. class Solution {
  2. public:
  3. string reverseStr(string s, int k) {
  4. int double_k = * k;
  5. int m = s.size() / double_k;
  6. int n = s.size() % double_k;  //剩余部分
  7. for (int i = ; i < m; i++) {
  8. for (int j = ; j <= (k - ) / ; j++) {
  9. char temp = s[i * double_k + j];
  10. s[i * double_k + j] = s[double_k * i + k - j - ];
  11. s[double_k * i + k - j - ] = temp;
  12. }
  13. }
  14. if (n == ) return s;
  15. int end = n >= k ? k : n;
  16. for (int j = ; j <= (end - ) / ; j++) {
  17. char temp = s[m * double_k + j];
  18. s[m * double_k + j] = s[double_k * m + end - j - ];
  19. s[double_k * m + end - j - ] = temp;
  20. }
  21. return s;
  22. }
  23. };

[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. VisualSVN安装图解

    VisualSVN安装教程... ----------------------------------- 参考网址:https://www.visualsvn.com/server/download/ ...

  2. java注释中使用注解@see

    缘起 在写java时,有时需要写注释,而为了更好的描述,需要引用和参考其他代码.为了让阅读者更好的体验,javadoc中支持链接跳转,这就需要用到注解@see. @see用法 注解@see可以在注释中 ...

  3. 灾难恢复-boot分区的恢复方法

    boot分区是系统启动中最重要的部分,如果服务器由于病毒攻击又或者被管理员误删除了boot分区.那么就会存在潜在的风险.为什么说是潜在的风险?因为boot分区被删除后系统仍在继续运行,看似无状况但是在 ...

  4. webpack常见的配置总结 ---只是一些常见的配置

    早期的构建工具grunt ,gulp 帮助我们配置一些开发环境,省去一些我们调试和重复的工作 现在我们的构建工具一般是webpack ,目前建议大家用3.0以上的版本 现在市场上比较优秀的构建工具,个 ...

  5. java初步—参数的值传递

    校招季,本人匆匆忙忙地参加各种宣讲会,几次笔试下来都遇到同一个题目,而且全都错在同一想法上,方知自己的基础实在不太牢固,因此特别写在博客上提醒自己要脚踏实地地学习!不多说了,题目如下: public ...

  6. 通过官网找到spring的jar包

    1.官网为:https://spring.io/ 2.打开之后,点击:PROJECTS,如图所示: 3.点击第三个:SPRING FRAMEWORK,如图所示: 4.进入之后,找到features,点 ...

  7. MySQL(九)之数据表的查询详解(SELECT语法)二

    上一篇讲了比较简单的单表查询以及MySQL的组函数,这一篇给大家分享一点比较难得知识了,关于多表查询,子查询,左连接,外连接等等.希望大家能都得到帮助! 在开始之前因为要多表查询,所以搭建好环境: 1 ...

  8. ajax请求成功前loading

    1.jquery方式 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  9. 详解java设计模式之责任链模式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt175 从击鼓传花谈起 击鼓传花是一种热闹而又紧张的饮酒游戏.在酒宴上宾客依次 ...

  10. TitleLayout——一个Android轻松实现标题栏的库

    TitleLayout 多功能.通用的.可在布局或者使用Java代码实现标题栏: 支持沉浸式状态栏: 支持左侧返回按钮不需要手动实现页面返回: 支持左侧按钮,中间标题,右边按钮点击 左侧支持图片+文字 ...