字符串中的元音字母翻转

345. Reverse Vowels of a String

 
 My Submissions
 
  • Total Accepted: 31038
  • Total Submissions: 86077
  • Difficulty: Easy

 

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. e 和 e 交换; 2. e 和 o交换

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

 

Subscribe to see which companies asked this question

 1 char* reverseVowels(char* s) {
 2     
 3     int left,right;
 4     char tmp;
 5 
 6     char a[10]={'a','e','i','o','u','A','E','I','O','U'};
 7     char *tmp_a;
 8     tmp_a = a;
 9     int  i= 0;
10     
11     left = 0;
12     right = strlen(s)-1;
13     
14     
15     //两边同时移动指针赋值
16     while(left < right ){
17         tmp = s[left];
18         //左边判断
19         while(i < 10){
20             if(tmp_a[i++] == s[left]){
21                 i = 0;
22                 break;
23             }
24 
25         }
26         
27         if(i != 0){
28            left++;
29            i = 0;//回到起始位置
30            continue;
31         }
32         //右边判断  
33         while(i < 10){
34             if(tmp_a[i++] == s[right]){
35                 i = 0;
36                 break;
37             }  
38         }
39         
40         if(i != 0){
41            right--;
42            i = 0;//回到起始位置
43            continue;
44         }
45         
46         //tmp = s[left];
47         s[left] = s[right];
48         s[right] = tmp;
49         left++;
50         right--;
51         i = 0;
52     }
53 
54      return s;
55 
56 }
View Code

 

posted on 2016-07-24 09:37  人生一世,草木一秋。  阅读(376)  评论(0编辑  收藏  举报