Document directory
- 1. Median after merging two sorted Arrays
- 2. It is known that the two linked lists are in order to write a program so that they can be merged.
- 3. search string matching
- 4. Reverse the string to keep the relative position of the substring unchanged.
- 5. Given an array a [n], we want to construct array B [N], where B [J] = A [0] * A [1]… A [N-1]/A [J], division is not allowed during construction
- 6. The following is a declaration of the type mystring. Please add a value assignment operator function for this type.
1. Median after merging two sorted Arrays
int getMedin(int A[],int m,int B[],int n) { int p1=0; int p2=0; int mid=p1; int flag=0; for(int i=0;i<(m+n)/2;) { while(p1<m&&p2<n&&i<(m+n)/2&&!flag) { if(B[p2]<A[mid]) { ++p2;++i; } else { mid=p2; flag=1; ++p1; ++i; } } while(p1<m&&p2<n&&i<(m+n)/2&&flag) { if(A[p1]<B[mid]) { ++p1;++i; } else { mid=p1; flag=0; ++p2; ++i; } } while((p1==m||p2==n)&&i<(m+n)/2) { ++mid; ++i; } } if(flag) return B[mid]; return A[mid]; }
2. It is known that the two linked lists are in order to write a program so that they can be merged.
struct node { int _data; node* _next; }; void merge(node *head1,node *head2,node *head3) { node *pl1=head1->_next; node *pl2=head2->_next; node *pl3; head3=head1; head3->_next=NULL; free(head2); pl3=head3; while(pl1!=NULL&&pl2!=NULL) { if(pl1->_data<=pl2->_data) { pl3->_next=pl1; pl1=pl1->_next; pl3=pl3->_next; } else { pl3->_next=pl2; pl2=pl2->_next; pl3=pl3->_next; } } pl3->_next=NULL; if(pl1!=NULL) pl3->_next=pl1; if(pl2!=NULL) pl3->_next=pl2; }
If you want to obtain the opposite sequence, use the header insertion method.
3. search string matching
# Include <iostream> using namespace STD; // str1 points to the string with search, str2 points to the char * strfind string to be matched (const char * str1, const char * str2) {const char * ptr1 = str1; const char * ptr2 = str2; const char * result = (char *) 0; while (* ptr1! = '\ 0') // The search string is not over {If (* ptr1 = * ptr2) {result = ptr1; while (* ptr2! = '\ 0') {If (* ptr1! = '\ 0') & (* ptr1 = * ptr2) {ptr1 ++; ptr2 ++;} else break ;} if (* ptr2 = '\ 0') Return (char *) result; else if (* ptr1! = '\ 0') {ptr2 = str2; ptr1 = Result + 1; Result = (char *) 0 ;}else {break ;}} ptr1 ++ ;} return (char *) 0;} int main () {char S1 [] = "abcdefghijk"; char S2 [] = "def"; char * PTR = strfind (S1, s2); cout <* PTR <Endl; return 0 ;}
This is a popular pen question. In fact, the code is relatively simple, that is, the logical relationship in it should be clearly considered, so it is not possible to miss any situation 4. The idea of reversing the string to keep the relative position of the substring unchanged: first, the substring in the string is reversed, and then the entire string is reversed.
Void reverse (char * STR, int M, int N) {If (! Str) // the string is null and exit (0); If (M = N) // The substring has only one element and does not need to exchange return; For (INT I = m, j = N; I <j; I ++, j --) // exchange element {char temp = STR [I]; STR [I] = STR [J]; STR [J] = temp;} int main () {char STR [] = "a bc def GH ijk"; int Len = strlen (STR); int m = 0, n = 0; For (INT I = 0; I <Len; I ++) {If (STR [I] = '') {n = I-1; reverse (STR, m, n); // exchange substring M = I + 1; n = I + 1;} reverse (STR, 0, len-1 ); // swap the entire array for (INT I = 0; I! = Len; I ++) cout <STR [I] <""; cout <Endl; return 0 ;}5. Given an array a [n], we want to construct array B [N], where B [J] = A [0] * A [1]… A [N-1]/A [J], division is not allowed during construction
#include<iostream> using namespace std; void printfn(int a[],int n) { for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; } int main() { const int MAX=5; int a[MAX]={1,2,3,4,5}; int b[MAX]; b[0]=1; for(int i=1;i<MAX;i++) { b[i]=b[i-1]*a[i-1]; } /* b[0]=1; b[1]=a[0]; b[2]=a[0]*a[1]; b[3]=a[0]*a[1]*a[2]; b[4]=a[0]*a[1]*a[2]*a[3]; */ for(int i=MAX-1;i>0;i--) { b[i]*=b[0]; b[0]*=a[i]; } /* b[4]=b[4]=a[0]*a[1]*a[2]*a[3]; b[0]=a[4]; b[3]=b[3]*a[4]=a[0]*a[1]*a[2]*a[4]; b[0]=a[4]*a[3]; b[2]=b[2]*a[2]=a[0]*a[1]*a[3]*a[4]; b[0]=a[4]*a[3]*a[2]; b[1]=b[1]*b[4]=a[0]*a[2]*a[3]*a[4]; b[0]=a[4]*a[3]*a[2]*a[1]; */ printfn(a,5); printfn(b,5); return 0; }
6. The following is a declaration of the type mystring. Please add a value assignment operator function for this type.
class CMyString{public:CMyString(char* pdata=NULL);CMyString(const CMyString& str);CMyString& operator=(const CMyString& str);~CMyString(void);private:char* m_data;};The following points should be noted in the written code: 1. Whether to declare the return value as a reference of this type. Continuous value assignment is allowed only when a reference is returned. 2. Whether the input parameter is a constant reference. 3. Whether to release the existing memory of the instance. 4. Whether to judge whether the input parameter is the same as the current instance.
CMyString& CMyString::operator=(const CMyString &str){if(this==&str)return *this ;delete []m_data; m_data=NULL;m_data=new char[strlen(str.m_data)+1];strcpy(m_data,str.m_data);return *this;}
But this is not a perfect code.
Refer to clause 11 in Objective C ++. If new char causes an exception, cmystring will eventually hold a pointer to a deleted char [], which is harmful, you cannot safely delete them, or even securely read them. The only security thing you can do for them is to make a lot of debugging to find out the root cause of the error. To avoid exceptions, make the following changes to the Code:
CMyString& CMyString::operator=(const CMyString &str){if(this!=&str){CMyString strTemp(str);char *ptemp=strTemp.m_data;strTemp.m_data=m_data;m_data=ptemp;}return *this;}
Create a temporary instance strtemp, and then exchange strtemp. m_data with m_data of the instance. Strtemp is a local variable. When the program runs outside the if clause, it automatically calls the destructor, which is equivalent to releasing the memory of the previous instance using the destructor.