#include<iostream> #include<vector> #include<string> #include<map> using namespace std; struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) :val(x), left(NULL), right(NULL) {} }; class IdenticalTree { public: string IntegertoStr(int m) { if(!m) return "0!"; string res; vector<int> temp; while(m) { temp.push_back(m%10); m=m/10; } for(vector<int>::reverse_iterator riter=temp.rbegin();riter!=temp.rend();riter++) res.push_back(*riter+48); res.push_back('!'); return res; } void Serialize(TreeNode* root,string &res) { //#==空值 !==值的结束符 if(!root) { res+="#!"; return; } res+=IntegertoStr((*root).val); Serialize((*root).left,res); Serialize((*root).right,res); } string toString(TreeNode* root) { string res; Serialize(root,res); return res; } bool chkIdentical(TreeNode* A, TreeNode* B) { //把两个二叉树序列化字符串 string s1=toString(A),s2=toString(B); if(s1.find(s2)!=string::npos) return true; else return false; } };
#include<iostream> #include<string> #include<vector> using namespace std; class Transform { public: bool chkTransform(string A, int lena, string B, int lenb) { if(lena!=lenb) return false; int HashTable1[256]={0},HashTable2[256]={0},i; for(i=0;i<lena;i++) { HashTable1[A[i]]++; HashTable2[B[i]]++; } i=0; while((i<256)&&HashTable1[i]==HashTable2[i]) i++; if(i>=256) return true; else return false; } }; int main() { string a("abce"),b("rbca"); Transform A; bool c=A.chkTransform(a,4,b,4); cout<<c; return 0; }
#include<iostream> #include<string> #include<vector> using namespace std; //遇到字符串翻转的问题,一般都是先局部翻转然后整体翻转 class Translation { public: string stringTranslation(string A, int n, int len) { if(len>n) exit(-1); //1.先翻转0——len-1之间的字符 reverseWord(A,0,len-1); //2.再翻转len——n-1之间的字符 reverseWord(A,len,n-1); //3.然后整体翻转字符串 reverseWord(A,0,n-1); return A; } void reverseWord(string &A,int low,int high) { if(low>=high) return; char temp; while(low<high) { temp=A[low]; A[low]=A[high]; A[high]=temp; low++; high--; } } }; int main() { string a("ABCDE"); Translation A; string res=A.stringTranslation(a,5,3); cout<<res; return 0; }
#include<iostream> #include<string> #include<vector> using namespace std; class Reverse { public: string reverseSentence(string A, int n) { if(A.empty()) return ""; reverseWord(A,0,n-1); //i记录单词的第一个字符,j记录单词的最后一个字符 int i=0,j=0; while(i<n) { while(i<n&&A[i]==' ') i++; if(i>=n) return A; j=i; while(i<n&&A[i]!=' ') i++; if(i>=n) { reverseWord(A,j,n-1); return A; } reverseWord(A,j,i-1); } return A; } void reverseWord(string &A,int low,int high) { if(low>=high) return; char temp; while(low<high) { temp=A[low]; A[low]=A[high]; A[high]=temp; low++; high--; } } }; int main() { string arr("dog loves pig"); Reverse a; a.reverseSentence(arr,13); cout<<arr; return 0; }
#include<iostream> #include<string> #include<vector> using namespace std; class Replacement { public: string replaceSpace(string iniString, int length) { int NumofBlank=0,i,j; for(i=0;i<length;i++) if(iniString[i]==' ') NumofBlank++; //替换后的新串的长度为:原长度+空格数*2 int NewLength=length+2*NumofBlank; string res(NewLength,0); for(i=length-1,j=NewLength-1;j>=0;) if(iniString[i]==' ') { res[j--]='0'; res[j--]='2'; res[j--]='%'; i--; } else res[j--]=iniString[i--]; return res; } }; int main() { string arr("a b c"); Replacement A; string res=A.replaceSpace(arr,5); cout<<res; return 0; }
#include<iostream> #include<string> #include<vector> using namespace std; class Prior { public: string findSmallest(vector<string> strs, int n) { QuickSort(strs,0,n-1); string res; for(int i=0;i<n;i++) res+=strs[i]; return res; } void QuickSort(vector<string> &strs,int low,int high) { if(low<high) { int res=Partition(strs,low,high); QuickSort(strs,low,res-1); QuickSort(strs,res+1,high); } } int Partition(vector<string> &strs,int low,int high) { //以low的值划分,左边比low小右边比low大 string key=strs[low]; while(low<high) { while(low<high&<(key,strs[high])) high--; //类似于快排,此时high比low小 strs[low]=strs[high]; while(low<high&<(strs[low],key)) low++; strs[high]=strs[low]; } strs[low]=key; return low; } bool LT(string s1,string s2)//s1小返回ture { /* 不可单独比较两个字符串的大小,因为b<ba,拼接出来是bba,而bab才是最小的,所以要整体拼接比较 */ string temp1=s1+s2,temp2=s2+s1; if(temp1<=temp2) return true; else return false; } }; int main() { string a("abc"),b("de"),c("cab"); vector<string> arr; arr.push_back(b); arr.push_back(a); arr.push_back(c); Prior A; string res=A.findSmallest(arr,3); cout<<res; return 0; }
思路:
#include<iostream> #include<string> #include<vector> using namespace std; //也可以用栈 class Parenthesis { public: bool chkParenthesis(string A, int n) { int number=0,i; for(i=0;i<n;i++) { if(A[i]=='(') number++; if(A[i]==')') number--; if(number<0)//此时出现的右括号比左括号多, return false; } if(number==0) return true; else return false; } }; int main() { string arr(")a()()"); Parenthesis A; bool c=A.chkParenthesis(arr,7); cout<<c; return 0; }
#include<iostream> #include<string> #include<map> using namespace std; //从左到右依次求出以该字符结尾的情况下向左最远的无重复子串的长度 //其中的最大值就是整体的无重复子串的最长 class DistinctSubstring { public: int longestSubstring(string A, int n) { map<char,int> map_table;//存该字符上一次出现的位置 int pre=0; int *dp=new int[n](); for(int i=0;i<n;i++) { if(map_table.count(A[i]))//比较该字符上次出现的位置与该字符的前一个字符的最长无重复字符的长度 { if(map_table[A[i]]>=(i-pre)) dp[i]=i-map_table[A[i]]; else dp[i]=pre+1; } else dp[i]=pre+1; pre=dp[i]; map_table[A[i]]=i; } int res=0; for(int i=1;i<n;i++) if(dp[i]>res) res=dp[i]; delete []dp; dp=NULL; return res; } }; int main() { string s("aabcb"); DistinctSubstring d; cout<<d.longestSubstring(s,5); return 0; }