1. Recursive merging of ordered linked lists
2. Finding the nearest common ancestor of two nodes in a binary tree
3. Conversion of the binary algorithm
4. Case Conversion
5. Finding the median problem of two identical long arrays
6. Find the number of the K-large of the array
7. Number of k nearest to the median of s
1. Recursive merging of ordered linked lists
node* mergaction (node* head1,node *head2)
{
Node *p=null;
if (head1==null&&head2==null)
return p;
else if (head1==null)
return head2;
else if (head2==null)
return head1;
else
{
if (Head1->data < Head2->data)
{
p = head1;
P->next = Mergaction (head1->next,head2);
}
else
{
p = head2;
P->next = Mergaction (Head1,head2->next);
}
return p;
}
}
2. Finding the nearest common ancestor of two nodes in a binary tree
Class node
{
node * left;
Node * RIGHT;
Node * parent;
};
/* Find the nearest public ancestor of P,q and return it. *
/node * nearestcommonancestor (node * p,node * q);
Node * nearestcommonancestor (node * root,node * p,node * q)
{
node * temp;
while (P!=null)
{
p=p->parent;
temp=q;
while (Temp!=null)
{
if (p==temp->parent)
return p;
temp=temp->parent;
}}}
Solution 2: algorithm idea: If the left subtree of a node contains one node in P,q and the right subtree contains another, then this node is the nearest public ancestor of P,q.
/* Find the nearest public ancestor of a, root, root node, out is the pointer address of the nearest public ancestor */
int Findnca (node* root, node* A, node* B, node** out)
{
if (root = = null)
{
return 0;
}
if (root = = A | | root = = b)
{
return 1;
}
int ileft = Findnca (Root->left, A, b, out);
if (ILeft = = 2)
{
return 2;
}
int iright = Findnca (Root->right, A, b, out);
if (IRight = = 2)
{
return 2;
}
if (ileft + iright = = 2)
{
*out = root;
}
return ileft + iright;
}
void Main ()
{
node* root = ...;
node* a = ...;
node* B = ...;
node* out = null;
int i = Findnca (root, a, b, &out);
if (i = = 2)
{
printf ("Result pointer is%p", out);
}
else
{
printf ("Not find pointer");
}
}
3. Conversion of the binary algorithm
The programming implementation converts decimal integers into arbitrary integers, which the user enters
A decimal number R and the number of binary x that you want to convert, and the program outputs the converted X-binary integer.
Algorithm idea: The decimal number R and the binary x modulo, namely the value of r%x as the last digit of the X-binary integer,
Then make R equals r/x, then take the value of r%x as the second-to-last digit of the X-binary integer ... In turn, direct
Until the last r/x=0.
#include <iostream> #include <string> using namespace std;
/* Converts an integer number to a character number, such as 8-> ' 8 ',12-> ' C '/void Numtochar (char &num) {
/*num is a number between 0 and 9 */if (num<=9&&num>=0) {num+=48; }
/*num is a number from 10 to 15 */else {switch (num) {case 10:num= ' A '; Case 11:num= ' B '; Case 12:num= ' C '; Case 13:num= ' D '; Case 14:num= ' E '; Case 15:num= ' F '; } } }
/* Conversion function--r represents the decimal number to be converted, X is the binary (1<x<17) */void Decimaltransmit (int r,int x) {string result;//save x-Binary char temp; while (r>0) {temp=r%x; Numtochar (temp); Result+=temp; r=r/x; }/* Output converted X-integer */for (int i=result.size () -1;i>=0;i--) cout<<result[i]; cout<<endl; }
int main () {int r,x; cout<< "Please enter a decimal number and the binary to be converted (with a space as the spacer):"; cin>>r>>x; Decimaltransmit (R,X); return 0; }
There is also a mton algorithm, very beautiful, but unfortunately only support the conversion of 1~10 binary.
void m2n (int m, char* mnum, int n, char* nnum)
{
int i = 0;
Char C, *p = Nnum;
This is a place to study, whether to use the least multiplication times.
while (*mnum! = ') '
i = i*m + *mnum++-' 0 ';
To take the remainder
while (i) {
*p++ = i% n + ' 0 ';
I/= n;
}
*p--= ' + ';
Inverse remainder sequence
while (P > Nnum) {
c = *p;
*p--= *nnum;
*nnum++ = C;
}
}
4. Case Conversion
#define TO_UPPERCASE (CH) ((CH)-' a ' + ' a ')
#define TO_LOWERCASE (CH) ((CH)-' a ' + ' a ')
Remember to bring in special values for inspection:
' A '-' a ' a '-' a ' + ' a '
' A '-' a ' a '-' a ' + ' a '
Seems so simple answer I have been in the examination room for a long time, the difference of state
5. Finding the median problem of two identical long arrays
median problem: set x[0:n-1] and y[0:n-1] as two arrays, each containing n ordered numbers. Try to design an O (LOGN) time algorithm to find out the median number of 2N for x and Y.
the core of the problem : identify cutting points that divide large problems into smaller-scale, identical problems, and recursively define the relationship between large and sub-problems. To determine the cutting point : For two arrays, we can select a median from each of them, called X, Y, and call the left and right edges of the two arrays aleft,aright,bleft,bright. In contrast to the two median, if the median number in the X array is greater than the median in the Y array, and the number of elements in the X array is even number, the X array is cut to X[aleft,x+1],y to Y[y,bright], and if the number of elements in the X array is not an even number, the x is cut directly to x[ ALEFT,X]. If the median of the x array is less than the median of the Y-array, the value is reversed. recursive relationship : As described above, for the original problem x[aleft, aright], Y[bleft, BRight]. Suppose the sub-problem after cutting is x[aleft, x+1],y[y,bright]. The median of X[aleft, aright], y[bleft, bRight] problem is solved by solving sub-problem x[aleft, X+1],y[y,bright]. Recursive end condition : The entire recursion ends when the two array of sub-problems obtained after the cut is 2 bits in length. input to the actual problem:
First line: N, the number of elements for the x and Y arrays
Second line: n number of x array, separated by a space
The third line: The n number of the Y array, separating the output of the algorithm with a space:
Median two, separated by a space
#include <stdio.h> #include <stdlib.h> int main () {//two array int len = 0;
Gets the length of the array scanf ("%d", &len);
Dynamically allocating array int * A;
int * b;
A = (int *) malloc (sizeof (int) * len);
b = (int *) malloc (sizeof (int) * len);
Gets the elements for each array for (int i=0; i < Len; i++) {scanf ("%d", &a[i]);
} for (int j=0; J < Len; J + +) {scanf ("%d", &b[j]);
}//two arrays of coordinates int aleft = 0 for the left and right end of the array;
int aright = len-1;
int bleft = 0;
int bRight = len-1;
Two array intermediate coordinates int aMid = 0;
int bmid = 0; The iteration Loop while (true) {//printf ("A-left is%d-is%d, and B-left is%d-right is%d.\n", Aleft, Aright,
Bleft, BRight); If two arrays are left with only two elements, the median must be in the IF ((aright-aleft) = = 1 && (bright-bleft) = = 1) {//
Output the maximum one value of the first row printf ("%d", (A[aleft]>=b[bleft])? A[aleft]:b[bleft]); Outputs the minimum one value of the first row, printf ("%d\n", (a[Aright]<=b[bright])? A[aright]:b[bright]);
End loop break;
} else {//solve the median value of each array aMid = (int) ((aleft + aright)/2);
Bmid = (int) ((bleft + bRight)/2);
If the value of a is less than the B median if (A[amid] < B[bmid]) {//If the existing series in B is an even number, the right value plus one
if ((Bleft + bRight + 1)% 2 = = 0) {aleft = AMid;
BRight = Bmid + 1;
} else {aleft = AMid;
BRight = Bmid;
}}//If the value in B is less than the value of a, else {//If the existing series in A is an even number, the right value plus one
if ((Aleft + aright + 1)% 2 = = 0) {aright = AMid + 1;
Bleft = Bmid;
} else {aright = AMid;
Bleft = Bmid;
}
}
}
} return 0; }
6. Find the number of the K-large of the array:
Too classic, often interview to ask. Specific reference to "Introduction to Algorithms"
#include <cstdlib> #include <iostream>/** * Find the number of K in an array * basic idea: * with the last element x as the axis, divide the array into two parts SA and SB. Elements in the SA are greater than or equal to X,SB elements less than x. There are two cases when the number of elements in the *1.sa is less than K, and the first k-| of SB
The sa| element is the K-large number, and the number of elements in the *2.SA is greater than or equal to K, and the number of K in the SA is returned.
* Time complexity approximate to O (n) */using namespace std;
void Exchange (int &a,int &b) {int temp;
Temp=a;
A=b;
B=temp;
}//fast-partition function int partition (int *a,int l,int r) {int i=l-1,j=l;
int X=a[r];
int temp;
for (j=l;j<r;j++) {if (a[j]>=x)//Put the number larger than x forward {Exchange (a[j],a[i+1]);
i++;
}} Exchange (A[r],a[i+1]);
return i+1;
} int k_element (int *a,int l,int r, int k) {if (l>=r) return a[l];
int q=partition (A,L,R);
if (q==k-1) return a[q]; else if (q>=k) return k_element (A,L,Q-1,K); The number of elements in SA is greater than or equal to K else return K_element (a,q+1,r,k-(q+1)); The number of elements in SA is less than k} int main (int argc, char *argv[]) {int a[100];
int length;
cin>>length;
for (int i=0;i<length;i++) cin>>a[i];
Cout<<k_element (a,0,length-1,4) <<endl;
System ("PAUSE");
return exit_success; }
7. Number of k nearest to the median of s
Problem Description:
Given the set of n complementary numbers and the positive integer k<=n, an O (n) time algorithm is designed to find the number of K nearest the median of s in S.
Algorithm Analysis:
First, through the select algorithm to get the median of this array, and then the most of the number of the set minus the median and then take abs (); Finally, in the second step to get the set, take the number of k small, and then take less than the number of small k-1, their original number, is the number of K required. PS: This algorithm is defective and requires no repetition in the absolute value array, otherwise it cannot be linear.