7kyu Ones and Zeros

题目:

Given an array of one‘s and zero‘s convert the equivalent binary value to an integer.

Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1

Examples:

Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11

答案:

// 1
const binaryArrayToNumber = arr => parseInt(arr.join(‘‘), 2);

// 2
function binaryArrayToNumber (arr) {
  return arr.reduce( (a, b) => a << 1 | b);
}

// 3
const binaryArrayToNumber = arr => {
  var regexComma = arr.toString().replace(/,/g, ‘‘);
  return parseInt(regexComma, 2);
}

时间: 2024-07-30 01:02:58

7kyu Ones and Zeros的相关文章

LeetCode(283): Move Zeros

Move Zeros: Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

AES 加密位: 128位,加密模式:CBC, 填充模式:Zeros

// AES 加密 public byte[] AESEncrypt(string text) { byte[] data = Encoding.Unicode.GetBytes(text); SymmetricAlgorithm aes = Rijndael.Create(); aes.Key = keyArray; aes.IV = keyArray; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.Zeros; using (Mem

[LintCode] Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Example Given a matrix [ [1,2], [0,3] ], return[[0,2],[0,0]] Challenge Did you use extra space?A straight forward solution using O(mn) space is probably a b

UVA 12063 Zeros and Ones (数位dp)

Binary numbers and their pattern of bits are always very interesting to computer programmers. In this problem you need to count the number of positive binary numbers that have the following properties: The numbers are exactly N bits wide and they hav

UVa 12063 (DP) Zeros and Ones

题意: 找出长度为n.0和1个数相等.没有前导0且为k的倍数的二进制数的个数. 分析: 这道题要用动态规划来做. 设dp(zeros, ones, mod)为有zeros个0,ones个1,除以k的余数为mod的二进制数的个数,则状态转移方程为: dp(zeros + 1, ones, (mod>>1) % k) += dp(zeros, ones, mod) dp(zeros, ones + 1, ((mod>>1)+1) % k) += dp(zeros, ones, mod)

[LintCode] Trailing Zeros

Write an algorithm which computes the number of trailing zeros in n factorial. Example 11! = 39916800, so the out should be 2 Challenge O(log N) time 1 class Solution { 2 public long trailingZeros(long n) { 3 long cnt = 0; 4 while(n != 0){ 5 cnt += n

A. Case of the Zeros and Ones----解题报告

A. Case of the Zeros and Ones Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Conside

【matlab】=size(img)的其中两种用法&amp;zeros( )

i1=imread('D:\Work\1.png'); i1=rgb2gray(i1); [m,n]=size(i1); 返回图片的尺寸信息, 并存储在m.n中.其中m中存储的是行数,n中存储的是列数.(要是数组就求出其m*n,要是照片的话就是求出其像素大小!) i3=zeros(size(i1)); 对于一个矩阵A,size(A)表示求A的行数和列数. 设m,n是正整数,zeros(m,n)表示生成一个mxn的零矩阵. y=zeros(size(x))表示生成一个和x同样大小的的矩阵y.

python—zeros函数和ones函数

使用numpy.zeros,numpy.ones,numpy.eye等方法可以构造特定的矩阵 例如: 代码如下: >>>from numpy import * >>> a=zeros((3,4)) >>> a array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> from numpy import * >>> a=ones((3,4)