加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Weekly Contest 130

发布时间:2020-12-14 04:35:32 所属栏目:大数据 来源:网络整理
导读:1029.?Binary Prefix Divisible By 5 Given an array? A ?of? 0 s and? 1 s,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?

1029.?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,and 3 in base-10. Only the first number is divisible by 5,so answer[0] is true. 

Example 2:

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

Example 3:

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

Example 4:

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

?


Note:

    1. 1 <= A.length <= 30000
    2. A[i]?is?0?or?1

?

Approach #1:?

class Solution {
public:
    vector<bool> prefixesDivBy5(vector<int>& A) {
        int n = A.size();
        vector<bool> ans;
        int temp = 0;
        for (int i = 0; i < n; ++i) {
            temp = (temp << 1) + A[i];
            if (temp % 5 == 0) ans.push_back(true);
            else ans.push_back(false);
            temp %= 5;
        }
        
        return ans;
    }
};

  

1028.?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:

  1. 0 <= N <= 10^9

?

Approach #1:?

?

  

1030.?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,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,5]
Output: [5,5,0] 

Example 2:

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

Example 3:

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

?

Note:

  1. 1 <= node.val?<= 10^9?for each node in the linked list.
  2. The given list has length in the range?[0,10000].

?

Approach #1:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x),next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
        vector<int> temp;
        while (head != NULL) {
            temp.push_back(head->val);
            head = head->next;
        }
        
        int len = temp.size();

        vector<int> ans;
 
        
        for (int i = 0; i < len; ++i) {
            bool flag = false;
            for (int j = i+1; j < len; ++j) {
                if (temp[j] > temp[i]) {
                    ans.push_back(temp[j]);
                    flag = true;
                    break;
                } 
            }
            if (!flag) ans.push_back(0);
        }
        
        return ans;
    }
};

  

1031.?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],[1,[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,0]]
Output: 0 Explanation: All 1s are either on the boundary or can reach the boundary.

?

Note:

  1. 1 <= A.length <= 500
  2. 1 <= A[i].length <= 500
  3. 0 <= A[i][j] <= 1
  4. All rows have the same size.

?

Approach #1:?

class Solution {
public:
    int numEnclaves(vector<vector<int>>& A) {
        int ans = 0; 
        row = A.size(),col = A[0].size();
        
        for (int i = 0; i < row; ++i) 
            for (int j = 0; j < col; ++j) 
                if (i == 0 || i == row-1 || j == 0 || j == col-1)
                    if (A[i][j] == 1)
                        dfs(A,i,j);

        for (int i = 0; i < row; ++i)
            for (int j = 0; j < col; ++j)
                if (A[i][j] == 1)
                    ans++;
        
        return ans;
    }
    
private:
    int row,col;
    vector<vector<int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
    void dfs(vector<vector<int>>& A,int x,int y) {
        A[x][y] = 0;
        for (int i = 0; i < 4; ++i) {
            int dx = x + dirs[i][0];
            int dy = y + dirs[i][1];
            if (dx < 0 || dy < 0 || dx >= row || dy >= col) continue;
            if (A[dx][dy] == 1) dfs(A,dx,dy);
        }
    }
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读