输入描述:输入一个整数参数M
输出描述:如果输入参数M小于等于1或者大于等于100,输出“ERROR!”;否则按照原先的编号从小到大的顺序,以英文逗号分割输出编号字符串
示例1:
输入
输出
3
58,91
function baoshu(num, numArr, item) {
let newArr = [];
for (let index = 0; index < numArr.length; index++) {
if (item !== num) {
newArr.push(numArr[index])
item++;
} else {
item = 1;
}
}
if (newArr.length < num) {
console.log(newArr);
} else {
baoshu(num, newArr, item)
}
}
function make(num) {
let arr = []
for (let i = 0; i < num; i++) {
arr.push(i + 1)
}
return arr;
}
baoshu(4, make(100), 1)
编写函数,计算输入中单词的出现次数。
Ⅰ按次数降序输出,
Ⅱ相同次数的按字母顺序升序排序。
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3
正确结果为:
Python : 5
2 : 2
or : 2
3 : 1
3? : 1
New : 1
Read : 1
and : 1
between : 1
choosing : 1
to : 1
function find(str) {
let arr = [];
str.split(' ').forEach(item => {
let bool = false;
arr.forEach((item2, index) => {
if (item2.key === item) {
bool = true;
arr[index].value += 1;
}
})
if (!bool) {
arr.push({
key: item,
value: 1
})
}
});
arr.sort((a,b)=>{
if(a.value == b.value){
if(a.key>b.key){
return 1;
}else{
return -1;
}
}else{
return b.value-a.value;
}
})
return arr;
}
let str = 'New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3'
console.log(find(str));
编写函数,给定var a = (5).plus(3).minus(6); 为 2 相当于(5+3-6=2)
//在原型上添加方法
Number.prototype.plus = function(num){ return this + num; }
Number.prototype.minus = function(num){ return this - num; }
var a = (5).plus(3).minus(6);
给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i] == nums[j] 且 abs(i - j) <= k 。如果存在,返回 true ;否则,返回 false 。
输入:nums = [1,2,3,1], k = 3
输出:true
示例 2:
输入:nums = [1,0,1,1], k = 1
输出:true
示例 3:
输入:nums = [1,2,3,1,2,3], k = 2
输出:false
//第一种方法
function find(arr, k) {
let set = new Set();
let len = arr.length;
for (let i = 0; i < len; i++) {
if(i>k){
set.delete(arr[i-k-1])
}
if(set.has(arr[i])){
return true;
}
set.add(arr[i])
}
return false;
}
//第二种方法
function find2(nums, k) {
const map = new Map();
const length = nums.length;
for (let i = 0; i < length; i++) {
const num = nums[i];
if (map.has(num) && i - map.get(num) <= k) {
return true;
}
map.set(num, i);
}
return false;
};
console.log(find([1,0,1,1],1));
console.log(find2([1,0,1,1],2));



