//翻转字符串

func T1_1() {

	str := "你好helloworld!"
fmt.Println("翻转前", str)
var ret string
for _, v := range str { //_ 占位使用
ret = string(v) + ret
}
fmt.Println("翻转后", ret)
} func T1_2() {
str := "你好!go"
fmt.Println("翻转前", str)
tmp := []rune(str)
strLen := len(tmp)
ret := make([]rune, strLen) for i := 0; i < strLen; i++ {
ret[i] = tmp[strLen-i-1]
}
fmt.Println("翻转后", string(ret))
} func T1_3() {
str := "你好!go"
fmt.Println("翻转前", str)
tmp := []rune(str)
var ret string
for i, j := 0, len(tmp)-1; i < j; i, j = i+1, j-1 {
tmp[i], tmp[j] = tmp[j], tmp[i]
}
ret = string(tmp)
fmt.Println("翻转后", ret)
}

  个人觉得第三种方法最优雅

go练习1-翻转字符串的更多相关文章

  1. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  2. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  3. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  4. lintcode :Reverse Words in a String 翻转字符串

    题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...

  5. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  6. [LeetCode] Reverse String II 翻转字符串之二

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  7. [Swift]LeetCode151. 翻转字符串里的单词 | Reverse Words in a String

    Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...

  8. C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. LeetCode 151 翻转字符串里的单词

    题目: 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 ...

  10. LintCode-53.翻转字符串

    翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 说明 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括 如何处理两个单词间的多个空格 ...

随机推荐

  1. Introduction to SIFT (Scale-Invariant Feature Transform)

    SIFT OpenCV 官方文档: https://docs.opencv.org/master/da/df5/tutorial_py_sift_intro.html https://opencv-p ...

  2. Python强制抛出自定义异常

    raise Exception("My Exception") 当程序运行到这行时,会抛出异常,打印出Exception: My Exception

  3. Atitit.spring体系结构大总结

    Atitit.spring体系结构大总结 1. Srping mvc 1 2. Ioc 4 3. ApplicationContext在BeanFactory的基础上构建,区别 4 4. Aop 5 ...

  4. jQuery新建HTML Element

    举个例: 创建一个<i>HelloWorld.</i> var italicText = $("<i></i>").text(&qu ...

  5. Vanya and Brackets

    Vanya and Brackets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  6. love2d--glsl02变量和语句

    Shader分为顶点着色器和片段着色器,GPU先处理顶点再处理片段,大概可以这么理解, 顶点着色器处理模型里的点,输出处理后的数据,这些数据经过GPU其它模块处理后传入 片段着色器,经片段着色器综合后 ...

  7. QT 5.7.0 移植之 tslib 编译配置

    QT5.7 编译请参考:http://www.cnblogs.com/chenfulin5/p/5798764.html 最新的 tslib 是从他的 github 下载下来的. 地址是:https: ...

  8. [fork]Linux中的fork函数详解

    ---------------------------------------------------------------------------------------------------- ...

  9. [kernel]如何主动触发一次kernel panic

    Step1: echo 1 > /proc/sys/kernel/sysrq 或者如果不想每次运行上面的命令,可以echo "kernel.sysrq=1" >> ...

  10. Spider Studio 新版本 (20140225) - 设置菜单调整 / 提供JQueryContext布局相关的方法

    这是年后的第一个新版本, 包含如下: 1. 先前去掉的浏览器设置功能又回来了! 说来惭愧, 去掉了这两个功能之后发现浏览经常会被JS错误打断, 很不方便, 于是乎又把它们给找回来了. :) 2. 为J ...