LeetCode Day9字符串+链表

299 猜数字游戏

题目理解

给定两个数组secret、guess。
公牛:两个数组对应位置的数字相等。
奶牛:数字相等,位置不同。
每位数字只统计一次,例如“1123” “0111”,第二个1为公牛,第三个或第四个1为奶牛(换句话说,如果“11231” “01110”,第三第四个1都为奶牛)。

思路

公牛数容易求得,遍历两数组,对应位置元素相等则公牛数+1。考虑用哈希表统计secret数组中出现的字符,求公牛数的过程中,将公牛数字对应的数量-1(代表该数字已被用过)。
接着求母牛数,因为哈希表中的数字都是去除公牛数以后的,只要判断guess中剩下的数字是否在secret中存在,即可判断是不是母牛数,如果存在,母牛数字对应的数量-1(因为每位数字只统计一次)。

代码

class Solution {
public:
    string getHint(string secret, string guess) {
        int Bulls=0,Cows=0;
        unordered_map<char,int> mp;
        string s;
        for(auto c:secret)
            ++mp[c];
        for(int i=0;i<guess.size();i++){
            if(guess[i]==secret[i])
            {
                ++Bulls;
                --mp[guess[i]];
                if(mp[guess[i]]==0)
                    mp.erase(guess[i]);
            }else
                s+=guess[i];
        }
        for(int i=0;i<s.size();i++)
            if(mp.find(s[i])!=mp.end())
            {
                ++Cows;
                --mp[s[i]];
                if(mp[s[i]]==0)
                    mp.erase(s[i]);
            }
        return to_string(Bulls)+'A'+to_string(Cows)+'B';
    }
};

481 神奇字符串

题目理解

神奇字符串:只有1、2,且1、2连续出现的次数与字符串相同。

思路

我们可以按两个字符串来考虑,串2为串1次数,如
1 22 11 2 1 22 1 22 11 2 11 22
1 2 2 1 1 2 1 2 2 1 2 2
可以发现串2第一个数字为1,串1有1个1;紧接着,串2为2,因为串1上一个数字为1,则此时必为2,且串2为串1的次数,因此为两个2,如果串1上一个数字为2呢,则串1要加的就是两个1;同理,串2数字为1,串1上一次数字为1,则串1加1个2,串1上一次数字为2,则串1加1个1。
具体实现我们可以考虑用两个指针来表示串1和串2,显然串1一定比串2要快。

代码

class Solution {
public:
    int magicalString(int n) {
        int fast=2;
        int res=1;
        if(n<=0) return 0;
        string s="122";
        for(int i=2;i<n;++i){
            if(s[i]=='2'){       //注意是字符2
                if(s[fast]=='2')
                    s+="11";
                else s+="22";
                fast+=2;
            }else{
                ++res;
                if(s[fast]=='2')
                    s+='1';
                else s+='2';
                fast+=1;
            }
        }
        return res;
    }
};

链表

203 移除链表元素

思路

删除链表中所有满足 Node.val == val 的节点,就是遍历链表寻找满足条件的结点删除即可。
链表的删除操作是上一结点的next指向当前结点的next,即可删除当前结点,如果要删除的结点是头结点呢,它没有上一结点,需要做的是将头结点向后移或是在头结点前加一个头指针,它指向头结点,这样头结点前也有结点。

代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* h=new ListNode();
        h->next=head;
        ListNode* cur=h;
        while(cur->next != nullptr){
            if(cur->next->val == val){
                ListNode* tmp=cur->next;
                cur->next=cur->next->next;
                delete tmp;
            }else 
                cur=cur->next;
        }
        return h->next;//注意是头指针所指结点
    }
};

707 设计链表

题目

考察的是对链表的常用操作。采用单链表实现。需要注意的是链表中的结点为0~index。

代码

class MyLinkedList {
public:
    struct LinkedNode{
        int val;
        LinkedNode* next;
        LinkedNode(int x):val(x), next(nullptr) {}
    };
    /** Initialize your data structure here. */
    MyLinkedList() {
        head=new LinkedNode(0);
        size=0;
    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    int get(int index) {
        if (index > (size-1) || index < 0)
            return -1;
        LinkedNode* cur = head->next;
        while (index--) { //不能为--index,否则index为0时为死循环
            cur = cur->next;
        }
        return cur->val;

    }
    
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    void addAtHead(int val) {
        LinkedNode* tmp = new LinkedNode(val);
        tmp->next = head->next;
        head->next = tmp;
        ++size;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        LinkedNode* tmp = head;
        while(tmp->next != nullptr)
            tmp=tmp->next;
        LinkedNode* tail=new LinkedNode(val);
        tmp->next=tail;
        ++size;
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    void addAtIndex(int index, int val) {
        if(index > size)
            return ;
      
            LinkedNode* tmp = new LinkedNode(val);
            LinkedNode* cur = head;
            while(index--)
                cur = cur->next;
            tmp->next = cur->next;
            cur->next = tmp;

        ++size;
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index > (size - 1) || index < 0)
            return ;
        LinkedNode* cur = head;
            while(index--)
                cur = cur->next;
            LinkedNode* tmp = cur->next;
            cur->next = tmp->next;
            delete tmp;
       
        size--;
    }
private:
    LinkedNode* head;
    int size;
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值