LeetCode Notes 024

Excel Sheet Column Number

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

1
2
3
4
5
6
7
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @param {string} s
* @return {number}
*/
var titleToNumber = function(s) {
var a = s.split('').reverse();
s = a.join('');
var ans = a.reduce(function(pre, item, index) {
return pre + Math.pow(26, index) * (s.charCodeAt(index) - 64);
}, 0);
return ans;
};

Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

1
2
3
4
5
6
7
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @param {number} n
* @return {string}
*/
var convertToTitle = function(n) {
var ans = '';
while (n) {
if (n % 26 === 0) {
ans += String.fromCharCode(26 + 64);
n = (n / 26) - 1;
}
else {
ans += String.fromCharCode(n % 26 + 64);
n = ~~(n / 26);
}
}

return ans.split('').reverse().join('');
};

Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {number} n
* @return {number}
*/
var trailingZeroes = function(n) {
var ans = 0;
while (n) {
ans += ~~(n / 5);
n /= 5;
n = ~~n;
}

return ans;
};

Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

Example 1:

1
2
3
4
5
6
7
8
9
Input:
s: "cbaebabacd" p: "abc"

Output:
[0, 6]

Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

1
2
3
4
5
6
7
8
9
10
Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use strict";

/**
* @param {string} s
* @param {string} p
* @return {number[]}
*/
var findAnagrams = function(s, p) {
let len = p.length;
let hash = {};
let ans = {};
let ret = [];

for (let i = 0, l = p.length; i < l; i++) {
let index = p.charCodeAt(i) - 97;
ans[index] = ~~ans[index] + 1;
}

for (let i = 0, l = s.length; i < l; i++) {
let index = s.charCodeAt(i) - 97;
hash[index] = ~~hash[index] + 1;

if (i >= len) { // remove
let index = s.charCodeAt(i - len) - 97;
hash[index] = ~~hash[index] - 1;
}

if (i + 1 >= len) { // can compare
help() && ret.push(i - len + 1);
}
}

function help() {
for (let i = 0; i < 26; i++) {
if (~~hash[i] !== ~~ans[i])
return false;
}

return true;
}

return ret;
};

Find All Duplicates in an Array

Given an array of integers, $1 ≤ a[i] ≤ n$ (n = size of array), some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

Could you do it without extra space and in $O(n)$ runtime?

Example:

1
2
3
4
5
Input:
[4,3,2,7,8,2,3,1]

Output:
[2,3]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @param {number[]} nums
* @return {number[]}
*/
var findDuplicates = function(nums) {
let len = nums.length;
let ans = [];

for (let i = 0; i < len; i++) {
let item = nums[i];
let val = Math.abs(item) - 1;

if (nums[val] < 0)
ans.push(val + 1);
else
nums[val] *= -1;
}

return ans;
};
文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/04/05/LeetCode-Notes-024/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏