题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
package main import ( "strings" ) /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param n int整型 * @return string字符串 */ func trans(s string, n int) string { strList := strings.Split(s, " ") //分割后,保证单词内部顺序 for i, j := 0, len(strList)-1; i < j; i, j = i+1, j-1 { //收尾互换方法很妙 strList[i], strList[j] = strList[j], strList[i] } result := make([]string, 0, len(strList)) for _, v := range strList { //遍历每个单词 temp := []byte(v) for i, _ := range temp { //每个字母改大小写 if temp[i] >= 'a' { temp[i] -= 32 } else if temp[i] <= 90 { temp[i] += 32 } } result = append(result, string(temp))//加入 } return strings.Join(result, " ") //转换 }