目录
回文数
版本一
#include <stdlib.h>
#include <string.h>
void reverse(char* p)
{
int left=0,right=strlen(p)-1;
while(left<right)
{
char tmp=*(p+left);
*(p+left)=*(p+right);
*(p+right)=tmp;
left++;
right--;
}
}
bool isPalindrome(int x){
char arr[24];
sprintf(arr,"%d",x);
reverse(arr);
if(atoi(arr)==x)
return 1;
else
return 0;
}
版本二:示例代码
bool isPalindrome(int x){
if(x<0){
return false;
}
else{
long i=x;
long sum=0;
while(x){
int j=x%10;
sum=sum*10+j;
x=x/10;
}
if(i==sum){
return true;
}
return false;}
}
罗马数字转整数
int GetNum2(char* str)
{
int ret=0;
if(*str=='I')
ret=1;
else if(*str=='V')
ret=5;
else if(*str=='X')
ret=10;
else if(*str=='L')
ret=50;
else if(*str=='C')
ret=100;
else if(*str=='D')
ret=500;
else if(*str=='M')
ret=1000;
return ret;
}
int Compare(char* str1,char* str2)
{
int a=GetNum2(str1),b=GetNum2(str2);
return a-b;
}
// int GetNum1(char c1,char c2)
// {
// if(c1=='V')
// return 4;
// else
// return 9;
// }
int romanToInt(char * s){
int sum=0;
while(*s)
{
if(Compare(s+1,s)>0)
{
sum+=Compare(s+1,s);
s+=2;
}
else
{
sum+=GetNum2(s);
s++;
}
}
return sum;
}
最长公共前缀
未能通关版本
// void InitDst(char* p,int n)
// {
// for(int i=0;i<n;i++)
// {
// p[i]=0;
// }
// }
char * longestCommonPrefix(char** strs, int strsSize){
// char dst[200]={0};
// InitDst(dst,200);
int i=0,j=0;
int k=0;
end2:
for(i=1;i<strsSize;i++)
{
if(strs[i][j]!=strs[i-1][j])//这里的j可能会越界
goto end1;
}
j++;
if(strs[0][j]!='\0')
goto end2;
end1:
for(k=0;k<j;k++)
{
;
}
strs[0][k]='\0';
return strs[0];
}
示例代码
char * longestCommonPrefix(char ** strs, int strsSize){
int p=strlen(strs[0]);
int i,j;
for(i=0;i<strsSize;i++)
{
if(p>=strlen(strs[i]))
{
p=strlen(strs[i]);
}
}
for(j=0;j<p;j++)
{
for(i=1;i<strsSize;i++)
{
if(strs[0][j]!=strs[i][j])
{
break;
}
}
if(i!=strsSize)
{
break;
}
}
strs[0][j]='\0';
return strs[0];
}
有效的括号
法一:入栈出栈(队列思想)
int IsValid(char* p1,char* p2)
{
if((*p1=='('&&*p2==')')||(*p1=='['&&*p2==']')||(*p1=='{'&&*p2=='}'))
return 1;
else
return 0;
}
bool isValid(char * s){
char stack[10000]={0};
int i=0;
int len=1;
while(*s)
{
if(*s=='('||*s=='{'||*s=='[')
{
stack[i++]=*s;//入栈
len=strlen(stack);
}
else
{
if(stack[0]==0)
return false;
if(IsValid(&stack[len-1],s))
{
stack[--i]=0;
len=strlen(stack);
}
else
return false;
}
s++;
}
if(stack[0]==0)
return true;
else
return false;
}
搜索插入位置
int searchInsert(int* nums, int numsSize, int target){
int left=0;
int right=numsSize-1;
int mid=(left+right)/2;
while(left<=right)
{
mid=(left+right)/2;
if(nums[mid]<target)
{
left=mid+1;
}
else if(nums[mid]==target)
return mid;
else
{
right=mid-1;
}
}
return left;
}
最后一个单词的长度
int lengthOfLastWord(char * s){
int count=0;
while(*s)
{
if(*s!=' ')
count++;
if(*(s+1)!=' '&&*(s+1)!='\0'&&*s==' ')
count=0;
s++;
}
return count;
}
复制带随机指针的链表
Myway:
struct Node* SlCreatNewnode(int val)
{
struct Node* newnode=(struct Node*)malloc(sizeof(struct Node));
if(newnode==NULL)
{
perror("malloc fail");
}
newnode->next=NULL;
newnode->val=val;
return newnode;
}
void SlPushBack(struct Node** pphead,int val)
{
struct Node* newnode=SlCreatNewnode(val);
//1.空链表
if(*pphead==NULL)
{
*pphead=newnode;
}
// 2.非空情况
else
{
struct Node* tail=*pphead;
while(tail->next)//找尾结点
{
tail=tail->next;
}
tail->next=newnode;
}
}
struct Node* copyRandomList(struct Node* head) {
if(head==NULL)
{
return NULL;
}
struct Node* newhead=SlCreatNewnode(head->val),*cur=head;
struct Node* head2=newhead;
struct Node* tail1=head,*cur1=head,*tail2=head2,*cur2=head2;
cur=cur->next;
while(cur)//先创建新空间
{
SlPushBack(&newhead,cur->val);
cur=cur->next;
}
while(cur1)
{
tail1=head;//tail从头更新
tail2=head2;
while(cur1->random!=tail1)
{
tail1=tail1->next;
tail2=tail2->next;
}
cur2->random=tail2;
cur1=cur1->next;
cur2=cur2->next;
}
return newhead;
}
注意:
若出现说存在可能引用NULL的情况
一定要纵观全局,看看哪些地方可能存在引用NULL的情况
时间复杂度O(N)
struct Node* copyRandomList(struct Node* head){
if(head==NULL)
{
return NULL;
}
struct Node* cur=head;
while(cur)
{
struct Node* copy=(struct Node*)malloc(sizeof(struct Node));
struct Node* next=cur->next;
cur->next=copy;
copy->next=next;
copy->val=cur->val;
cur=next;
}
//1.进行copy的random连接
cur=head;
while(cur)
{
struct Node* curnext=cur->next;
if(cur->random==NULL)
{
curnext->random=NULL;
}
else
{
curnext->random=cur->random->next;
}
cur=curnext->next;
}
//2.将copy的next用尾插连接
struct Node* copyhead=NULL,*copytail=NULL;
cur=head;
while(cur)
{
struct Node* copy=cur->next;
struct Node* next=copy->next;
if(copytail==NULL)
{
copyhead=copytail=copy;
}
else
{
copytail->next=copy;
copytail=copytail->next;
}
//恢复原链表
cur=cur->next;
cur=next;
}
return copyhead;
}
加一
int* plusOne(int* digits, int digitsSize, int* returnSize){
int i=0;
int len=digitsSize;
int* Newnum=(int*)malloc(sizeof(int)*(len+1));
// memset(Newnum,0,sizeof(Newnum));//数组初始化为0
*Newnum=1;
*Newnum = 1;
for (i = 1; i < len + 1; i++)
{
Newnum[i] = 0;//数组初始化为0
}
digits[len-1]+=1;
for(i=len-1;i>=0;i--)
{
if(digits[0]>9)
{
*returnSize=len+1;
return Newnum;
}
if(digits[i]>9)
{
digits[i-1]+=1;
digits[i]=0;
}
}
*returnSize=len;
return digits;
}