前言:18/06/06開始學習,每個程式都會用C寫一遍,因書中用C++舉例,也會換種思路寫,供學習和參考!!!很推薦這本書很不錯,準備入手,一般不買實體書,都用電子書,因一般都看一遍,但這本會看很多遍!無論剛畢業,還是跳槽都可以看一下...
面試題3:二維數組中的查找

題目如圖,所有題目截圖均引自《劍指offer》,接下來不再說明!
書中從右上角開始查找,我的程式從左下角查找:
#include<stdio.h>
#include<stdbool.h>
bool Find(int (*num)[4],int rows,int column,int number)
{
bool found = false;
if(num!=NULL && rows>0 && column>0)
{
int col = 0;
int row = rows-1;
while(col<column && row>=0)
{
if((*num)[row*column+col] == number)
{
found = true;
break;
}
if((*num)[row*column+col] < number)
++col;
else
--row;
}
}
return found;
}
int main()
{
int number;
int num[4][4] = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
puts("please input your number:");
scanf("%d",&number);
bool res = false;
res = Find(num,4,4,number);
printf("res:%d\n",res);
return 0;
}
對邏輯不了解的,可以随時留言,随時解答!!!
面試題4:
代碼如下:
#include<stdio.h>
void ReplaceBlank(char string[],int length)
{
if(string==NULL && length<=0)
return;
int originalLen = 0;//字元串實際長度
int numBlank=0,i=0;
while(string[i] != '\0')
{
++originalLen;
if(string[i] == ' ')
++numBlank;
++i;
}
//替換後的長度
int newLen = originalLen + numBlank*2;
if(newLen > length)
return;
int indexLen = originalLen;
int indexOfNew = newLen;
while(indexLen>=0 && indexOfNew>indexLen)
{
if(string[indexLen] == ' ')
{
string[indexOfNew--]='0';
string[indexOfNew--]='2';
string[indexOfNew--]='%';
}
else
{
string[indexOfNew--] = string[indexLen];
}
--indexLen;
}
}
int main()
{
char buf[]={"hello world!"};
ReplaceBlank(buf,20);
printf("buf:%s\n",buf);
return 0;
}
注:重點了解從尾到頭周遊的時間複雜度為O(n)!!!
面試題5:
#include<stdio.h>
#include<stdlib.h>
typedef int data_t;
//棧結構
typedef struct LIFO
{
int *data; // 4位元組的指針 指向數組(後配置設定的數組)
int maxlen; // 存儲資料的個數最大值
int top;
}seqstack_t;
//連結清單結構
typedef struct node{
int data;
struct node *next;
}linknode_t,* linklist_t;
//建立空的連結清單
linknode_t *CreateLink()
{
linknode_t *H;
H = (linklist_t)malloc(sizeof(linknode_t));
H->next = NULL;
return H;
}
//初始化一個空連結清單
void InitLink(linknode_t *H)
{
linknode_t *r,*p;//r指向隊尾
r = H;
int i=0;
for(;i<5;i++)
{
p = (linklist_t)malloc(sizeof(linknode_t));
p->data = i+1;
p->next = NULL;
r->next = p;
r = p;//再将r指向隊尾
}
}
//列印所有節點
linknode_t * ShowLink(linknode_t *H,seqstack_t *T)
{
linknode_t *p,*r;
int temp=0;
p = H->next;
while(p != NULL)
{
T->data[T->top]=p->data;
p = p->next;
T->top++;
}
--T->top;
while(T->top>=0)
{
printf("data:%-5d",T->data[T->top]);
T->top--;
}
}
//清空連結清單
void ClearLink(linknode_t *H)
{
linknode_t *p,*q;//p是删除的節點,q記錄下一個要删除的節點
q = H->next;
while(q != NULL)
{
p = q;
q = p->next;
free(p);
}
printf("\n");
H->next = NULL;
}
seqstack_t * CreateStack(int max)
{
seqstack_t *H; // 配置設定結構體空間 --> 一個棧
H = (seqstack_t *)malloc(sizeof(seqstack_t));
H->data = (data_t *)malloc(sizeof(data_t) * max);// 結構體成員指派
H->maxlen = max;
H->top = 0;
return H;
}
int main()
{
seqstack_t *T=CreateStack(10); // 擷取一個空的棧
linknode_t *H=CreateLink();
InitLink(H);
ShowLink(H,T);
ClearLink(H);
free(H);
return 0;
}
注:用棧的後進先出LIFO思想實作連結清單的從尾到頭的輸出!!!
未完待續......
作者:
柳德維出處:
https://www.cnblogs.com/liudw-0215/-------------------------------------------
個性簽名:獨學而無友,則孤陋而寡聞。做一個靈魂有趣的人!
如果覺得這篇文章對你有小小的幫助的話,記得在右下角點個“推薦”哦,部落客在此感謝!
萬水千山總是情,打賞一分行不行,是以如果你心情還比較高興,也是可以掃碼打賞部落客,哈哈哈(っ•̀ω•́)っ⁾⁾!