Weekly Contest 130
1029. Binary Prefix Divisible By 5
Given an array
A
of0
s and1
s, considerN_i
: the i-th subarray fromA[0]
toA[i]
interpreted as a binary number (from most-significant-bit to least-significant-bit.)Return a list of booleans
answer
, whereanswer[i]
istrue
if and only ifN_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]
is0
or1
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 = 2Example 2:
Input: 3
Output: "111"
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3Example 3:
Input: 4
Output: "100"
Explantion: (-2) ^ 2 = 4
Note:
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 thenode_j.val
such thatj > i
,node_j.val > node_i.val
, andj
is the smallest possible choice. If such aj
does not exist, the next larger value is0
.Return an array of integers
answer
, whereanswer[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]
.
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,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.
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);
}
}
};
Weekly Contest 130的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- mysql优化概述4
一.分区 1.分区概念 将某张表数据,分别存储到不同的区域中. 每个分区,都是独立的表,都要存储该分区的数据,索引信息. 2.创建分区 创建表并指定分区的选项 create table 表名 ( 定义 ...
- 将Boost库添加到Visual Studio 2017
在windows 环境中,一般比较推荐的打包软件的方式是,将自己所需要的共享库放在软件自己的文件夹中,并且避免与其它的软件共用.除非是微软的官方组件,比如微软自家的VC Runtime. Boost库 ...
- DNA motif 搜索算法总结
DNA motif 搜索算法总结 2011-09-15 ~ ADMIN 翻译自:A survey of DNA motif finding algorithms, Modan K Das et. al ...
- 2018年这些UI设计趋势正在流行,跟上必拿高薪!
数字设计领域和时尚圈是一样的,总会有各种各样的趋势让人眼花缭乱.无论是用户界面的视觉元素,还是用户体验的细节,总有许多值得说道的新玩法和新方向.就目前来看,UI设计的大趋势是更加大胆新颖的视觉设计,通 ...
- php调用C#写的dll包
1.转到需要注册的dll路径下 2.输入regasm命令+文件名 3问题解决
- 如何修改路由器的登录IP地址?
如何修改路由器的登录IP地址? 因为有多个路由器,为了区分不同路由器,我们可以修改它的登录IP,而且修改后,可以在连接的电脑上直观地知道所连接的是哪一台路由器 买回来的路由器,一般默认的登录地址是19 ...
- 为了记忆和方便翻阅 vue构建后的结构目录说明
一. ├── build // 项目构建(webpack)相关代码 记忆:(够贱) 9个 │ ├── build.js // 生产环 ...
- PHP二个高精确度数字相加减
1.相加 string bcadd(string left operand, string right operand, int [scale]); 2.相减 string bcsub(string ...
- C++动态分配内存(new)和撤销内存(delete)
在软件开发过程中,常常需要动态地分配和撤销内存空间,例如对动态链表中结点的插入与删除.在C语言中是利用库函数malloc和free来分配和撤销内存空间的.C++提供了较简便而功能较强的运算符new和d ...
- VHDL数据类型
VHDL表示16进制 如 a : std_logic_vector(7 downto 0) 把0x55赋给a a <= x"55"; b表示二进制 b“1011_1111” ...