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;}
};
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!