1029. Binary Prefix Divisible By 5

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

Approach #1:

 

  

1030. Next Greater Node In Linked List

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

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);
}
}
};

  

04-17 12:02