leetcode's People
leetcode's Issues
N阶乘的结果尾数有多少个0
eventEmit
Merge Two Sorted Lists
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
计算插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
罗马数字转整数
let maps = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
'IV': 4,
'IX': 9,
'XL': 40,
'XC': 90,
'CD': 400,
'CM': 900
}
/**
* @param {string}
* @return {number}
*/
function romanToInt (str) {
let map = /(X[LC])|(I[VX])|(C[DM])|[IVXLCDM]/g // 注意:g的使用使得 match返回 一个包含所有匹配的元素的数组
let keys = str.match(map) // 返回拆分后的匹配的各个key
let resulut = keys.reduce(function(accumulator, currentValue){
return maps[currentValue] + accumulator
}, 0) // 第二个参数为accumulator的初始值
console.log(resulut)
}
优化后:在match时就进行计算,减少一次遍历
function romanToInt (str) {
let map = /(X[LC])|(I[VX])|(C[DM])|[IVXLCDM]/g // 注意:g的使用使得 match返回 一个包含所有匹配的元素的数组
let result = 0;
str.replace(map, function(match){
console.log(match)
result += maps[match]
})
console.log(result)
}
无重复字符的最长子串
let a = 'abcashdekdajdhf';
let max = 0, str;
let strObj = [] // 保存所有不重复子串
for (let m = 0, n = 0; n <= a.length; n++) {
str = a.slice(m, n) // 截取区间不包含N
let next = a.charAt(n)
let index = str.indexOf(next);
if (index != -1) {
strObj.push(str)
max = Math.max(max, str.length)
m = m + index + 1 // 准确的窗口m值
}
}
console.log(strObj)
console.log(max)
爬楼梯
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
promise-some
报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。
1 被读作 "one 1" ("一个一") , 即 11。
11 被读作 "two 1s" ("两个一"), 即 21。
21 被读作 "one 2", "one 1" ("一个二" , "一个一") , 即 1211
按照报数方式 写出数值
二维数组斜45度打印
let arr = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12,13]
]
let arrLen = arr.length
let result =[]
for (let i = 0; i< arrLen; i++) {
let initIndex = i > 0 ? (arr[i - 1].length - 1) : 0
for (let j = initIndex; j < arr[i].length; j++) {
for (let next_index = i; next_index < arrLen; next_index++) {
let offset = next_index - i
let item = j - offset
if (item < 0) {
break
} else {
result.push(arr[next_index][item])
}
}
}
}
console.log(result.join(','))
有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
回文数
function isRoundNum(number){
if (number < 0 || (number% 10 == 0 && number != 0) ) { // 负数不能是回文 和 最后一个数为0 的情况
return false;
}
let a = number.toString(); // 去掉+
let harf = Math.round(a.length / 2) - 1; // 注意中间数 和 下标值之间差1
for(let i = 0; i<= harf; i++) {
let other = a.length -1 - i; // 注意最后一个中间数的下标值
if (a[i] != a[other]) {
return false;
}
}
return true
}
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数
let nums = [2, 7, 11, 15], target = 9;
let maps = {}
function (nums, target) {
for(let i=0; i++; i<nums.length) {
let value = nums[i]
maps[value] = i // { value: index } 快速查找
if ((target-value) in maps) { // 组件maps时 向前查找已存在maps,优于 全部 maps 之后再便利
return [maps[target-value], i]
}
}
}
1-100 每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.