每日一题day2:[LeetCode:面试题 16.05、16.15、10.05、05.01、HOT100 19、2]

每日一题day2:LeetCode

面试题 16.05. 阶乘尾数

面试题 16.15. 珠玑妙算

面试题 10.05. 稀疏数组搜索

面试题 05.01. 插入

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

2. 两数相加

34. 在排序数组中查找元素的第一个和最后一个位置


1、面试题 16.05. 阶乘尾数

class Solution {
    
    
public:
    int trailingZeroes(int n) {
    
    
        int ans = 0;
        while(n >= 5){
    
    
            ans += n / 5;
            n /= 5;
        }
        return ans;
    }
};

2、面试题 16.15. 珠玑妙算

class Solution {
    
    
public:
    int ans[2];
    vector<int> masterMind(string solution, string guess) {
    
    
        map<char, int> cnt1, cnt2;
        ans[0] = ans[1] = 0;
        int len1 = solution.size();
        for(int i = 0; i < len1; i++){
    
    
            if(solution[i] == guess[i]) ans[0]++;
            else{
    
    
                cnt1[solution[i]]++;
                cnt2[guess[i]]++;
            }
        }
        for(int i = 0; i < len1; i++){
    
    
            if(cnt1.count(guess[i]) != 0){
    
    
                ans[1] += min(cnt1[guess[i]], cnt2[guess[i]]);
                cnt1[guess[i]] = cnt2[guess[i]] = 0;
            }
        }
        return {
    
    ans[0], ans[1]};
    }
};

3、面试题 10.05. 稀疏数组搜索

class Solution {
    
    
public:
    int findString(vector<string>& words, string s) {
    
    
        int l = 0, r = words.size() - 1, ans = -1;
        while(l <= r){
    
    
            if(words[l].size() == 0){
    
    
                l++; continue;
            }
            if(words[r].size() == 0){
    
    
                r--; continue;
            }
            int mid = (l + r) >> 1;
            while(words[mid].size() == 0){
    
    
                mid--;
                if(mid == l){
    
    
                    l = (l + r) >> 1;
                    continue;
                }
            }
            if(words[mid] == s){
    
     ans = mid; break; }
            else if(words[mid] > s) r = mid - 1;
            else l = mid + 1;
        }
        return ans;
    }
};

4、面试题 05.01. 插入

class Solution {
    
    
public:
    int insertBits(int N, int M, int i, int j) {
    
    
        if(!N) return M;
        for(int k = i; k <= j; k++){
    
    
            int temp = M % 2; M /= 2;
            if(temp){
    
    
                N |= (1 << k);
            }else{
    
    
                if(N & (1 << k)) N -= (1 << k);
            }
        }
        return N;
    }
};

5、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) {
    
    
        ListNode* p = head;
        ListNode* q = head;
        ListNode* HEAD = new ListNode(1);
        HEAD -> next = head;
        ListNode* pre = HEAD;
        while(n-- && p) p = p -> next;
        if(n != -1) return head;
        while(p){
    
    
            pre = q;
            p = p -> next;
            q = q -> next;
        }
        pre -> next = pre -> next -> next;
        return HEAD -> next;
    }
};

6、2. 两数相加

/**
 * 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* addTwoNumbers(ListNode* l1, ListNode* l2) {
    
    
        ListNode* HEAD = new ListNode(1);
        ListNode* p = l1;
        ListNode* q = l2;
        ListNode* now = HEAD;
        int yu = 0, jin = 0;
        while(p && q){
    
    
            yu = (p -> val + q -> val + jin) % 10;
            jin = (p -> val + q -> val + jin) / 10;
            ListNode* temp = new ListNode(yu);
            now -> next = temp; now = now -> next;
            p = p -> next; q = q -> next;
        }
        while(p){
    
    
            yu = (p -> val + jin) % 10;
            jin = (p -> val + jin) / 10;
            ListNode* temp = new ListNode(yu);
            now -> next = temp; now = now -> next;
            p = p -> next;
        }
        while(q){
    
    
            yu = (q -> val + jin) % 10;
            jin = (q -> val + jin) / 10;
            ListNode* temp = new ListNode(yu);
            now -> next = temp; now = now -> next;
            q = q -> next;
        }
        if(jin){
    
    
            ListNode* temp = new ListNode(jin);
            now -> next = temp; now = now -> next;
        }
        return HEAD -> next;
    }
};

7、34. 在排序数组中查找元素的第一个和最后一个位置

class Solution {
    
    
public:
    vector<int> searchRange(vector<int>& nums, int target) {
    
    
        nums.push_back(INT_MAX);
        int pos1 = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
        int pos2 = upper_bound(nums.begin(), nums.end(), target) - nums.begin() - 1;
        if(nums[pos1] > target) return {
    
    -1, -1};
        else return {
    
    pos1, pos2};
    }
};

2021/3/6完结。

猜你喜欢

转载自blog.csdn.net/shangzhengyu/article/details/114433935