Comments (8)
动态规划
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
示例 1:
输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
示例 2:
输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶
正确答案:
var climbStairs = function(n) {
if(n >= 3){
return climbStairs(n-1) + climbStairs(n-2)
}
if(n<3){
return n
}
};
from blog.
In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.
Example:
highAndLow("1 2 3 4 5"); // return "5 1"
highAndLow("1 2 -3 4 5"); // return "5 -3"
highAndLow("1 9 3 4 -5"); // return "9 -5"
Notes:
All numbers are valid Int32, no need to validate them.
There will always be at least one number in the input string.
Output string must be two numbers separated by a single space, and highest number is first.
我的答案
function highAndLow(numbers){
// ...
return Math.max(...numbers.split(' ')) + " " + Math.min(...numbers.split(' '))
}
from blog.
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b.
array_diff([1,2],[1]) == [2]
If a value is present in b, all of its occurrences must be removed from the other:
array_diff([1,2,2,2,3],[2]) == [1,3]
我的答案:
function array_diff(a, b) {
return a.filter(v=>b.indexOf(v)<0)
}
from blog.
"123456789876543212345678987654321..." 的第 n 位是什么?
答案:
function whichn(n){
let arr = [1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2]
return arr[n % 16 - 1]
}
function whichn(str, n){
return str.charAt(n-1)
}
function whichn(str, n){
str.split('')
return str.split('')[n - 1]
}
from blog.
Friday 13th or Black Friday is considered as unlucky day. Calculate how many unlucky days are in the given year.
Find the number of Friday 13th in the given year.
Input: Year as an integer.
Output: Number of Black Fridays in the year as an integer.
Precondition: 1000 < |year| < 3000
Examples:
unluckyDays(2015) == 3
unluckyDays(1986) == 1
我的答案:
function unluckyDays(year){
//your code here
let months = [0,1,2,3,4,5,6,7,8,9,10,11]
return months.reduce((num,v) => {
if (new Date(year, v,13).getDay() == 5){
num ++
}
return num
}, 0
)
}
from blog.
Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we mask it.
Your task is to write a function maskify, which changes all but the last four characters into '#'.
Examples
maskify("4556364607935616") == "############5616"
maskify( "64607935616") == "#######5616"
maskify( "1") == "1"
maskify( "") == ""
// "What was the name of your first pet?"
maskify("Skippy") == "##ippy"
maskify("Nananananananananananananananana Batman!") == "####################################man!"
function maskify(cc) {
let arr = cc.split('')
return arr.map((ele, i) => (i - arr.length) >= -4 ? ele : '#' ).join('')
}
from blog.
判断是否为质数
function isPrime(num) {
//TODO
let ns = Math.sqrt(num)
if ( num === 0 || num === 1 ) return false
if ( num === 2 || num === 3 ) return true
if ( num % 6 !== 1 && num % 6 !== 5 ) return false
for (let i = 5; i <= ns ; i++) {
if ( num % i === 0 || num % (i + 2) === 0 ) return false
}
return true
}
from blog.
数组里独特的数字
There is an array with some numbers. All numbers are equal except for one. Try to find it!
findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55
It’s guaranteed that array contains more than 3 numbers.
我觉得我的解答方法是💩:
function findUniq(arr) {
// do magic
let obj={}
for(let i=0; i<3; i++) {
if(!obj[arr[i]]) {
obj[arr[i]] = 1
} else {
obj[arr[i]] = obj[arr[i]] + 1
if (obj[arr[i]] >= 2) return arr.filter(ele => (ele != arr[i]))[0]
}
}
}// 拿前3个数做比对 如果有数字的数量超过1 就在原数组过滤掉这个数
console.log( findUniq([ 1, 1, 1, 2, 1, 1 ]));
看看别人比较好的解法
// solution 1
function findUniq(arr) {
arr.sort((a,b)=>a-b);
return arr[0]==arr[1]?arr.pop():arr[0]
}// 排序[ 1, 1, 1, 2, 1, 1 ]->[ 1, 1, 1, 1, 1, 2 ]
// solution2:
function findUniq(arr) {
return arr.find(n => arr.indexOf(n) === arr.lastIndexOf(n));
} // 这个性能应该是3个解法里最差的,但简洁
from blog.
Related Issues (20)
- 链接失效
- 关于 electron 数据缓存
- 一些关于 VSCode 插件开发的解决方案 HOT 1
- peerDependencies作用
- ELO rating system
- VSCode 插件在 kooltest 自动化测试的开发实践
- 薅一手 Github Actions 自动发包 VSCode 插件
- 八月小结
- [摘]个人效率指南
- 关于 CDN 前端该懂的一切 HOT 1
- 《DDIA》读书笔记
- PostgreSQL 数据迁移
- 九月小结
- 如何在 Sketch 插件中使用 React Typescript HOT 1
- 十月小结
- 从 Node 上传服务说起的爬坑日记
- 八股合集
- 一个说新不算新的服务端渲染技术——流式渲染 HOT 1
- 我来杭州的这两年 HOT 1
- 五月第一周周报
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.