1. Only one digit in the array appears once, and the others appear in pairs
such as: int a[] = {2,3,5,6,4,3,2,5,6}; Print out 4.
First of all, analyze the problem first.
You can associate the first element of an array with other elements that are behind it, or (an XOR nature: any one number XOR itself is 0) if it is 0, the element is deleted.
For example: The first element of the array is 2, and when it touches the back of the 2 o'clock, it deletes the element behind. A[] = {2,3,5,6,4,3,5,6}.
Then compare the second array, and so on.
When we find this element that appears only once in front of the array, we can return it. Because the problem is that only one number appears once.
int Findonenum (int* a,int size) {int i = 0; for (int j=i+1;j<size-i;j++) {if (i<size) {if ((([a[i]) ^ (A[j]) = = 0) {for (int k=j;k<size-i;k++) {A[k] = a[k+1];} I++;j = i;continue;} if (((a[i) ^ (a[j]))! = 0 && J = = size-i-1) {return a[i];}} return a[i];}
Test function
void Test () {int a[] = {4,2,3,5,6,3,2,5,6};//int a[] = {2,3,5,6,4,3,2,5,6};//int a[] = {2,3,5,6,3,2,5,6,4};int size = Sizeo F (a)/sizeof (a[0]); int ret = Findonenum (a,size); Cout<<ret<<endl;return 0;}
Test results
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/70/wKioL1ce8FeQfM5xAAAJx-6nqrs449.png "title=" 2.PNG " alt= "Wkiol1ce8feqfm5xaaajx-6nqrs449.png"/>
2. If there is a number in the array that appears more than half the length of the array, find out the number.
Analysis:
If the array is unordered, the array is sorted first.
The number of elements in an array is more than half the length of the array, meaning that it appears more often than other numbers.
So when we iterate over the array, we should save two values: One is the number, and the other is the number. When we traverse to the next number, the number is 1 and the number is reduced by 1, if it is the same as the one we saved earlier. If the number is 0, you need to save the next number and set the number to 1. So what we're looking for is probably the last number that will be set to 1.
Bool checkarray (int* a,int size)//Check if the array is valid {if (a == null | | size <= 0) Return false;return true;} Void sort (int* a,int size)//array Sort {if (Checkarray (a,size)) {Int i = 0;int j = 0;for (i=0;i<size-1;i++) {for (j=0;j<size-i-1;j++) {if (a[j] > a[j+1]) {int tmp = a[j];a[j] = a[j+1];a[j+1] = tmp;}}}} return;} Bool mornthanhalf (Int* a,int size,int num)//Whether more than half {int count = 0;for (int i=0;i<size;i++) {if (a[i] == num) {count++;}} if (count*2 >= size) {return true;} Return false;} Int morethan (int* a,int size)//number of Numbers {if (! Checkarray (a,size)) return 0;int num = a[0];int count = 1;for (int i=1;i <size;i++) { if (Num == a[i])//digital Same {count++; } else {count--; } if (count == 0) {num = a[i];count = 1; }}if (Mornthanhalf (a,size,num)) {return num;} return 0;}
Test function
void Test () {int a[] = {3,2,3,3,2,3,4,3,4,2,3,3};int size = sizeof (a)/sizeof (a[0]); Sort (a,size); int ret = Morethan (a,size); Cout<<ret<<endl;}
Test results
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/73/wKiom1ce8s2w9cE8AAAKxSxAGHg701.png "title=" 3.PNG " alt= "Wkiom1ce8s2w9ce8aaakxsxaghg701.png"/>
3. implement a function that can right-click K characters in a string. (The K characters in the string can be left-handed)
ABCDE right-handed one character to get Bcdea
ABCDE right two characters to get Cdeab
ABCDE left-handed one character gets EABCD
Abcde left two characters get DEABC
void Move (char* left,char* right) {when (left < right) {char tmp = *left;*left = *right;*right = tmp;left++;right--;}} int main () {char p[] = {"ABCDE"};int size = strlen (p); int n = 0;cin>>n; n=3//Right-handed N is//move (p,p+n-1); Cbade//move (p+n,p+size-1); Cbaed//move (p,p+size-1); deabc//cout<<p<<endl;//left-n-bit move (p+n,p+size-1); Abedcmove (p,p+n-1); Baedcmove (p,p+size-1); Cdeabcout<<p<<endl;}
Question 3rd extension: Determines whether a string is a string after the rotation of another string.
BOOL Checkequel (char* p,char* q,int size) {char* tmp = new char[size+1];for (int n=0;n<size;n++)//up to size of characters {strcpy ( TMP,P); Each entry and exit must copy the contents of P to Tmpmove (tmp,tmp+n-1); Call the move function, rotate n characters move (tmp+n,tmp+size-1), Move (tmp,tmp+size-1), if (strcmp (tmp,q) = = 0)//rotate the string with Q equal {return true;}} return false;} int main () {char p[] = {"ABCDE"};char q[] = {"Cdeab"};int size = strlen (p); bool ret = Checkequel (p,q,size); if (ret = = True) { cout<< "The string is derived from rotation" <<ENDL;} else{cout<< "string not derived from rotation" <<ENDL;} return 0;}
Test results:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/70/wKioL1ce94rAJvygAAAMwwLyhAo390.png "title=" 5.PNG " alt= "Wkiol1ce94rajvygaaamwwlyhao390.png"/>
This article from "Together to see the Stars" blog, reproduced please contact the author!
Array and string Classic questions