加州提子面包


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • 搜索
加州提子面包

JavaScript算法试题系列-05

发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 82 字 | 阅读 次

题目要求:

1
2
// 会问字符串判断某个字符串是否为回文字符串,譬如racecar与race car都是回文字符串:isPalindrome("racecar"); // true
isPalindrome("race Car"); // true

解答:

1
2
3
4
5
6
7
function isPalindrome(word) {
// Replace all non-letter chars with "" and change to lowercase
var lettersOnly = word.toLowerCase().replace(/\s/g, "");
// Compare the string with the reversed version of the string
return lettersOnly === lettersOnly.split("").reverse().join("");
}
加州提子面包

JavaScript算法试题系列-04

发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 108 字 | 阅读 次

题目要求:

1
2
3
4
// 乱序同字母字符串给定两个字符串,判断是否颠倒字母而成的字符串,譬如Mary与Army就是同字母而顺序颠倒:var firstWord = "Mary";
var secondWord = "Army";
isAnagram(firstWord, secondWord); // true

解答:

1
2
3
4
5
6
7
8
9
10
11
function isAnagram(first, second) {
// For case insensitivity, change both words to lowercase.
var a = first.toLowerCase();
var b = second.toLowerCase();
// Sort the strings, and join the resulting array to a string. Compare the results
a = a.split("").sort().join("");
b = b.split("").sort().join("");
return a === b;
}
加州提子面包

JavaScript算法试题系列-03

发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 93 字 | 阅读 次

题目要求:

1
2
3
4
5
6
7
// 字符串颠倒字符串给定某个字符串,要求将其中单词倒转之后然后输出,譬如"Welcome to this Javascript Guide!" 应该输出为 "emocleW ot siht tpircsavaJ !ediuG"。var string = "Welcome to this Javascript Guide!";
// Output becomes !ediuG tpircsavaJ siht ot emocleW
var reverseEntireSentence = reverseBySeparator(string, "");
// Output becomes emocleW ot siht tpircsavaJ !ediuG
var reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");

解答:

1
2
3
function reverseBySeparator(string, separator) {
return string.split(separator).reverse().join(separator);
}
加州提子面包

JavaScript算法试题系列-02

发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 172 字 | 阅读 次

题目要求:

1
2
3
4
// 数组交集给定两个数组,要求求出两个数组的交集,注意,交集中的元素应该是唯一的。var firstArray = [2, 2, 4, 1];
var secondArray = [1, 2, 0, 2];
intersection(firstArray, secondArray); // [2, 1]

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function intersection(firstArray, secondArray) {
// The logic here is to create a hashmap with the elements of the firstArray as the keys.
// After that, you can use the hashmap's O(1) look up time to check if the element exists in the hash
// If it does exist, add that element to the new array.
var hashmap = {};
var intersectionArray = [];
firstArray.forEach(function(element) {
hashmap[element] = 1;
});
// Since we only want to push unique elements in our case... we can implement a counter to keep track of what we already added
secondArray.forEach(function(element) {
if (hashmap[element] === 1) {
intersectionArray.push(element);
hashmap[element]++;
}
});
return intersectionArray;
// Time complexity O(n), Space complexity O(n)
}
加州提子面包

JavaScript算法试题系列-01

发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 169 字 | 阅读 次

题目要求:

1
2
3
4
5
6
7
var firstArray = [2, 2, 4, 1];
var secondArray = [0, 0, 0, 2];
var thirdArray = [-2, -2, -3, 2];
productExceptSelf(firstArray); // [8, 8, 4, 16]
productExceptSelf(secondArray); // [0, 0, 0, 0]
productExceptSelf(thirdArray); // [12, 12, 8, -12]

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function productExceptSelf(numArray) {
var product = 1;
var size = numArray.length;
var output = [];
// From first array: [1, 2, 4, 16]
// The last number in this case is already in the right spot (allows for us)
// to just multiply by 1 in the next step.
// This step essentially gets the product to the left of the index at index + 1
for (var x = 0; x < size; x++) {
output.push(product);
product = product * numArray[x];
}
// From the back, we multiply the current output element (which represents the product
// on the left of the index, and multiplies it by the product on the right of the element)
var product = 1;
for (var i = size - 1; i > -1; i--) {
output[i] = output[i] * product;
product = product * numArray[i];
}
return output;
}
1234…6
MiG

MiG

Learning | Coding | Reading | Running

30 日志
11 分类
20 标签
RSS
GitHub 微博 知乎
Creative Commons
Links
  • 更新日志
  • CNode
© 2016 - 2018 Created By MiG
由 Hexo 强力驱动
主题 - NexT.Mist