365天挑战LeetCode1000题——Day 095 旋转数字 电话号码的字母组合 删除链表的倒数第 N 个结点

788. 旋转数字

在这里插入图片描述

代码实现(模拟)

class Solution {
public:
    int rotatedDigits(int n) {
        set<int> success = {2, 5, 6, 9};
        set<int> accept = {0, 1, 8};
        int ans = 0;
        for (int i = 2; i <= n; i++) {
            int num = i;
            bool flag = false;
            while (num) {
                if (success.count(num % 10) || accept.count(num % 10)) {
                    if (success.count(num % 10)) flag = true;
                    num /= 10;
                }
                else {
                    break;
                }
            }
            if (num == 0 && flag) ans++;
        }
        return ans;
    }
};

17. 电话号码的字母组合

在这里插入图片描述

代码实现(BFS)

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        if (digits.empty()) return {};
        map<char, vector<char>> dict;
        dict['2'] = {'a', 'b', 'c'};
        dict['3'] = {'d', 'e', 'f'};
        dict['4'] = {'g', 'h', 'i'};
        dict['5'] = {'j', 'k', 'l'};
        dict['6'] = {'m', 'n', 'o'};
        dict['7'] = {'p', 'q', 'r', 's'};
        dict['8'] = {'t', 'u', 'v'};
        dict['9'] = {'w', 'x', 'y', 'z'};
        vector<string> ans = { "" };
        for (char c : digits) {
            vector<string> tmp;
            for (string str : ans) {
                for (char nex : dict[c]) {
                    string tmpStr = str;
                    tmpStr += nex;
                    tmp.push_back(tmpStr);
                }
            }
            ans = tmp;
        }
        return ans;
    }
};

19. 删除链表的倒数第 N 个结点

在这里插入图片描述

代码实现(遍历)

/**
 * 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* removeNthFromEnd(ListNode* head, int n) {
        int sz = 0;
        ListNode* tmp = head;
        while (tmp) {
            tmp = tmp->next;
            sz++;
        }
        int step = sz - n;
        tmp = head;
        if (step == 0) return head->next;
        while (--step) {
            tmp = tmp->next;
        }
        tmp->next = tmp->next->next;
        return head;

    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值