Charles
BAN USERint Count(char *t, char *s)
{
int a [26]={};
int b=0;
for(int i=0;i<strlen(s);i++)
{
if(a[*(s+i)-'a']==0)
{
a[*(s+i)-'a']=1;
b++;
}
}
for(int i=0;i<strlen(t);i++)
{
if(a[*(t+i)-'a']>=1)
{
a[*(t+i)-'a']++;
}
}
int count=0;
for(int i=0;i<26;i++)
{
count=count+a[i];
}
return count-b;
}
- Charles August 02, 2014int count1s(int num)
{
char pat[16]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
int cnt=0;
char t= num&0xf;
cnt=cnt+pat[t];
t=(num&0xf0)>>4;
cnt=cnt+pat[t];
t=(num&0xf00)>>8;
cnt=cnt+pat[t];
t=(num&0xf000)>>12;
cnt=cnt+pat[t];
t=(num&0xf0000)>>16;
cnt=cnt+pat[t];
t=(num&0xf00000)>>20;
cnt=cnt+pat[t];
t=(num&0xf000000)>>24;
cnt=cnt+pat[t];
t=(num&0xf0000000)>>28;
cnt=cnt+pat[t];
return cnt;
}
HANDLE p,v;
void odd()
{
int a =0;
while(a<100)
{
WaitForSingleObject(p,-1);
printf("%d ",a);
a=a+2;
ReleaseSemaphore( v,1,NULL);
}
}
void even()
{
int a =1;
while(a<100)
{
WaitForSingleObject(v,-1);
printf("%d ",a);
a=a+2;
ReleaseSemaphore( p,1,NULL);
}
}
void twothread()
{
int i=0;
p = CreateSemaphore(
NULL, // default security attributes
1, // initial count
1, // maximum count
NULL); // unnamed semaphore
v = CreateSemaphore(
NULL, // default security attributes
0, // initial count
1, // maximum count
NULL); // unnamed semaphore
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&odd,(LPVOID) &i,0,NULL);
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&even,(LPVOID) &i,0,NULL);
while(1);
}
this is the correct version, this version can achieve O(n) time with O(n) space:
1) given an array a
2) from left to right, a[i]+=a[i-1]; which is 3,0,8,14,13,8
3) generated an array b, memcpy(b,a,sizeof(a))
4) from right to left ,b[i-1]+=b[i]; which is 8,5,8,0,-6,-5
5) find the place '8', which is a[i] and b[i](i==2), if a[i-1]==b[i+1] then return 1
6) else return 0
you are both wrong, you want to consider both horizonal and vertical;
bool doesRectOverlap(rect ra, rect rb)
{
bool ret=0;
ret|=(ra.topx<rb.topx)&&(ra.botx>rb.topx)&&(ra.topy<rb.topy)&&(ra.boty>rb.topy);
ret|=(ra.topx<rb.topx)&&(ra.botx>rb.topx)&&(ra.topy<rb.boty)&&(ra.boty>rb.boty);
ret|=(ra.topx<rb.botx)&&(ra.botx>rb.botx)&&(ra.topy<rb.topy)&&(ra.boty>rb.topy);
ret|=(ra.topx<rb.botx)&&(ra.botx>rb.botx)&&(ra.topy<rb.boty)&&(ra.boty>rb.boty);
return ret;
}
same2(concatinate(1,2)) --->same1(concatinate(1,2)) ---->same1((1##2)) ->#(12)->"12";
same1(concatinate(1,2))--->#(concatinate(1,2))->"concatinate(1,2)"
Note after “#”“##” is scanned , no macro can be treated as macro, they will be treated as string.
It looks to me like Macro is from left to right BFS instread of DFS
I made a version based on binary tree, you can tweak it to tree of any children size;
int FindHeight(bstree root)
{
int left,right;
if (root==NULL) return 0;
else
{
left=FindHeight(root->lc)+1;
right=FindHeight(root->rc)+1;
if(left>right)
return left;
else return right;
}
}
int FindWidth(bstree root,int level)
{
if(root==NULL) return 0;
if(level==1) return 1;
return FindWidth(root->lc,level-1)+FindWidth(root->rc,level-1);
}
int FindWidthMax(bstree root)
{
int max=0;
int depth=FindHeight(root);
int width;
for(int i=0;i<depth;i++)
{
width=FindWidth(root,i+1);
max=(max>width?max:width);
}
return max;
}
I have to give you -1. you didn't consider negative value:
void dectobin(int x,int count)
{
unsigned a;
if(x<0)
{
a=0xffffffff+x+1;
}
if((x==0)&&(count==0))
{
printf("%d",x);
return;
}
else
{
a=x;
}
if(a>0)
dectobin(a>>1,count+1);
if(a!=0)
printf("%d",a%2);
return ;
}
Use recursive and hash table.
recursive to the end of the linklist, fill all the hash table with the count of each index in the linklist, then back tracking ,during backtracking ,
1) for each element, mark the element on the hash table to be negative ,
2)if you seen a index with a value of negative number, then remove it
-----this is based on the understanding that one node will have only one duplicated at most ;
also I assume the value in linklist is in a limited range
this is a good way , to use sprintf,
here is another way, without sprintf:
I haven't consider negative number , only decimal float are considerred .
void myftoa(float x)
{
int i=x;
int j;
float m=x-(float)i;
char CInt[18]={0}; // biggest integer decimal would be 10 digital, we need another 1 for'.', 1 for 0
for( j=0;j<10;j++)
{
if(i/10!=0)
{
CInt[10-j-1]=i%10+'0';
i=i/10;
}
else
{
if(i%10!=0)
{
CInt[10-j-1]=i%10+'0';
}
break;
}
}
char *result=&(CInt[10-j-1]);
char Cmas[7]={0}; // only have to handle value bigger than 0.000001
for(int i=0;i<6;i++)
{
Cmas[i]=(int)(m*10)+'0';
m=m*10-(int)(m*10);
}
char *p=".";
result=strcat(result,p);
result=strcat(result,Cmas);
printf("%s", result);
}
- Charles December 31, 20131) malloc return a pointer which is a virutal address, not physical, so MMU will not even do swap for this returned pointer back, therefore very efficient
2) calloc return a pointer which is also a virtual address, but calloc do zero out the physical memory , therefore page in/out will happen and thus slow down the speed
why do we need sleep in the kernel?
Kernel is like a machine which run everything , if kernel sleep , other thread won't get a chance to run , so the whole system will stop , unless you want to process interrupt.
I guess if in kernel you want this thread to sleep, you can do a context switch to let the current thread sleep, then you can let other thread run.
if you really want to sleep in kernel, you can disable interrupt, so that the scheduler won't have a chance to work , you will be able to sleep as long as you want with a while loop.
no need to keep from pointer , waste register.
what if abs(dest-src)<strlen(src)?
what if *dest is NULL?
char * mystrcpy(char * t, const char *s)
{
int size=strlen(s);
char *t1=t;
if(t==NULL) return NULL;
if((t-s<=strlen(s))||(s-t<=strlen(s))) return NULL;
while(*t++=*s++);
return t1;
}
Say we don't have pad, then say c start at address 0, a will start at address 1 ,d will start at address 5, so when we want to read a, we need read an integer from address 0 first for the _first_ 3 bytes of a, then we need read from address 4 to read the last byte;
Datatype misallignment error might happen due to this.
bool mysubstr(char* t,const char *s, unsigned int start, unsigned int len)
{
unsigned int strsize=strlen(s);
if(start>strsize) return 0;
if(len>strsize) return 0;
if(start+len>strsize) return 0
for(int i=start;i<start+len;i++)
{
t[i-start]=s[i];
}
return 1;
}
void * Allignedmalloc(int size, int allsize)
{
int totalsize=size+allsize-1;
void *p1=(void*)malloc(totalsize);
void **p2;
if(p1==NULL) return NULL;
p2=(void**)(((int)p1+allsize-1)&(~(allsize-1)));
p2[-1]=p1;
return (void*)p2;
}
void Allignfree(void* p)
{
free((((void**)p)[-1]));
}
This is mine , which is faster? a or b ?
This can be asked to cache performance issue
int **allocate(int row,int col)
{
int b[2][3]={};
int **a=(int**)malloc(row*sizeof(int*));
for(int i=0;i<row;i++)
{
a[i]=(int*)malloc(sizeof(int)*col);
}
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
a[i][j]=i;
}
return a;
}
- Charles December 30, 2013I guess this is what they need?
bool IncIntChar(char*a,int s)
{
bool flag=0;
if(*(a+1)!=0)
{
flag=IncIntChar(a+1,s);
}
if(strlen(a)<s)
{
if(strlen(a)==1)
{
if(*a=='9') {
*a= '0';
flag=1;
}
else *a=*a+1;
}
else
{
if(flag)
{
if(*a=='9') {
*a= '0';
flag=1;
}
else
{
*a=*a+1;
flag=0;
}
}
}
}
else
{
if(flag==1) *a='1';
else *a='0';
printf("%s\n",a);
}
return flag;
}
I got say it is difficult than I first thought
char* binaryadd()
{
char *s="10001";
char *t="101";
char *p=NULL;
char *q=NULL;
int sizea=strlen(s);
int sizeb=strlen(t);
int small=sizea>sizeb?sizeb:sizea;
int big=sizea>sizeb?sizea:sizeb;
if(big==sizea)
{
p=s;
s=s+sizea-sizeb;
}
else
{
p=t;
t=t+sizeb-sizea;
}
char *r=(char*)malloc(big+2);
q=r;
r=r+1;
memset(q,0,big+2);
bool flag=0;
int i;
for( i=small-1;i>=0;i--)
{
if(((*(s+i))==(*(t+i)))&&((*(t+i)=='1'))&&flag)
{
*(r+i+big-small)='1';
flag=1;
}
else if(((*(s+i))==(*(t+i)))&&((*(t+i)=='0'))&&flag)
{
*(r+i+big-small)='1';
flag=0;
}
else if(((*(s+i))==(*(t+i)))&&((*(t+i)=='1'))&&!flag)
{
*(r+i+big-small)='0';
flag=1;
}
else if(((*(s+i))==(*(t+i)))&&((*(t+i)=='0'))&&!flag)
{
*(r+i+big-small)='0';
flag=0;
}
else if(((*(s+i))!=(*(t+i)))&&flag)
{
*(r+i+big-small)='0';
flag=1;
}
else if(((*(s+i))!=(*(t+i)))&&!flag)
{
*(r+i+big-small)='1';
flag=0;
}
}
if(sizea==sizeb)
{
if(flag==1) *(r+i)='1';
else q=r;
}
else
{
for(int j=big-small-1;j>=0;j--)
{
if(flag==0)
{
*(r+j)=*(p+j);
}
else
{
if(*(p+j)=='0')
{
*(r+j)='1';
flag=0;
}
else
{
*(r+j)='0';
flag=1;
}
}
}
}
if(flag==1)
{
*q='1';
}
else
{
q=q+1;
}
return p;
}
I guess this is what he exepct ?
typedef struct node
{
int data;
struct node *child[N];
struct node *next
} *ptree;
ptree TraverNtree(ptree root)
{
ptree q;
for(int i=0;i<N;i++)
{
if(root->child[i]==NULL)
{
continue;
}
else
{
if (root->next==NULL) root->next=TraverNtree(root->child[i]));
else
{
q=root->next;
root->next=TraverNtree(root->child[i]));
root->next->next=q;
}
}
}
return root;
}
Linklist Merge2Lists(Linklist s,Linklist t)
{
Linklist head=NULL,tail=NULL,p1=s,p2=t;
if((s==NULL)&&(t==NULL)) return NULL;
if(s==NULL) return t;
if(t==NULL) return s;
while((p1!=NULL)&&(p2!=NULL))
{
if(p1->data>p2->data)
{
if(head==NULL) head=p2;
if (tail==NULL) tail=p2;
else tail->next=p2;
tail=p2;
p2=p2->next;
if(head==NULL) head=p2;
}
else
{
if(head==NULL) head=p1;
if (tail==NULL) tail=p1;
else tail->next=p1;
tail=p1;
p1=p1->next;
}
}
if(p1!=NULL)
{
tail->next=p1;
}
else
{
tail->next=p2;
}
return head;
}
typedef struct bin{
unsigned char a1:4;
unsigned char a2:4;
} hex;
#pragma pack(1)
typedef struct {
hex d[6];
} max;
void Macasctohex()
{
char mac[]="12:34:56:ab:cd:ef";
max opt;
int j=0;
memset(&opt,0,sizeof(opt));
for(int i=0;i<sizeof(mac)/sizeof(mac[0]);i+=3)
{
opt.d[j].a1=((mac[i+1]>='a')?(mac[i+1]-'a'+10):(mac[i+1]-'0'));
opt.d[j++].a2=((mac[i]>='a')?(mac[i]-'a'+10):(mac[i]-'0'));
}
}
}
- Charles August 03, 2014