春节刷题day10:[LeetCode:面试题 02.06、203、面试题 04.03、1669、1342、剑指offer 49]

春节刷题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. 合并两个链表》这个题写起来有点奇怪,但是还是通过了,等啥时候有心情了再看看吧,写的我一头雾水。。。)。

猜你喜欢

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