1. 程式人生 > 其它 >春節刷題day10:[LeetCode:面試題 02.06、203、面試題 04.03、1669、1342、劍指offer 49]

春節刷題day10:[LeetCode:面試題 02.06、203、面試題 04.03、1669、1342、劍指offer 49]

技術標籤:刷題指南c++

春節刷題day10:LeetCode

面試題 02.06. 迴文連結串列

203. 移除連結串列元素

面試題 04.03. 特定深度節點連結串列

1669. 合併兩個連結串列

1342. 將數字變成 0 的操作次數

劍指 Offer 49. 醜數


1、面試題 02.06. 迴文連結串列

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution { public: bool isPalindrome(ListNode* head) { int cnt = 0; ListNode* p = head; while(p){ cnt++; p = p -> next; } if(cnt <= 1) return true; int mid = floor(cnt / 2.0); if(cnt & 1) mid++; ListNode*
HEAD; p = head; cnt = 0; while(p){ cnt++; if(cnt == mid){ HEAD = p; break; } p = p -> next; } p = p -> next; HEAD -> next = NULL; while(p){ ListNode* r = p -> next; p -
> next = HEAD -> next; HEAD -> next = p; p = r; } p = head; HEAD = HEAD -> next; bool ok = true; while(HEAD){ if(p -> val != HEAD -> val){ ok = false; break; } p = p -> next; HEAD = HEAD -> next; } return ok; } };

2、203. 移除連結串列元素

/**
 * 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* HEAD = new ListNode(1);
        HEAD -> next = head;
        ListNode* p = HEAD -> next;
        ListNode* pre = HEAD;
        while(p){
            ListNode* suf = p -> next;
            if(p -> val == val){
                pre -> next = suf;
                p = suf;
            }else{
                pre = p; p = suf;
            }
        }
        return HEAD -> next;
    }
};

3、面試題 04.03. 特定深度節點連結串列

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<ListNode*> listOfDepth(TreeNode* tree) {
        vector<ListNode*> ans;
        queue<TreeNode*> que;
        que.push(tree);
        while(!que.empty()){
            ListNode* head = new ListNode(1);
            ListNode* p = head;
            int size = que.size();
            while(size--){
                TreeNode* now = que.front(); que.pop();
                ListNode* node = new ListNode(now -> val);
                p -> next = node; p = p -> next;
                if(now -> left) que.push(now -> left);
                if(now -> right) que.push(now -> right);
            }
            p -> next = NULL;
            ans.push_back(head -> next);
        }
        return ans;
    }
};

4、1669. 合併兩個連結串列

/**
 * 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* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
        ListNode* head = new ListNode(1); head -> next = list1;
        ListNode* p = head;
        ListNode* l;
        ListNode* r;
        int cnt = -1;
        while(p){
            cnt++;
            if(cnt == a) l = p;
            if(cnt == b){
                r = p -> next -> next; break;
            }
            p = p -> next;
        }
        l -> next = list2;
        while(list2 -> next) list2 = list2 -> next;
        list2 -> next = r;
        return head -> next;
    }
};

在這裡插入圖片描述

5、1342. 將數字變成 0 的操作次數

class Solution {
public:
    int numberOfSteps (int num) {
        int ans = 0;
        while(num){
            ans++;
            if(num & 1) num -= 1;
            else num /= 2;
        }
        return ans;
    }
};

6、劍指 Offer 49. 醜數

class Solution {
public:
    int nthUglyNumber(int n) {
        int a = 0, b = 0, c = 0;
        vector<int> dp(n); dp[0] = 1;
        for(int i = 1; i < n; i++){
            int x = dp[a] * 2;
            int y = dp[b] * 3;
            int z = dp[c] * 5;
            dp[i] = min(min(x, y), z);
            if(dp[i] == x) a++;
            if(dp[i] == y) b++;
            if(dp[i] == z) c++;
        }
        return dp[n - 1];
    }
};

2021/2/15完結(《1669. 合併兩個連結串列》這個題寫起來有點奇怪,但是還是通過了,等啥時候有心情了再看看吧,寫的我一頭霧水。。。)。