Weekly Contest 130

前言

上一篇博客已经是contest 119啦,也就是说2个月没有系统的刷leetcode啦。目睹了师兄师姐笔试、面试时在传统算法上的挣扎,我才陡然感觉到巨大的压力,因为我并没有比他们好多少。现在不得不把算法提到工作日程里,以期当我明年面试时能够free bug,享受到AK的快意。

1017. Convert to Base -2

传送门:https://leetcode.com/contest/weekly-contest-130/problems/convert-to-base-2/

Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).

The returned string must have no leading zeroes, unless the string is “0”.

Example 1:

Input: 2
Output: “110”
Explantion: (-2) ^ 2 + (-2) ^ 1 = 2

Example 2:

Input: 3
Output: “111”
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3

Example 3:

Input: 4
Output: “100”
Explantion: (-2) ^ 2 = 4

Note:

0 <= N <= 10^9
class Solution {
public:
    //t/S: O(logN)
    string baseNeg2(int N) {
        string ans = "";
        
        while(N) {
            ans = to_string(N&1) + ans;
            //if i is a even number,2^i==(-2)^i; if i is a odd number, 2^i==-(-2)^i, so we just need to add a "-" when it is at the odd loop
            N = -(N >> 1); 
        }
        
        return ans == "" ? "0" : ans;
    }
};

1018. Binary Prefix Divisible By 5

传送门:https://leetcode.com/contest/weekly-contest-130/problems/binary-prefix-divisible-by-5/

Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.)

Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.

Example 1:

Input: [0,1,1]
Output: [true,false,false]
Explanation:
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.

Example 2:

Input: [1,1,1]
Output: [false,false,false]

Example 3:

Input: [0,1,1,1,1,1]
Output: [true,false,false,false,true,false]

Example 4:

Input: [1,1,1,0,1]
Output: [false,false,false,false,false]

Note:

1 <= A.length <= 30000
A[i] is 0 or 1
class Solution {
public:
    vector<bool> prefixesDivBy5(vector<int>& A) {
        vector<bool> ans(A.size(), false);
        int s = 0;
        
        for(int i = 0; i < A.size(); i++) {
            /*
            优先级:`<<` < `+`
            s位移后必须对5取余,否则爆int
            */
            s = (s << 1)%5 + A[i]; 
            if(s % 5 == 0) ans[i] = true;
        }
        
        return ans;
    }
};

1019. Next Greater Node In Linked List

传送门:https://leetcode.com/contest/weekly-contest-130/problems/next-greater-node-in-linked-list/

We are given a linked list with head as the first node. Let’s number the nodes in the list: node_1, node_2, node_3, … etc.

Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0.

Return an array of integers answer, where answer[i] = next_larger(node_{i+1}).

Note that in the example inputs (not outputs) below, arrays such as [2,1,5] represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5.

Example 1:

Input: [2,1,5]
Output: [5,5,0]

Example 2:

Input: [2,7,4,3,5]
Output: [7,0,5,5,0]

Example 3:

Input: [1,7,5,1,9,2,5,1]
Output: [7,9,9,9,0,5,0,0]

Note:

1 <= node.val <= 10^9 for each node in the linked list.
The given list has length in the range [0, 10000].
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
	//S/T : O(N)
	//维护一个单调不增栈
    vector<int> nextLargerNodes(ListNode* head) {
        vector<int> v;
        for(ListNode *p = head; p; p = p->next)  //p++: AddressSanitizer: heap-buffer-overflow
            v.push_back(p->val);
        stack<int> S;
        vector<int> ans(v.size());
        
        for(int i = 0; i < v.size(); i++) {
            while(!S.empty()) {
                if(v[S.top()] < v[i]) {
                    ans[S.top()] = v[i];
                    S.pop();
                }
                else break;
            }
            S.push(i);
        }
        while(!S.empty()) {
            ans[S.top()] = 0;
            S.pop();
        }
            
        return ans;
    }
};

1020. Number of Enclaves

传送门:https://leetcode.com/contest/weekly-contest-130/problems/number-of-enclaves/

Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)

A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.

Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves.

Example 1:

Input: [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation:
There are three 1s that are enclosed by 0s, and one 1 that isn’t enclosed because its on the boundary.

Example 2:

Input: [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation:
All 1s are either on the boundary or can reach the boundary.

Note:

 1 <= A.length <= 500
 1 <= A[i].length <= 500
 0 <= A[i][j] <= 1
 All rows have the same size.
struct Pos{
    int x, y;
    Pos(int x, int y) : x(x), y(y) {}
};
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; //dx: scalar object 'dx' requires one element in initializer
class Solution {
public:
    //O(row * col)
    //bfs
    int numEnclaves(vector<vector<int>>& A) {
        vector<vector<int> > B = A;
        queue<Pos> Q;
        int ans = 0;
        
        for(int x = 0; x < B.size(); x++) {
            for(int y = 0; y < B[x].size(); y++) {
                if(!B[x][y]) continue;
                if(x > 0 && x < B.size()-1 && y > 0 && y < B[x].size()-1) {
                    ans++; //统计不在边界的land
                    continue;
                }
                Q.push(Pos(x, y));
                B[x][y] = 0; //已访问的land等同于sea
            }
        }
        
        while(!Q.empty()) {
            Pos cur = Q.front();
            Q.pop();
            for(int i = 0; i < 4; i++) {
                int x = dx[i] + cur.x;
                int y = dy[i] + cur.y;
                if(x < 0 || x >= B.size() || y < 0 || y >= B[0].size() || !B[x][y]) continue; 
                Q.push(Pos(x, y));
                B[x][y] = 0;
                ans--;
            }
        }
        
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值