题解 | #字符串变形#

字符串变形

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

全部评论

相关推荐

看到这个内容真是闹麻了。。。。。。现在有了AI以后很多人面试都会作弊吗?&nbsp;那对老老实实面试的人岂不是不公平....
程序员牛肉:公平那是对小孩子讲的童话故事,成年人的世界只有能不能接受失败的后果。 你要是能接受面试作弊被发现之后多家公司联合永久拉黑的后果,你就搞。
点赞 评论 收藏
分享
酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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