题解 | #字符串变形#

字符串变形

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, " ") //转换
}

全部评论

相关推荐

03-29 14:19
门头沟学院 Java
你背过凌晨4点的八股文么:加油同学,人生的容错率很高,只是一个暑期罢了,后面还有很多机会!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务