CamelCase Method
Write simple .camelCase method (camel_case function in PHP, CamelCase in C# or camelCase in Java) for strings. All words must have their first letter capitalized without spaces.
For instance:
1 | "hello case".camelCase() => HelloCase |
解法
- 我自己的解法
1 | String.prototype.camelCase = function () { |
- 看到的更加巧妙的解法
1 | String.prototype.camelCase = function () { |
Format a string of names like ‘Bart, Lisa & Maggie’
Given: an array containing hashes of names.
Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.
Example:
1 | list([{name: "Bart"}, {name: "Lisa"}, {name: "Maggie"}]) |
解法
1 | function list(names) { |
scramble
Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
Notes:
- Only lower case letters will be used (a-z).
- Performance needs to be considered.
Examples:
1 | scramble("rkqodlw", "world") ==> true |
我的解法
1 | function scramble(str1, str2) { |
最开始的时候我用了 indexOf 来查找 str1 是否存在 str2 中的字符,跑测试的时候超时了。
果然还是效率太低了。构造对象之后,使用 key:value 的方式寻找效率更好。