[LeetCode]Reverse String

題目:Reverse Stringless

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

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

 題意:逆置字符串blog

 思路:很簡單沒什麼好說的ip

string reverseString(string s){
    for (int i = 0, j = s.length() - 1; i < j; ++i, --j){
        s[i] ^= s[j];//交換
        s[j] ^= s[i];
        s[i] ^= s[j];
    }
    return s;
}

題目:Reverse String IIleetcode

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]

 題意:每長度爲k的子串逆置一次。input

思路:string

也很簡單,細心寫沒什麼問題,須要注意的是不足k時的狀況,此時也須要逆置。it

 
 
string reverseStr(string s, int k){
    //i,j分別表示2k中前一個k個元素的起始和終止的位置
    int i = 0, j = k - 1, t = 2 * k - 1;
    for (; t < s.length(); t += 2 * k){//t每次前移2k個位置
        for (int l = i, r = j; l < r; ++l, --r){
            s[l] ^= s[r];//交換
            s[r] ^= s[l];
            s[l] ^= s[r];
        }
        i = t + 1;
        j = i + k - 1;
    }
    if (j >= s.length())j = s.length() - 1;//不足k時
    for (int l = i, r = j; l < r; ++l, --r){
        s[l] ^= s[r];//交換
        s[r] ^= s[l];
        s[l] ^= s[r];
    }
    return s;
}

題目:Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

 

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

題意:逆置一句話中的每一個單詞,每一個單詞使用一個空格隔開。

string reverseWords(string s){
    int i = 0, j = 0;
    while (j < s.length()){
        while (j < s.length() && !isspace(s[j]))++j;//找到間隔的空格字符
        for (int l = i, r = j - 1; l < r; ++l, --r){
            s[l] ^= s[r];//交換
            s[r] ^= s[l];
            s[l] ^= s[r];
        }
        while (j < s.length() && isspace(s[j]))++j;//跳過連續空格
        i = j;
    }
    return s;
}

題目:Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

題意:逆置一個字符串中的元音字母,其餘字母位置不變。

string reverseVowels(string s){
    string vowels("aeiouAEIOU");
    for (int i = 0, j = s.length() - 1; i < j;){
        if (vowels.find(s[i]) == string::npos){
            ++i;
            continue;
        }
        else if (vowels.find(s[j]) == string::npos){
            --j;
            continue;
        }
        //i和j對應的都是元音字母,則交換
        s[i] ^= s[j];
        s[j] ^= s[i];
        s[i] ^= s[j];
        ++i;
        --j;
    }
    return s;
}
相關文章
相關標籤/搜索