ts
function hammingWeight(n: number): number {
const str = n.toString(2)
let sum = 0
for (let i = 0; i < str.length; i++) {
if (str[i] === '1') sum++
}
return sum
};
ts
function hammingDistance(x: number, y: number): number {
let strx = x.toString(2)
let stry = y.toString(2)
if (strx.length > stry.length) {
const cha = strx.length - stry.length
for (let i = 0; i < cha; i++) stry = '0' + stry
} else if (stry.length > strx.length) {
const cha = stry.length - strx.length
for (let i = 0; i < cha; i++) strx = '0' + strx
}
let sum = 0
for (let i = 0; i < strx.length; i++) {
if (strx[i] !== stry[i]) sum++
}
return sum
};
ts
function reverseBits(n: number): number {
let str = n.toString(2)
let cha = 32 - str.length
for (let i = 0; i < cha; i++) {
str = '0' + str
}
let sum = 0
for(let i=0;i<str.length;i++){
if(str[i]==='1')sum += 2**i
}
return sum
};
ts
function generate(numRows: number): number[][] {
let arr: number[][] = new Array
for (let i = 0; i < numRows; i++) {
let len = i + 1
let _arr: number[] = new Array
_arr[0] = 1
_arr[len - 1] = 1
if (len > 2) {
for (let i2 = 1; i2 < len - 1; i2++) {
_arr[i2] = arr[i - 1][i2 - 1] + arr[i - 1][i2]
}
}
arr.push(_arr)
}
return arr
};
ts
function missingNumber(nums: number[]): number {
const arr = nums.sort((a, b) => a - b)
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== i) return i
}
return arr.length
};