leetcode隨筆V

  1. leetcode題目
    1.1題目描述
    1.2知識點及思路
    1.3代碼
  2. 總結

一.leetcode題目
1.You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.
For example:
Secret number: “1807”
Friend’s guess: “7810”
Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
題目描述:猜謎遊戲,找出位置對應相同的元素,記爲公牛;位置不同的元素,並且滿足在其他位置有出現(錯位),記爲母牛
知識點:hashTabel;交叉集
思路:①循環遍歷兩個數字對應的每一位,如果②位置相同,並且元素相同,則將bull++;否則③ 分別建立兩個hashTable用於存儲每個元素出現的次數(以出現的數字爲下標);④依次遍歷找出對應位置的出現次數最小者求和
代碼如下:

class Solution {
public:
    string getHint(string secret, string guess) {
        int n = secret.length();
        int a[10] = {0};
        int b[10] = {0};
        int numA = 0;
        int numB = 0;
        for (int i = 0; i < n; i++)
        {
            if (secret[i] == guess[i])
            {
                numA++;
            }
            else
            {
                a[secret[i]-'0']++;
                b[guess[i]-'0']++;
            }
        }
        for (int i = 0; i < 10; i++)
        {
            numB += min(a[i], b[i]);
        }
        char aStr[32];
        char bStr[32];
        sprintf(aStr, "%d", numA);
        sprintf(bStr, "%d", numB);
        return string(aStr) + "A" + string(bStr) + "B";  
    }
};

2.Intersection of Two Linked Lists
題目描述:找兩個鏈表的交點
知識點:鏈表查找;地址指針
思路:①統計兩個鏈表的長度num1,num2,並且②返回其尾結點,用於判斷是否有交叉,③長鏈表走abs(num1-num2),步數之後④兩個鏈表同時遍歷,判斷是否有相同結點
知識擴展:判斷鏈表迴文;判斷鏈表是否存在環
代碼如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 
    {
        int lenA=0,lenB=0;
        if(length(headA,lenA)!=length(headB,lenB))
            return NULL;
        ListNode*fast,*slow;
        if(lenA>=lenB)
        {
            fast=headA;
            slow=headB;
        }
        else
        {
            fast=headB;
            slow=headA;
        }
        int lenDiff=abs(lenA-lenB);
        while(lenDiff--)
            fast=fast->next;
        while(fast!=slow)
        {
            fast=fast->next;
            slow=slow->next;
        }
        return fast;
    }
    ListNode* length(ListNode*head,int &len)
    {
        ListNode*p=head;
        if(!head)
            return NULL;
        len++;
        while(p->next)
        {
            len++;
            p=p->next;
        }
        return p;
    }
};

3.Plus One
題目描述:用一個數組保存非負數,將其值加一
知識點:字符串求和;變量值保存更新
注意事項:①判斷結果滿10進位,保存flag位,並更新②最前一位的值可能有進位
代碼如下:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) 
    {
        int flag=0;
        digits[digits.size()-1]+=1;
        for(int i=digits.size()-1;i>=0;i--)
        {
            if(digits[i]+flag==10)
            {
                flag=1;
                digits[i]=0;
            }
            else
            {
                digits[i]=digits[i]+flag;
                flag=0;
            }
        }
        if(flag==1)
        {
            digits.resize(digits.size()+1,0);
            digits[0]=1;
        }
        return digits;
    }
};

4.Implement Stack using Queues
題目描述:用隊列實現棧
知識點:棧、隊列基本概念
思路:①創建兩個棧;②一個棧始終爲空,作爲輔助棧;③入棧操作進存在元素的非空棧;④出棧操作時,將非空棧size()>1的元素入空棧,將size()==1元素出棧
代碼如下:

class Stack {
private:
queue<int> queue1,queue2;   
public:
    // Push element x onto stack.
    void push(int x) 
    {
       if(queue1.empty()&&queue1.empty())
        queue1.push(x);
       else
       {
           if(!queue1.empty())
            queue1.push(x);
           else
            queue2.push(x);
       }
    }

    // Removes the element on top of the stack.
    void pop() 
    {
        if(queue1.empty())
        {
            while(queue2.size()>1)
            {
                int temp=queue2.front();
                queue2.pop();
                queue1.push(temp);
            }
            queue2.pop();
        }
        else
        {
            while(queue1.size()>1)
            {
                int temp=queue1.front();
                queue1.pop();
                queue2.push(temp);
            }
            queue1.pop();
        }
    }
    // Get the top element.
    int top() 
    {
       if(queue1.empty())
        return queue2.back();
        else
        return queue1.back();
    }

    // Return whether the stack is empty.
    bool empty() 
    {
        if(queue1.empty()&&queue2.empty())
            return true;
        return false;
    }
};

5.Pascal’s Triangle
題目描述:打印楊輝三角
知識點:楊輝三角特性
思路:①temp[j]=temp[i]+temp[i+1],同時②兩邊元素分別爲1 注:j爲i的下一行元素
代碼如下:

class Solution {
private:
vector<int>computeNext(vector<int>&temp)
{
    vector<int> tempNext;
    tempNext.push_back(1);
    for(int i=0;i<temp.size()-1;i++)
        tempNext.push_back(temp[i]+temp[i+1]);
    tempNext.push_back(1);
    return tempNext;
}
public:
    vector<vector<int>> generate(int numRows) 
    {
        vector<vector<int>>gene;
        if(!numRows)
            return gene;
        vector<int> only;
        only.push_back(1);
        gene.push_back(only);
        for(int i=1;i<numRows;i++)
            gene.push_back(computeNext(gene[i-1]));
        return gene;
    }
};

6.Pascal’s TriangleII
題目描述:打印楊輝三角第k行,空間複雜度O(k)
知識點:楊輝三角特性
注意事項:result[j]=result[j]+result[j-1];下標從後往前更新
代碼如下:

class Solution {
public:
    vector<int> getRow(int rowIndex) 
    {

       vector<int>result(rowIndex+1,0);
       if(rowIndex<0)
        return result;
       for(int i=0;i<=rowIndex;i++)
       {
           result[rowIndex]=1;
           for(int j=rowIndex-1;j>0;j--)
               result[j]=result[j]+result[j-1];
           result[0]=1;
       }
       return result;
    }
};

二.總結
I.①hashTabel使用空間換時間的方法②更新數組最好採用從後往前會減少麻煩.II讓我們一同努力,明天會更好!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章