C++——LeetCode.151- 字符串中单词反转问题
在上题之前先复习下C++中的字符串操作:
/*注:本文参考了作者wid的文章
http://www.cnblogs.com/mr-wid/archive/2012/12/06/2805107.html
在此谢过作者。*/
字符串的概念:
在C++中,用双引号括起来的一串字符称为字符串,如“Hello”或是“peanut”,字符串使用字符型一维数组存放。
声明一维字符型数组方式如下:
char a[10];
char a[6]="Hello";
char a[]="Hello“;
一定要注意对于"Hello"来说,因为字符串最后一位有‘\0’,所以一定要声明6位, 若使用char a[5]="Hello"则会报错。
字符串的简单操作:
1 #include "stdafx.h" 2 #include <iostream> 3 using namespace std; 4 5 int _tmain(int argc, _TCHAR* argv[]) 6 { 7 char arr[6] = "Hello"; 8 for (int i = 0; i < sizeof(arr); i++) 9 cout << arr[i]<<" "; 10 cout << endl; //逐个输出 11 12 char change[] = "World"; 13 arr[0] = change[0]; 14 arr[4] = change[4]; 15 cout << arr<<endl; //替换字符 16 17 char temp; 18 int n = (sizeof(arr)-1)/2; 19 for (int i = 0; i < n; i++) 20 { 21 temp = arr[i]; 22 arr[i] = arr[sizeof(arr) - 2 - i]; 23 arr[sizeof(arr) - 2 - i] = temp; 24 } 25 cout << arr << endl; //翻转 26 27 return 0; 28 }
可以看到字符串赋值后可以通过数组的方式将其中的字符单独操作。
字符串的输入方式:
1. cin , 使用cin只能输入不含有空格与回车的字符串, 空格后的部分不会通过cin记录到变量里。
cin>>a, 当输入“Hello World”时,只有Hello会被记录到a中。
2.cin.get(),使用方法如下:
char a[50] ;
cin.get(a, 50)
cin.get()不会抛弃换行符,会影响接下来来的输入判断,为了保险起见可以使用 cin.get(a,50).get()来构成一个完整的输入。
3. cin.getline(),效果等同于cin.get(a,50).get(),会获得一行的输入,并抛弃换行符。
字符串反转问题:
先上链接:
https://leetcode.com/problems/reverse-words-in-a-string/
题目很短:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
就是把单词倒过来输出,但是每个单词内保持不变:
先用简单的一维数组来解决问题:
具体操作如下:
1.用户输入单词后,返回单词长度countword,如输入“We are young and fun",单词长度为20,为方便数组操作,返回19,即 a[0]='W', a[19]='n';
2.从末位进行读取,记录单词长度 wordlength 遇到 ‘ ’停止计数并输出,从 a[countword-wordlength]开始逐字输出,直到wordlength变为0.
3.之后继续读取,重新记录单词长度。遇到空格重复步骤2.
4.由于最后一个单词没有空格,所以要加一个判断,在循环控制变量为0时,直接输出这个字符
代码如下:
1 int _tmain(int argc, _TCHAR* argv[]) 2 { 3 4 char a[50]; 5 cin.get(a, 50).get(); 6 int i,j,k; //循环控制变量 7 int countword; //字符串长度 8 int wordlength = 0;//单个单词的长度 9 10 for (i = 0; i < 50; i++) 11 { 12 if (a[i] == '\0') 13 { 14 countword = i-1; //这个长度是带空格的,而且返回的长度减1,方便数组操作。 15 } 16 } 17 18 for (j = countword; j >= 0; j--) 19 { 20 if (j != 0) 21 { 22 if (a[j] != ' ') 23 { 24 wordlength++; 25 } 26 else 27 { 28 for (k = 0; k <wordlength; k++) 29 { 30 cout << a[j + 1 + k]; 31 } 32 wordlength = 0; 33 cout << ' '; 34 } 35 } 36 else 37 { 38 for (k = 0; k <wordlength+1; k++) 39 { 40 cout << a[j + k]; 41 } 42 } 43 44 } 45 cout << endl; 46 return 0; 47 }
结果如下:

这个程序并没有将单词分辨出来,若是要求对单词更复杂的操作,则无法实现,比较完美的办法是使用2维数组对单词进行分类。
利用二维数组分类法:
这个方法的思想就是在用户输入句子以后,检测单词的最大长度maxwordlength与总的单词数量countword,然后利用这两个参数声明一个2维字符型数组。
数组每一行是一个单独的单词,每行的单词结尾都是‘\0’.
输出时从最后一行开始即可,也可以满足佷、更复杂的操作:
代码如下,想欢迎检查下,看看有没有优化的方法:
1 // switchword2.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <vector> 6 #include <iostream> 7 using namespace std; 8 9 int _tmain(int argc, _TCHAR* argv[]) 10 { 11 char inputword[50]; 12 int countword=1; //check the number of words 13 int total_size=0; //the total size of input string 14 int i,j,k =0; //loop control 15 int wordlength=0; //the current word length 16 int maxwordlength=0; //the maximum word length 17 cin.get(inputword, 50).get(); 18 19 for (i = 0; i<50; i++) 20 { 21 if (inputword[i] == '\0') 22 { 23 total_size = i - 1; 24 } 25 else 26 { 27 wordlength++; 28 if (inputword[i] == ' ') 29 { 30 if (wordlength >= maxwordlength) 31 { 32 maxwordlength = wordlength; 33 } 34 countword++; 35 wordlength = 0; 36 } 37 } 38 } 39 40 vector<vector<char> > result_map(countword, vector<char>(maxwordlength)); 41 42 k = 0;//store the word form th 1st row; 43 j = 0;//from first column 44 for (i = 0; i <=total_size; i++) 45 { 46 if (inputword[i] != ' ') 47 { 48 result_map[k][j]=inputword[i]; 49 j++;//move to the next column; 50 } 51 else 52 { 53 k++; // move to the next row 54 j = 0;//this word finished, move to the first column 55 } 56 } 57 58 for (i = countword-1; i >=0 ; i--) 59 { 60 for (j = 0; result_map[i][j]; j++) 61 { 62 cout << result_map[i][j]; 63 } 64 cout << " "; 65 } 66 return 0; 67 }
结果如下

比如说,不输出第四个单词:

利用这个方法可以很轻松地实现,只要在输出时加一个判断,不输出第4行即可。
浙公网安备 33010602011771号