回文判断
參考:http://zhedahht.blog.163.com/
1、思路:
只要從兩頭開始同時(shí)向中間掃描字串,如果直到相遇兩端的字符都一樣,那么這個(gè)字串就是一個(gè)回文。我們只需要維護(hù)頭部和尾部?jī)蓚€(gè)掃描指針即可;另一種方法是從中間開始、向兩邊擴(kuò)展查看字符是否相等。
2、思路:
遍歷字符串,對(duì)每一個(gè)字符往兩邊擴(kuò)展,看兩側(cè)的字符是否相同,這樣時(shí)間復(fù)雜度是O(n2)。注意擴(kuò)展時(shí)考慮奇數(shù)和偶數(shù)兩種情況。
GetLongestSymmetricalLength 1 int GetLongestSymmetricalLength_2(char* pString) 2 { 3 if(pString == NULL) 4 return 0; 5 6 int symmeticalLength = 1; 7 8 char* pChar = pString; 9 while(*pChar != '\0') 10 { 11 // Substrings with odd length 12 char* pFirst = pChar - 1; 13 char* pLast = pChar + 1; 14 while(pFirst >= pString && *pLast != '\0' && *pFirst == *pLast) 15 { 16 pFirst--; 17 pLast++; 18 } 19 20 int newLength = pLast - pFirst - 1; 21 if(newLength > symmeticalLength) 22 symmeticalLength = newLength; 23 24 // Substrings with even length 25 pFirst = pChar; 26 pLast = pChar + 1; 27 while(pFirst >= pString && *pLast != '\0' && *pFirst == *pLast) 28 { 29 pFirst--; 30 pLast++; 31 } 32 33 newLength = pLast - pFirst - 1; 34 if(newLength > symmeticalLength) 35 symmeticalLength = newLength; 36 37 pChar++; 38 } 39 40 return symmeticalLength; 41 }?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wangpengjie/archive/2013/04/16/3024712.html
總結(jié)
- 上一篇: JQuery EasyUI DataGr
- 下一篇: 编辑器过程总结