码迷,mamicode.com
首页 > 其他好文 > 详细

字符串旋转

时间:2016-05-05 17:15:55      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

题目:Reverse String (难度一颗星)

Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".

实现:

 1 class Solution {
 2 public:
 3     string reverseString(string s) {
 4         int length = s.length();
 5         int start = 0;
 6         int ends = length - 1;
 7         while(start < ends)
 8         {
 9             int temp = s[start];
10             s[start] = s[ends];
11             s[ends] = temp;
12             start++;
13             ends--;
14         }
15         return s;
16     }
17 };

 

题目: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".

 

实现:

 1 class Solution {
 2 public:
 3     bool validate(char c)
 4   {
 5         return (c == a) || (c == e) || (c == i) || (c == o) || (c == u) || (c == A) || (c == E) || (c == I) || (c == O) || (c == U);
 6   }
 7     
 8     string reverseVowels(string s) {
 9         int starts = 0;
10         int ends = s.size()-1;
11         while (starts < ends)
12     {
13             while (!validate(s[starts]))
14       {
15                 starts++;
16       }
17             while (!validate(s[ends]))
18       {
19                 ends--;
20       }
21             if (starts > ends)
22       {
23                 break;
24       }
25             int temp = s[starts];
26             s[starts] = s[ends];
27             s[ends] = temp;
28             starts++;
29             ends--;
30   }
31         return s;
32 }
33 };

 

 

 

字符串旋转

标签:

原文地址:http://www.cnblogs.com/lrh-xl/p/5462410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!