题解 | #字符逆序#

字符逆序

https://www.nowcoder.com/practice/cc57022cb4194697ac30bcb566aeb47b

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
  reader := bufio.NewReader(os.Stdin)
  str,_ := reader.ReadString('\n')
   // reader.ReadLine()
   // If the line was too long for the buffer then isPrefix is set and the beginning of the line is returned. The rest of the line will be returned from future calls. isPrefix will be false when returning the last fragment of the line. The returned buffer is only valid until the next call to ReadLine. ReadLine either returns a non-nil line or it returns an error, never both.The text returned from ReadLine does not include the line end ("\r\n" or "\n").
  line := []byte(str)
  if line[len(line)-1] == '\n' {
    line = line[:len(line)-1]
  }
  for i, j := 0, len(line)-1; i < j; i, j =i+1, j-1 {
    line[i], line[j] = line[j], line[i]
  }
  fmt.Println(string(line))
}

全部评论

相关推荐

01-27 15:41
门头沟学院 Java
想躺平的菜鸡1枚:我项目比你难、学历比你好、还有SCI论文,投java都被拒一大片,现在基本上都要问点agent开发
软件开发投递记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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