原题地址:

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. ReactiveSwift源码解析(十二) MutableProperty基本代码实现

    前两篇博客我们分别聊了ReactiveSwift框架中的负责标记对象的生命周期的类Lifetime以及负责原子性操作的Atomic类的具体代码实现.前两篇博客之所以聊Lifetime以及Atomic的 ...

  2. WeQuant比特币交易策略回测记录

    程序参数 PARAMS = { "start_time": "2017-02-01 00:00:00", "end_time": " ...

  3. SpringMVC的@RequestParam的解释

    自SpringMVC4.2之后,RequestParam内部有4个参数: 1.String name 2.String value 3.boolean required 4.String defaul ...

  4. uval 6657 GCD XOR

    GCD XORGiven an integer N, nd how many pairs (A; B) are there such that: gcd(A; B) = A xor B where1 ...

  5. zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

  6. Why I donot give up cnblogs for Jianshu

    我为什么不放弃博客园使用简书 Why I donot give up cnblogs for Jianshu Chapter0 从2016年8月开始接触简书开始,就有些喜欢上简书了,因为简书支持 ma ...

  7. 【前端】webkit内核浏览器DIV滚动条样式修改和设置

    webkit内核浏览器DIV滚动条样式修改和设置 引言: 最近在做自己的小项目,为了设计出好看的页面费劲了心思,大到页面的整体布局,小到DIV的滚动条都不放过,以下是我通过查阅资料总结的webkit内 ...

  8. Android Studio的Terminal配置

    1.首先检查你的setting设置如下图 2.如果是已经ok的,请在你的Android sdk的文件夹目录下找到adb.exe,并配置环境变量 3.重启as,在terminal内输入 -adb hel ...

  9. 认识大明星——轻量级容器docker知识树点亮

    docker是一个轻量级容器,属于操作系统层面的虚拟化技术,封装了文件系统(AUFS)以及网络互联,进程隔离等特性. 传统虚拟化架构: docker虚拟化架构: 可以看出,docker是没有Guest ...

  10. sql 比较不同行不同字段值

    需求:在一个表table中有两三列,分别是"货物名称"."进货时间"."出货时间"."存放天数",货物名称和两种&quo ...