go练习1-翻转字符串
//翻转字符串
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-翻转字符串的更多相关文章
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [CareerCup] 1.2 Reverse String 翻转字符串
1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...
- lintcode :Reverse Words in a String 翻转字符串
题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...
- [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 ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- [Swift]LeetCode151. 翻转字符串里的单词 | Reverse Words in a String
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- LeetCode 151 翻转字符串里的单词
题目: 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 ...
- LintCode-53.翻转字符串
翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 说明 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括 如何处理两个单词间的多个空格 ...
随机推荐
- Qt 积累
总结(-) 1> 定时器的使用 QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(u ...
- iOS之美: UIView 与 UIWindow之间的关系
转自:http://leopard168.blog.163.com/blog/static/168471844201381584533466/ 面对iOS初学者,总会被问到一些不常被关注的问题,比如: ...
- JAVA类加载器概念与线程类加载器
类加载器的功能:通过一个类的全限定名来获取描述此类的二进制字节流的过程 java的类加载器大致可以分为两类,一类是系统提供的,一类是由应用开发人员编写的.系统提供的类加载器有以下三种: 引导类加载器( ...
- 【LeetCode】065-验证数字
写在前面 前面研究OS的经历实在是令人心力憔悴..所以换个新鲜的,把自己的刷题感悟整理一番.刷了有些题了,就先拿最近几天hard题打头阵吧.首先说的是(065)Valid Number这个题,其实一眼 ...
- Hystrix的用法demo
1.引入依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- Git中保存用户名和密码
每次操作都需要输入用户名和密码感觉很繁琐,解决方法,在本地的工程文件夹的.git下打开config文件添加: [credential] helper = store 再输入一次用户名密码后就可 ...
- 跟着百度学PHP[11]-PHP当中的异常处理
首先要说一下常见的三种错误: 1.语法错误 2.运行错误 3.逻辑错误 00x1 错误报告及错误级别 PHP的错误分为三个等级 1.注意(notice) 没有变量a 2.警告(warning) 没 ...
- lua字符串对齐函数
最近要用到字符串对齐,开始只是一部分字符串,就直接加空格了,后来发现有很多, 于是写了个字符串对齐的函数. --功能:分割字符串 --参数:带分割字符串,分隔符 --返回:字符串表 function ...
- 使用模板方法模式简化JDBC操作
在使用JDBC时,会重复的写很多重复的代码,例如 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; S ...
- oozie4.3.0+sqoop1.4.6实现mysql到hive的增量抽取
1.准备数据源 mysql中表bigdata,数据如下: 2. 准备目标表 目标表存放hive中数据库dw_stg表bigdata 保存路径为 hdfs://localhost:9000/user/h ...