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让我们一同努力,明天会更好!

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