We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
输入:"Let's take LeetCode contest" 输出:"s'teL ekat edoCteeL tsetnoc"
在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reverse-words-in-a-string-iii
The text was updated successfully, but these errors were encountered:
我们可以从字符串尾部向前遍历字符串,把每个字符写入数组,如果遇到空格,则转换数组,拼接字符串,最后返回拼接的字符串就可以。
func reverseWords(s string) string { var ret string var byteArr []byte for i := len(s) - 1; i >= 0; i-- { if s[i] != 32 { byteArr = append(byteArr, s[i]) } else { ret = " " + string(byteArr) + ret byteArr = []byte{} } } return string(byteArr) + ret }
func reverseWords(s string) string { sArr := strings.Split(s, " ") for i, v := range sArr { sArr[i] = reverse([]byte(v), 0, len(v)-1) } return strings.Join(sArr, " ") } func reverse(s []byte, start, end int) string { for start < end { s[end], s[start] = s[start], s[end] start++ end-- } return string(s) }
Sorry, something went wrong.
No branches or pull requests
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例:
提示:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-words-in-a-string-iii
The text was updated successfully, but these errors were encountered: