题解 | #字符串变形#

字符串变形

http://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

//别的大佬写的,我加了注释
func trans(s string, n int) string {
	strList := strings.Split(s, " ")
	// reverse
	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 i, _ := range strList {
		//
		temp := []byte(strList[i])
		for j, _ := range temp {
			//A的ASC值为65,Z为90
			//a为97,z为122

			//小写变大写
			if temp[j] >= 97 {
				temp[j] -= 32
				//大写变小写
			} else if temp[j] <= 90 {
				temp[j] += 32
			}
		}
		//将结果转化为string保存进数组
		result = append(result, string(temp))
	}
	//用空格将字符串拼接你起来
	return strings.Join(result, " ")
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 12:05
俺不中了,BOSS遇到了一个hr,我觉得我咨询的问题都很正常吧,然后直接就被拒绝了???
恶龙战士:你问的太多了,要不就整理成一段话直接问他,一个一个问不太好
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
07-07 17:06
已编辑
深圳技术大学 golang
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务