【代码随想录】3

哈希表篇

有效的字母异位词

bool isAnagram(char* s, char* t) {
   
int hash[26]={
   0};

for(int i=0;i<strlen(s);i++)
hash[s[i]-'a']++;
for(int i=0;i<strlen(t);i++)
hash[t[i]-'a']--;
for(int i=0;i<26;i++)
if(hash[i]!=0)return false;
return true;

    
}

两个数组的交集

int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
   
    int hash[1002]={
   0};
    int k=0;
    *returnSize=nums1Size>nums2Size?nums1Size:nums2Size;
    int*a=(int*)malloc(*returnSize*sizeof(int));
    for(int i=0;i<nums1Size;i++)
    hash[nums1[i]]++;
    for(int i=0;i<nums2Size;i++)
    {
   if(hash[nums2[i]]>0)
   {
    a[k++]=nums2[i];
    hash[nums2[i]]=0;}
       

 }

*returnSize=k;
return a;







}

字符串篇

翻转字符串

void swap(char *q,char *t)
{
   char tmp=*q;
*q=*t;
*t=tmp;



}

void reverseString(char* s, int sSize) {
   
int left=0;
int right=sSize-1;
while(left<right)
{
   swap(&s[left],&s[right]);
left++;
right--;










}








    
}

翻转字符串2

void reverse(char*t,char*p)
{
   char tmp=*t;
*t=*p;
*p=tmp;}


char* reverseStr(char* s, int k) {
   
int size=strlen(s);
for(int i=0;i<size;i+=2*k)
{
   if(i+k<=size)
{
   int left=i;
int right=i+k-1;
    while(left<=right)
    {
    reverse(&s[left],&s[right]);
      left++;
      right--;
    }
    continue;}
int left=i;
int right=size-1;
    while(left<=right)
    {
    reverse(&s[left],&s[right]);
      left++;
      right--;
    }
}
return s;
}

翻转字典中的单词

void reverse(char*s,int left,int right)
{
   while(left<=right)
{
   char tmp=s[left];
   s[left]=s[right];
   s[right]=tmp;
   left++;
   right--;
}
}
void removeExtraSpace(char* s)
{
   int start=0;
int end=strlen(s)-1;
int count=0;
while(s[start]==' ')start++;
while(s[end]==' ')end--;
for(int i=start;i<=end;i++)
{
   if(s[i]==' '&&s[i+1]==' ')
  continue;

s[count++]=s[i];
}

s[count]='\0';
}
char* reverseWords(char* s) {
   
   removeExtraSpace(s);
    reverse(s,0,strlen(s)-1);
   int slow=0;
   for(int i=0;i<=strlen(s);i++)
   {
   if(s[i]==' '||s[i]=='\0')
    {
    reverse(s,slow,i-1);
     slow=i+1;}




   }
return s;

}

重复的子字符串
kmp法

void getnext(int*next,char*s)
{
   next[0]=0;
int j=0;
for(int i=1;i<strlen(s);i++)
{
   while(j>0&&s[i]!=s[j])
   j=next[j-1];
   if(s[i]==s[j])
   j++;
   next[i]=j;
   
}

}
bool repeatedSubstringPattern(char* s) 
{
   int len=strlen(s);
    if(len==0)return false;
    int*next=(int*)malloc(sizeof(int)*len);
    getnext(next,s);
   if(next[len-1]!=0&&len%(len-next[len-1])==0)
   return true;

   return false;
    
}

栈与队列篇

用栈实现队列



typedef struct Stack//定义一个栈的结构体变量	
{
   
	int * a;
	int top; // 栈顶
	int capacity; // 容量
}Stack;
void StackInit(Stack* ps)
{
   
	assert(ps);//断言,防止为空指针
	ps->a = NULL;//所指向的地址为空
	ps->capacity = ps->top = 0;//容量和栈中元素个数均为0
}
void StackPush(Stack* ps, int data)
{
   
	assert(ps);
	if (ps->capacity == ps->top)//如果栈中的元素个数等于栈的容量时考虑扩容,
	{
   
		int newcapcity = ps->capacity == 0 ? 4 : ps->capacity * 2;//如果刚开始时都等于0,就先给4个空间大小,后面如果满的话,容量扩大1倍
		 int* newnode = (int*)realloc(ps->a,sizeof(int)* newcapcity);//申请空间,将申请好的空间首地址传给newnode指针
		 assert(newnode);//断言,防止malloc失败
		 ps->a = newnode;//将newnode保存的申请空间的首地址传给ps->a,让ps->a指向创建好的空间
		ps->capacity = newcapcity;//容量大小更新为新容量大小



	}
	ps->a[ps->top] = data;//像存数组一样存数据
	ps->top++;//指向下一个
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
   
	assert(ps);
	return ps->top ==0;//ps->top为栈中元素个数.==0栈中无元素,无元素要返回1, 无元素ps->t0p==0,这个表达式结果是1,返回1;





}
// 出栈
void StackPop(Stack* ps)
{
   
	assert(ps);
	assert(!StackEmpty(ps));//防止栈内无元素,继续出栈
	ps->top--;
}
// 获取栈顶元素
int StackTop(Stack* ps)
{
   
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];//ps->top为栈中元素个数,由于数组下标是从0开始,所以栈顶元素下标为ps->top-1;

}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
   
	assert(ps);
	return ps->top;

}
// 销毁栈
void StackDestroy(Stack* ps)
{
   
	assert(ps);
	free(ps->a);//free掉动态申请的内存
	ps->a = NULL;//防止野指针
	ps->capacity = ps->top = 0;//容量和栈中元素个数置为0



}



typedef struct {
   
Stack popst;
Stack pushst;
    
} MyQueue;


MyQueue* myQueueCreate() {
   
MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
if(obj==NULL)
perror("malloic fail");
StackInit(&obj->popst);
StackInit(&obj->pushst);
return obj;

    
}
bool myQueueEmpty(MyQueue* obj) {
   
    return StackEmpty(&obj->popst)&&StackEmpty(&obj->pushst);
    
}

void myQueuePush(MyQueue* obj, int x) {
   
    StackPush(&obj->pushst,x);
    
}

int myQueuePop(MyQueue* obj) 
{
   
    if(StackEmpty(&obj->popst))
    {
   while(StackSize(&obj->pushst))
        {
   StackPush(&obj->popst, StackTop(&obj->pushst));
          StackPop(&obj->pushst);
       }
       }
       int ret=StackTop(&obj->popst);
        StackPop(&obj->popst);
        return ret;








    
    
}

int myQueuePeek(MyQueue* obj) {
   
       if(StackEmpty(&obj->popst))
    {
   while(StackSize(&obj->pushst))
        {
   StackPush(&obj->popst, StackTop(&obj->pushst));
          StackPop(&obj->pushst);
       }
    }
       int ret=StackTop(&obj->popst);
       // StackPop(&obj->popst);
        return ret;







    
}



void myQueueFree(MyQueue* obj) {
   
    StackDestroy(&obj->popst);
    StackDestroy(&obj->pushst);
    free(obj);
    
}

用队列实现栈




typedef struct QListNode
{
   
	struct QListNode* next;
	int data;
}QNode;

typedef struct Queue
{
   
	QNode* front;
	QNode* tail;
}Queue;
void QueueInit(Queue* q)
{
   
	assert(q);
	q->front = q->tail = NULL;

}
// 队尾入队列
void QueuePush(Queue* q, int x)
{
   
	assert(q);
	
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);
	newnode->data =x;
	newnode->next = NULL;
	if (q->tail == NULL)
	{
   
		q->tail = q->front = newnode;


	}
	else
	{
   
		q->tail->next = newnode;
		q->tail = newnode;
		



	}
	







}
bool QueueEmpty(Queue* q)
{
   
	assert(q);
	return q->front == NULL;



}

// 队头出队列
void QueuePop(Queue* q)
{
   
	assert(q);
	assert(!QueueEmpty(q));
	if (q->front->next == NULL)
	{
   
		free(q->tail);
		q->tail = q->front = NULL;



	}
	else
	{
   
		QNode* next = q->front->next;
		free(q->front);
		q->front = next;




	}

}
// 获取队列头部元素
int QueueFront(Queue* q)
{
   
	assert(q);
	assert(q->front);
	return q->front->data;


}
// 获取队列队尾元素
int QueueBack(Queue* q)
{
   
	assert(q);
	assert(q->tail);
	return q->tail->data;



}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
   
	assert(q);
	int size = 0;

	QNode* cur = q->front;
	while (cur)
	{
   
		size++;
		cur = cur->next;
	}
	return size;




}
// 销毁队列
void QueueDestroy(Queue* q)
{
   
	assert(q);
	QNode* cur = q->front;
	while (cur)
	{
   
		QNode* next = cur->next;
		free(cur);
		cur = next;





	}
	q->front = q->tail = NULL;




}




typedef struct {
   
    Queue n1;
    Queue n2;

    
} MyStack;


MyStack* myStackCreate() {
   
    MyStack*obj=(MyStack*)malloc(sizeof(MyStack));
   if(obj==NULL)
   perror("malloc fail");
   QueueInit(&obj->n1);
   QueueInit(&obj->n2);
   return obj;
    
}

void myStackPush(MyStack* obj, int x) {
   
    if(!QueueEmpty(&obj->n1))
    {
   QueuePush(&obj->n1,x);


        
    }
    else
    {
   QueuePush(&obj->n2,x);


    }
}

int myStackPop(MyStack* obj) {
   
Queue*nonempty=&obj->n1;
Queue*empty=&obj->n2;  
if(!QueueEmpty(&obj->n2)) 
{
   nonempty=&obj->n2;
  empty=&obj->n1; 


}
while(QueueSize(nonempty)>1)
{
   QueuePush(empty,QueueFront(nonempty));
      QueuePop(nonempty);


}
int ret=QueueBack(nonempty);
 QueuePop(nonempty);
 return ret;





}







    


int myStackTop(MyStack* obj) {
   
    if(!QueueEmpty(&obj->n1))
    {
   return QueueBack(&obj->n1);


    }
    else
    {
   return QueueBack(&obj->n2);


    }
    
}

bool myStackEmpty(MyStack* obj) {
   
    return  QueueEmpty(&obj->n1)&&QueueEmpty(&obj->n2);
    
}

void myStackFree(MyStack* obj) {
   
   QueueDestroy(&obj->n1);
    QueueDestroy(&obj->n2);
    free(obj);  
    
}

有效的括号

typedef struct Stack
{
   
	char* a;
	int top; // 栈顶
	int capacity; // 容量
}Stack;
void StackInit(Stack* ps)
{
   
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void StackPush(Stack* ps, int data)
{
   
	assert(ps);
	if (ps->capacity == ps->top)
	{
   
		int newcapcity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		char* newnode = (char*)realloc(ps->a,sizeof(char) * newcapcity);
		assert(newnode);
		ps->a = newnode;
		ps->capacity = newcapcity;



	}
	ps->a[ps->top] = data;
	ps->top++;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
   
	assert(ps);
	return ps->top == 0;





}
// 出栈
void StackPop(Stack* ps)
{
   
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}
// 获取栈顶元素
char StackTop(Stack* ps)
{
   
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];

}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
   
	assert(ps);
	return ps->top;

}
// 销毁栈
void StackDestroy(Stack* ps)
{
   
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;



}

bool isValid(char* s) {
   
Stack st;
StackInit(&st);
while(*s)
{
   if(*s=='{'||*s=='['||*s=='(')
{
    StackPush(&st,*s);
s++;


}
else
{
   if(StackEmpty(&st))
return false;
    

else
{
   char top=StackTop(&st);
    if(top=='{'&&*s=='}'||top=='['&&*s==']'||top=='('&&*s==')')
{
   StackPop(&st);
s++;
}
else
return false;
}






}










}









int ret=StackEmpty(&st);
return ret;
 StackDestroy(&st);  
}

相关推荐

  1. 代码随想3

    2024-01-17 03:42:02       48 阅读
  2. 代码随想 字符串

    2024-01-17 03:42:02       61 阅读
  3. 代码随想 -- 数组

    2024-01-17 03:42:02       51 阅读
  4. 代码随想

    2024-01-17 03:42:02       32 阅读
  5. 代码随想

    2024-01-17 03:42:02       30 阅读
  6. 代码随想【字符串】

    2024-01-17 03:42:02       30 阅读
  7. 代码随想】栈

    2024-01-17 03:42:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 03:42:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 03:42:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 03:42:02       87 阅读
  4. Python语言-面向对象

    2024-01-17 03:42:02       96 阅读

热门阅读

  1. Android mk文件

    2024-01-17 03:42:02       49 阅读
  2. linux 空间莫名消失

    2024-01-17 03:42:02       52 阅读
  3. 英语连读技巧6

    2024-01-17 03:42:02       47 阅读
  4. 图机器学习年度汇集

    2024-01-17 03:42:02       55 阅读
  5. 【100条git命令】

    2024-01-17 03:42:02       55 阅读