目录
- 168. Excel表列名称
- 171. Excel 表列序号
- 190. 颠倒二进制位
- 205. 同构字符串
- 228. 汇总区间
- 448. 找到所有数组中消失的数字
- 461. 汉明距离
- 876. 链表的中间结点
- 836. 矩形重叠
- 844. 比较含退格的字符串
168. Excel表列名称
var convertToTitle = function(columnNumber) {
let str = ''
let n = columnNumber
while (n) {
let y = n % 26 // 从后往前取余
n = n / 26 | 0 // 除以26并且向下取整
y || n-- // 余数为0 ,n--
str = String.fromCharCode(64 + (y || 26)) + str
}
return str
};
171. Excel 表列序号
var titleToNumber = function(columnTitle) {
let result = 0;
let s = columnTitle;
for (let i = 0, len = s.length; i < len; i++) {
result += (s[i].charCodeAt() - 64) * 26 ** (len - i - 1);
}
return result;
};
190. 颠倒二进制位
var reverseBits = function(n) {
return parseInt((n).toString(2).padStart(32, '0').split('').reverse().join(''), 2)
};
205. 同构字符串
var isIsomorphic = function(s, t) {
let sMap = {}
let tMap = {}
for (let i = 0; i < s.length; i++) {
if (sMap[s[i]] !== tMap[t[i]]) return false
sMap[s[i]] = i
tMap[t[i]] = i
}
return true
};
228. 汇总区间
var summaryRanges = function(nums) {
if (nums.length == 0) return []
let left = 0,
right = 0
let res = []
for (let i = 0; i < nums.length; i++) {
if (nums[i] + 1 === nums[i + 1]) {
right++
} else {
res.push(left === right ? `${nums[i]}` : `${nums[left]} -> ${nums[right]}`)
left = i + 1
right = i + 1
}
}
return res
};
448. 找到所有数组中消失的数字
var findDisappearedNumbers = function(nums) {
var res = []
for (let i = 1; i <= nums.length; i++) {
if (nums.indexOf(i) === -1) {
res.push(i)
}
}
return res
};
461. 汉明距离
var hammingDistance = function(x, y) {
var binX = x.toString(2)
var binY = y.toString(2)
var len = binX.length > binY.length ? binX.length : binY.length
binX = binX.padStart(len, '0')
binY = binY.padStart(len, '0')
var disCount = 0
for (let i = 0; i < len; i++) {
if (binX[i] !== binY[i]) {
disCount++
}
}
return disCount
};
876. 链表的中间结点
var middleNode = function(head) {
let slow = head
let fast = head
while (fast !== null && fast.next !== null) {
slow = slow.next
fast = fast.next.next
}
return slow
};
836. 矩形重叠
var isRectangleOverlap = function(rec1, rec2) {
if (rec1[0] >= rec2[2] || rec1[2] <= rec2[0] || rec1[1] >= rec2[3] || rec1[3] <= rec2[1]) {
return false
}
return true
};
844. 比较含退格的字符串
var backspaceCompare = function(s, t) {
let i = s.length - 1
let j = t.length - 1
let backSpaceS = 0
let backSpaceT = 0
while (i >= 0 || j >= 0) {
while (i >= 0) {
if (s[i] == '#') {
backSpaceS++
i--
} else if (backSpaceS > 0) {
backSpaceS--
i--
} else {
break
}
}
while (j >= 0) {
if (t[j] == '#') {
backSpaceT++
j--
} else if (backSpaceT > 0) {
backSpaceT--
j--
} else {
break
}
}
if (s[i] !== t[j]) {
return false
}
i--
j--
}
return true
};
标签:function,448,205,--,链表,length,let,var,return
From: https://www.cnblogs.com/echoyya/p/17041225.html