CC150(Data Structures)

1.Arrays and Strings

1.1

bool isUniqueChars(string s)
{
    if(s.length()>256)return false;
    vector<bool> vec(256,false);
    for(int i=0;i<s.length();++i)
    {
        if(vec[int(s[i])])return false;
        vec[int(s[i])]=true;
    }
    return true;
}

1.2

void reverse(char *s)
{
    if(!s)return;
    char *t=s;
    while(*t)++t;
    --t;
    while(s<t)
    {
        *s^=*t;
        *t^=*s;
        *s++^=*t--;
    }
}

1.3

bool permutation(string s,string t)
{
    if(s.length()!=t.length())return false;
    vector<int> vec(256,0);
    for(int i=0;i<s.length();++i)++vec[int(s[i])];
    for(int i=0;i<t.length();++i)
    {
        if(--vec[int(t[i])]<0)return false;
    }
    return true;
}

1.4

void  replaceSpaces(char *s,int n)
{
    if(!s||n<=0)return;
    int cnt=0;
    for(int i=0;i<n;++i)
    {
        if(s[i]==' ')++cnt;
    }
    int m=n+cnt*2;
    s[m--]='\0';
    for(int i=n-1;i>=0;--i)
    {
        if(s[i]!=' ')s[m--]=s[i];
        else
        {
            s[m--]='0';
            s[m--]='2';
            s[m--]='%';
        }
    }
}

1.5

string compress(string s)
{
    if(s.length()<=2)return s;
    int i=0,j=0;
    string t;
    while(j<s.length())
    {
        while(j<s.length()&&s[j]==s[i])++j;
        t+=s[i];
        t+='0'+j-i;
        i=j;
    }
    if(t.length()<s.length())return t;
 return s;
}

1.6

void rotate(vector<vector<int> > &matrix)
{
    int n=matrix.size();
    for(int i=0;i<n/2;++i)
    {
        for(int j=i;j<n-1-i;++j)
        {
            int tmp=matrix[i][j];
            matrix[i][j]=matrix[n-1-i-(j-i)][i];
            matrix[n-1-i-(j-i)][i]=matrix[n-1-i][n-1-i-(j-i)];
            matrix[n-1-i][n-1-i-(j-i)]=matrix[i+(j-i)][n-1-i];
            matrix[i+(j-i)][n-1-i]=tmp;
        }
    }
}

1.7

void setZeros(vector<vector<int> > &matrix)
{
    vector<bool> row(matrix.size(),false),col(matrix[0].size(),false);
    for(int i=0;i<matrix.size();++i)
    {
        for(int j=0;j<matrix[0].size();++j)
        {
            if(matrix[i][j]==0)row[i]=col[j]=true;
        }
    }
    for(int i=0;i<matrix.size();++i)
    {
        for(int j=0;j<matrix[0].size();++j)
        {
            if(row[i]||col[j])matrix[i][j]=0;
        }
    }
}

1.8

bool isRotation(string s1,string s2)
{
    if(s1.size()!=s2.size())return false;
    s1+=s1;
    return isSubstring(s1,s2);
}

2.Linked Lists

2.1

node *deleteDups(node *head)
{
    node dummy(-1);
    dummy.next=head;
    node *prev=&dummy,*cur=head;
    unordered_set<int> set;
    while(cur)
    {
        if(set.find(cur->value)==set.end())
        {
            set.insert(cur->value);
            prev=cur;
            cur=cur->next;
        }
        else
        {
            prev->next=cur->next;
            delete cur;
            cur=prev->next;
        }
    }
    return dummy.next;
}

2.2

node *kthToLast(node *head,int k)
{
    node *p=head,*q=head;
    while(q&&--k)q=q->next;
    if(!q)return NULL;
    while(q->next)
    {
        p=p->next;
        q=q->next;
    }
    return p;
}

2.3

bool deleteNode(node *p)
{
    if(!p||!p->next)return false;
    node *tmp=p->next;
    p->value=tmp->value;
    p->next=tmp->next;
    delete tmp;
    return true;
}

2.4

node *partition(node *head,int x)
{
    node dummy1(-1),dummy2(-1);
    node *p=&dummy1,*q=&dummy2;
    for(node *cur=head;cur;cur=cur->next)
    {
        if(cur->value<x)
        {
            p->next=cur;
            p=cur;
        }
        else 
        {
            q->next=cur;
            q=cur;
        }
    }
    p->next=dummy2.next;
    if(q->next)q->next=NULL;
    return dummy1.next;
}

2.5

node *addLists(node *l1,node *l2)
{
    node dummy(-1);
    node *prev=&dummy;
    int carry=0;
    for(;l1||l2;l1=l1?l1->next:NULL,l2=l2?l2->next:NULL,prev=prev->next)
    {
        int ai=l1?l1->value:0,bi=l2?l2->value:0;
        prev->next=new node((ai+bi+carry)%10);
        carry=(ai+bi+carry)/10;
    }
    if(carry)prev->next=new node(carry);
    return dummy.next;
}

你可能感兴趣的:(CC150(Data Structures))