[LeetCode By Go 87]345. Reverse Vowels of a String

题目

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

解题思路

  1. 从两端遍历字符串,分别找到一个元音字母s[i], s[j],i < j, 然后交换两个字符;
  2. i, j = i+1, j-1 ;继续执行同样的操作,直至遍历完字符串

代码

func isVowels(r rune) bool {
    if r == rune('a') || r == rune('e') || r == rune('i') || r == rune('o') || r == rune('u') || r == rune('A') || r == rune('E') || r == rune('I') || r == rune('O') || r == rune('U'){
        return true
    }
    return false
}

func reverseVowels(s string) string {
    len1 := len(s)
    runeS := []rune(s)
    for i, j := 0, len1-1; i < j; i, j = i+1, j-1{
        for ;!isVowels(runeS[i]) && i < len1-1; i++ {}
        fmt.Printf("i:%+v\n", i)
        for ;!isVowels(runeS[j]) && j > 0; j-- {}

        if i < j {
            runeS[i], runeS[j] = runeS[j], runeS[i]
        }
    }

    ret := string(runeS)

    fmt.Printf("ret:%+v\n", ret)
    return ret
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,776评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,939评论 0 23
  • 标签(空格分隔): 编程 Go官方文档 Using the tour 1.1 Hello, 世界 Welcome...
    uangianlap阅读 1,553评论 0 5
  • 上周四参加光谷的拆书活动,作为观察员点评了小舒,晚上回家有了些感想,别问我为什么现在才发,我有拖延症!!! 如果你...
    牙牙呓语阅读 589评论 0 1