栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

华为od的一些算法题

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

华为od的一些算法题

【报数游戏】100个人围成一圈,每个人有一个编码,编号从1开始到100。他们从1开始依次报数,报到为M的人自动退出圈圈,然后下一个人接着从1开始报数,直到剩余的人数小于M。请问最后剩余的人在原先的编号为多少?

输入描述:输入一个整数参数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));
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1005357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号