题目:在一个二维数组中每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。完成一个函数,输入这样的数组和一个整数,判断数组中是否含有该整数。
思路:由于该二维数组从左到右,从上到下均为顺序递增的。因此右下角与左上角的数字均大于或小于其周围数字,如果选择改组作为起始点,则无法确定下一步的方向。而右上角的数字是同行最大值、同列最小值,选择该点为起始点则可以根据该数子与目标的比较确定下一步是向右移还是向下移,同理选择左下角也可以实现该函数。
代码:
#include
#include
#include
#include
#include
using namespace std;
const int N = 4;
bool Find(int arr[][N], int n, int m, int value) {
if(arr == NULL || n<=0 || m<=0) return false;
int r=0;
int c=m-1;
while((r=0)) {
int num = arr[r][c];
if(num == value) {
return true;
}else if(num > value) {
--c;
}else {
++r;
}
}
return false;
}
int main() {
int arr[][4] = {
{1,2,8,9},
{2,4,9,12},
{4,7,10,13},
{6,8,11,15}
};
int value=7;
if(Find(arr,4,4,7)) printf("yes\n");
else printf("no\n");
return 0;
}
题目:请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。
代码:
#include
#include
#include
#include
using namespace std;
void ReplaceBlank(char* strin) {
char *s=new char[strlen(strin)*3];
if(strin == NULL) {
return;
}
int length = strlen(strin);
int c = 0;
for(int i=0; iif(strin[i] != ' ') {
s[c]=strin[i];
c++;
}else {
s[c]='%';
c++;
s[c]='2';
c++;
s[c]='0';
c++;
}
}
s[c]='\0';
printf("%s\n",s);
}
int main() {
char strin[] = "we are happy.";
ReplaceBlank(strin);
return 0;
}
根据中序和前序建树
代码:
#include
#include
#include
#include
#include
using namespace std;
struct BinaryTreeNode {
int value;
BinaryTreeNode *lson;
BinaryTreeNode *rson;
};
BinaryTreeNode* BuildTree(int* preOrder, int* inOrder, int n) {
if(preOrder==NULL || inOrder==NULL || n<=0) {
return NULL;
}
BinaryTreeNode* root = new BinaryTreeNode();
root->value = preOrder[0];
int index=0;
while(index0] != inOrder[index]) index++;
if(index >= n) return NULL;
root->lson = BuildTree(preOrder+1, inOrder, index);
root->rson = BuildTree(preOrder+1+index, inOrder+1+index, n-index-1);
return root;
}
树的层序遍历:
#include
#include
#include
#include
#include
#include
using namespace std;
struct BinaryTreeNode {
int value;
BinaryTreeNode *lson;
BinaryTreeNode *rson;
};
BinaryTreeNode* BuildTree(int* preOrder, int* inOrder, int n) {
if(preOrder==NULL || inOrder==NULL || n<=0) {
return NULL;
}
BinaryTreeNode* root = new BinaryTreeNode();
root->value = preOrder[0];
int index=0;
while(index0] != inOrder[index]) index++;
if(index >= n) return NULL;
root->lson = BuildTree(preOrder+1, inOrder, index);
root->rson = BuildTree(preOrder+1+index, inOrder+1+index, n-index-1);
return root;
}
void StateOrder(BinaryTreeNode* root) {
if(root == NULL) {
return;
}
queue q;
q.push(root);
while(!q.empty()) {
BinaryTreeNode* temp = q.front();
q.pop();
printf("%d\n", temp->value);
if(temp->lson != NULL) q.push(temp->lson);
if(temp->rson != NULL) q.push(temp->rson);
}
}
int main() {
int a[] = {1,2,4,5,3,6,7};
int b[] = {4,2,5,1,6,3,7};
StateOrder(BuildTree(a,b,7));
return 0;
}
链表反转:
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
struct ListNode {
int value;
ListNode* nextNode;
};
ListNode* BuildList(ListNode **headNode) {
if(*headNode != NULL) return *headNode;
*headNode = new ListNode();
(*headNode)->value=1;
(*headNode)->nextNode=NULL;
ListNode *pre = *headNode;
for(int i=2; i<=5; i++) {
ListNode *temp = new ListNode();
temp->value = i;
temp->nextNode = NULL;
pre->nextNode = temp;
pre = temp;
}
}
void ReverList(ListNode **headNode) {
while(*headNode == NULL) return;
ListNode *pre = NULL;
ListNode *cur = *headNode;
while(cur != NULL) {
ListNode *Next = new ListNode();
Next = cur->nextNode;
cur->nextNode = pre;
pre = cur;
cur = Next;
}
(*headNode) = pre;
}
void OutputList(ListNode* headNode) {
if(headNode == NULL) {
return;
}
ListNode *temp = headNode;
while(temp != NULL) {
printf("%d\n", temp->value);
temp = temp->nextNode;
}
}
int main() {
ListNode *headNode = NULL;
BuildList(&headNode);
OutputList(headNode);
ReverList(&headNode);
OutputList(headNode);
return 0;
}
给定一个无序序列,找到第k大的数:
#include
#include
#include
#include
using namespace std;
int Partition(int* arrNum, int l, int r) {
if(arrNum==NULL || l>r) return -1;
int mid = (l+r)>>1;
int temp = arrNum[mid];
arrNum[mid] = arrNum[r];
arrNum[r] = temp;
int index = l;
for(int i=l; iif(arrNum[i]if(i>index) {
temp = arrNum[i];
arrNum[i]=arrNum[index];
arrNum[index]=temp;
}
index++;
}
}
temp = arrNum[index];
arrNum[index] = arrNum[r];
arrNum[r] = temp;
return index;
}
int GetNumber(int* arrNum, int n,int k) {
if(arrNum==NULL || n<=0 || k<=0 || k>n) return -1;
k = n-k+1;
int l=0, r=n-1;
while(l<=r) {
int index = Partition(arrNum, l, r);
if(index+1 == k) return arrNum[index];
else if(index+1 < k) l=index+1;
else r=index-1;
}
}
int main() {
int arrNum[] = {0,9,-1,6,7,3,5};
int value = GetNumber(arrNum,7,3);
if(value != -1) printf("%d\n", value);
else printf("not found\n");
return 0;
}
求无序序列中最小k个数
代码:
#include
#include
#include
#include
using namespace std;
int Partition(int* arrNum, int l, int r) {
if(arrNum==NULL || l>r) return -1;
int mid = (l+r)>>1;
int temp = arrNum[mid];
arrNum[mid] = arrNum[r];
arrNum[r] = temp;
int index = l;
for(int i=l; iif(arrNum[i]if(i>index) {
temp = arrNum[i];
arrNum[i]=arrNum[index];
arrNum[index]=temp;
}
index++;
}
}
temp = arrNum[index];
arrNum[index] = arrNum[r];
arrNum[r] = temp;
return index;
}
void Print(int* arrNum, int index) {
for(int i=0; i<index; i++) printf("%d\n", arrNum[i]);
}
int GetNumber(int* arrNum, int n,int k) {
if(arrNum==NULL || n<=0 || k<=0 || k>n) return -1;
//k = n-k+1;
int l=0, r=n-1;
while(l<=r) {
int index = Partition(arrNum, l, r);
if(index+1 == k) {
Print(arrNum, index);
return arrNum[index];
}
else if(index+1 < k) l=index+1;
else r=index-1;
}
}
int main() {
int arrNum[] = {0,9,-1,6,7,3,5};
int value = GetNumber(arrNum,7,3);
if(value != -1) printf("%d\n", value);
else printf("not found\n");
return 0;
}
求素数:
#include
#include
#include
#include
using namespace std;
const int N = 10010;
int GetPrime(int n) {
bool prime[N];
memset(prime, true, sizeof(prime));
if(n<=0) return -1;
for(int i=2; i<=n; i++) {
for(int j=i*2; j<=n; j+=i) {
prime[j] = false;
}
}
for(int i=2; i<=n; i++) {
if(prime[i]) printf("%d ", i);
}
printf("\n");
}
int main() {
GetPrime(10000);
return 0;
}
计算两个链表的公共长度:
不想写了,讲一下重点:
两个链表A,B可能平行或相加,如果是相交的,那么较长的那个(假定A)和B的长度差一定是A在B前面没与B重叠的地方,因为链表没有说长度的,所以终点一定是一样的.