Day 16 - Reverse String

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~


344. Reverse String

Question

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


Example

Example1

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

Example2

Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is a printable ascii character.

解题

题目

首先先简单的翻译一下题目
给一个Strings,把这个String用in-place的方式做reverse。

Think

作法大致上是这样

  • lowhigh两个index指向String的头跟尾,然後互换就这样XD。

Code

Python

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        low = 0
        high = len(s)-1
        
        while low<=high:
            s[low], s[high] = s[high], s[low]
            low += 1
            high -= 1

C

void reverseString(char* s, int sSize){
    int low = 0;
    int high = sSize-1;
    int tmp;
    
    while (low<=high){
        tmp = s[low];
        s[low] = s[high];
        s[high] = tmp;
        low++;
        high--; 
    }
}

Result

  • Python

  • C

大家明天见/images/emoticon/emoticon29.gif


<<:  密码学基础篇

>>:  Cmder 命令提示字元工具使用参考笔记

Day24 测试写起乃 - Guard

Guard 可以帮助你在开发时监听,并启用测试,让你在开发阶段可以顺便跑测试,当然我也是第一次玩XD...

自我成长书单分享

最近读到从 Sr. 工程师转成工程师主管的的经验,作者同时也分享自己的阅读清单。看了一下阅读清单,经...

求助 Excel VBA 搜寻关键字後贴到其他的tab的写法

各位先备好 如果有一个excel里面资料大概有50列,而每一行都是记载着不同的资讯,譬如:姓名/电话...

[Day30]蓝牙的学习笔记下台一鞠躬

终於完成三十天份量的文章了!!! 虽然蓝牙对我来说是一个已经认识一年多的主题 但是在撰写内容时还是觉...

成员 9 人:在办公室内,建立时空虫洞

根据相对论,如果一对挛生兄弟, 哥哥搭乘宇宙飞船,以接近光速,飞离地球,在宇宙间航行; 当回到地球的...