Comments (2)
1.写出一个方法输出1-100内的所有素数。
解析:
由素数(质数)定义可知:
- ① 0、1都不是质数,那么最小的质数就是2。
- ② 除了1和它自身,不能被其他数整除,那么代码表达式表示为:
i % j === 0
代码实现
function fn() {
const arr = []
for (let i = 2; i <= 100; i++) {
let bl = false
for (let j = 2; j <= i; j++) {
if (i === j) continue
i % j === 0 && (bl = true)
}
!bl && arr.push(i)
}
return arr
}
console.log(fn())
输出:// [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
2.给定一个整数数组,实现快速排序算法进行升序排列。如[2, 5, 8, 9, 3] =>[2, 3, 5, 8, 9]
代码实现
function qSort(arr) {
if (arr.length <= 1) return arr
const index = Math.floor(arr.length / 2)
const midVal = arr.splice(index, 1)[0]
const left = [],
right = []
arr.forEach(item => {
if (item < midVal) left.push(item)
else right.push(item)
})
return [...qSort(left), midVal, ...qSort(right)]
}
qSort([2, 5, 8, 9, 3])
**输出:// [2, 3, 5, 8, 9]
解析:(分治法)
将一个列表分割为左右两块,然后再将字列表再进行分割为左右两块,如何反复,知道子元素长度为1时,结束!
from fe9-interview.
from fe9-interview.
Related Issues (20)
- 微店: 将二维数组转成一维数组的方法有哪些(一面) HOT 1
- 有赞: 为什么在类实例中可以访问this.setState (二面) HOT 1
- 有赞: 介绍暂时性死区(一面) HOT 2
- 兑吧: prototype 和 proto 区别(一面) HOT 1
- 今日头条: 介绍下Promise,内部实现(一面) HOT 1
- 有赞: 在不改变原数组的前提下,添加或删除某个元素(一面) HOT 2
- 阿里巴巴: 使用伪码实现 npm install 部分内容(二面)
- 有赞: sum(2, 3)实现sum(2)(3)的效果(一面) HOT 1
- To Tom0121 前端开发中设计到什么模式 HOT 1
- 滴滴: JS异步解决方案的发展历程以及优缺点(一面) HOT 1
- 携程:数组去重和数组扁平化(一面) HOT 1
- 阿里巴巴:实现一些主流框架的循环渲染(笔试) HOT 1
- To littleKnave: 实现一个Promise.prototype.first(今日头条) HOT 1
- 阿里巴巴:浏览器的强缓存和协商缓存(一面) HOT 3
- 51 信用卡: apply,call,bind 的区别(一面) HOT 2
- 为什么使用 ctrl+f5 或启用开发者面板中 Disable cache 选项后还会存在读取缓存的现象 HOT 1
- 阿里:Koa-body原理(一面)
- 腾讯:js中精度问题及解决方案(一面) HOT 1
- 如何将 node 标准的回调 API 在 fibjs 内转为"同步"调用形式(一面) 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 fe9-interview.