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);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用reverse函数反转leetcode字符串的方法是:将字符串作为参数传入reverse函数,然后将反转后的字符串返回给调用者。例如:let reversedString = reverse("leetcode")。 ### 回答2: 要使用reverse函数反转leetcode这个字符串,可以使用以下步骤: 1. 将字符串"leetcode"转换为字符数组。可以使用字符串的toCharArray()方法,该方法会返回一个字符数组,其中每个元素是字符串的一个字符。 2. 定义两个指针,left和right,分别指向字符数组的首尾。 3. 循环交换left和right指向的字符,然后将left指针向右移动一位,将right指针向左移动一位,直到left指针大于等于right指针。 4. 将反转后的字符数组转换回字符串。可以使用字符数组的valueOf()方法,该方法会将字符数组转换为一个新的字符串。 以下是用Java代码实现上述步骤的示例: ```java public String reverseString(String s) { // 将字符串转换为字符数组 char[] chars = s.toCharArray(); // 定义两个指针 int left = 0; int right = chars.length - 1; // 循环交换字符 while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } // 将字符数组转换为字符串 return String.valueOf(chars); } ``` 通过调用reverseString("leetcode")函数,将会返回字符串"edocteel",即将"leetcode"反转的结果。 ### 回答3: 要使用reverse函数来反转字符串"leetcode",只需将字符串转换为一个列表,然后使用reverse函数来翻转该列表,最后将列表转回字符串即可。 首先,将字符串"leetcode"转换为一个字符列表,可以使用list函数来实现,即list("leetcode"),得到列表['l', 'e', 'e', 't', 'c', 'o', 'd', 'e']。 接下来,使用reverse函数来反转该列表,即reverse(['l', 'e', 'e', 't', 'c', 'o', 'd', 'e'])。 最后,将翻转后的列表转换回字符串,可以使用join函数将列表中的字符连接起来,即''.join(['e', 'd', 'o', 'c', 't', 'e', 'e', 'l'])。 所以,将字符列表['l', 'e', 'e', 't', 'c', 'o', 'd', 'e']翻转并转换为字符串的过程为:''.join(reverse(list("leetcode"))),得到的结果为"edocteel",即字符串"leetcode"反转后的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值