大家好,我是毛毛。ヾ(´∀ ˋ)ノ
廢話不多說開始今天的解題Day~
Write a function that reverses a string. The input string is given as an array of characters s
.
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
1 <= s.length <= 10^5
s[i]
is a printable ascii character.首先先簡單的翻譯一下題目
給一個Strings
,把這個String用in-place
的方式做reverse。
作法大致上是這樣
low
跟high
兩個index指向String的頭跟尾,然後互換就這樣XD。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
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--;
}
}
Python
C
大家明天見