Middle-题目42/43:274. H-Index && 275. H-Index II

题目原文:
274. H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.
According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.”
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.

275.HIndexII

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
题目大意:
274.HIndex

给出一个研究者的引用量数组,写一个算法计算这个研究者的h指数。
H指数的定义:一个人的h指数是指在一定期间内他发表的论文至少有h篇的被引频次不低于h次。
例如,给出citations数组=[3,0,6,1,5],代表这个研究者写了5篇论文,引用次数分别是3,0,6,1,5.那么他的h值是3,因为有3篇论文的引用次数>=3次。

275.HIndexII

若citations数组是升序排列的,能否优化算法?
题目分析:
根据wiki中给出的算法:
这里写图片描述
把引用数降序排列画在坐标系中,位于直线y=x上面的点个数就是h值。由于本题中引用数是降序排列的,所以改为统计位于直线x+y=n(n为论文数目)上方点的数目即可。因为数组是有序的,所以可以使用二分查找。
源码:(language:java)

public class Solution {
    public int hIndex(int[] citations) {
        int count=0,length = citations.length;
        int start=0,end=length-1,mid=0;
        while(start<=end) {
            mid = (start+end) / 2;
            if(citations[mid] >= length-mid) {
                if(mid==0 || citations[mid-1]<length-mid+1)
                    return length-mid;
                else if(citations[mid-1]>=length-mid)
                    end=mid-1;
            }
            else
                start=mid+1;
        }
        return 0;       
    }
}

成绩:
H-Index:4ms,beats 9.28%,众数1ms,38.82%
H-Index II:13ms,beats 31.68%,众数12ms,24.72%
Cmershen的碎碎念:
在第一题中,使用二分查找比无脑查找慢,但第二题中无脑插的耗时是213ms,远差于二分查找。

app.json原代码{ "pages": [ "pages/index/index", "pages/mine/index", "pages/order/list", "pages/message/index", "pages/tab-car/index", "pages/tab-product/index", "pages/tab-finance/index" ], "subPackages": [ { "root": "subpackages/car-service", "pages": ["pages/index/index", "pages/detail/index", "pages/order/create"] }, { "root": "subpackages/product", "pages": ["pages/list/index", "pages/detail/index", "pages/cart/index"] }, { "root": "subpackages/finance-circle", "pages": ["pages/index/index", "pages/loan/apply", "pages/insurance/list"] } ], "tabBar": { "custom": true, "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "static/tab-home.png", "selectedIconPath": "static/tab-home-active.png" }, { "pagePath": "pages/tab-car/index", "text": "汽车服务", "iconPath": "static/tab-car.png", "selectedIconPath": "static/tab-car-active.png" }, { "pagePath": "pages/tab-product/index", "text": "产品", "iconPath": "static/tab-product.png", "selectedIconPath": "static/tab-product-active.png" }, { "pagePath": "pages/tab-finance/index", "text": "金融", "iconPath": "static/tab-finance.png", "selectedIconPath": "static/tab-finance-active.png" }, { "pagePath": "pages/mine/index", "text": "我的", "iconPath": "static/tab-mine.png", "selectedIconPath": "static/tab-mine-active.png" } ] }, "window": { "navigationBarTitleText": "亮叶企服", "navigationBarBackgroundColor": "#1890ff", "navigationBarTextStyle": "white", "backgroundColor": "#f8f8f8", "navigationStyle": "custom" }, "usingComponents": { "custom-tab-bar": "custom-tab-bar/index" }, "preloadRule": { "pages/index/index": { "network": "all", "packages": ["car-service", "product", "finance-circle"] }, "pages/tab-car/index": { "packages": ["car-service"] }, "pages/tab-product/index": { "packages": ["product"] }, "pages/tab-finance/index": { "packages": ["finance-circle"] } }, "style": "v2", "sitemapLocation": "sitemap.json" } 与下列错误信息进行修复后给我完整代码;app.json: ["tabBar"]["list"][0]["iconPath"]: "static/tab-home.png" not found ["tabBar"]["list"][0]["selectedIconPath"]: "static/tab-home-active.png" not found ["tabBar"]["list"][1]["iconPath"]: "static/tab-car.png" not found ["tabBar"]["list"][1]["selectedIconPath"]: "static/tab-car-active.png" not found ["tabBar"]["list"][2]["iconPath"]: "static/tab-product.png" not found ["tabBar"]["list"][2]["selectedIconPath"]: "static/tab-product-active.png" not found ["tabBar"]["list"][3]["iconPath"]: "static/tab-finance.png" not found ["tabBar"]["list"][3]["selectedIconPath"]: "static/tab-finance-active.png" not found ["tabBar"]["list"][4]["iconPath"]: "static/tab-mine.png" not found ["tabBar"]["list"][4]["selectedIconPath"]: "static/tab-mine-active.png" not found
07-08
<template> <div class="pages"> <div style="margin: 0 240px"> <audio-view :key="currentQuestion?.audiourl" :autoplay="false" ref="audioPlay" :directionRow="true" :audiourl="currentQuestion?.audiourl || ''" ></audio-view> <div style="padding-bottom:115px"> <div class="topic" v-if="currentQuestion && currentQuestion.sub"> <div style="margin-bottom:10px" v-for="(tItem, tIndex) in currentQuestion.sub || []" :key="tIndex"> <div class="topicTitle">{{ tItem.subject }}</div> <div class="topicCon" v-for="subItem in getAnswerOptions(tItem)" :key="subItem.key" @click="selectOption(tItem, subItem.value)"> <span class="optionBox" :class="[{ 'optionBoxActive': tItem.student_answer === subItem.value && tItem.againSelect }]"></span> {{ subItem.value }} </div> </div> </div> </div> </div> <div class='pages-bottom'> <div style="margin-right: 240px;display: flex;"> <div class="back" @click="showInstruction">返回</div> <div class="number"> <span class="in-progress">{{ currentIndex + 1 }}</span> <span>/</span> <span class="total-number">{{ questions.length }}</span> </div> <div class="next" @click="nextQuestion">{{currentIndex >= questions.length - 1 ? '提交练习' : '下一题'}}</div> </div> </div> <el-dialog title="" v-model="visible" modal-class="back-dialog" :close-on-click-modal="false" :append-to-body="true" :show-close="false" :close-on-press-escape="false" top="20vh" @close="visible = false"> <div class="dialog-title">提示</div> <div class="dialog-main"> <img class="dialog-main-img" src="@/assets/images/tasklist/expression-icon.png" alt=""> <div> <div class="dialog-main-text">学如逆水行舟啊,少侠!</div> <div style="height: 10px;"></div> <div class="dialog-main-text">若退出,进度将不能保存,真的要退出吗?</div> </div> </div> <template v-slot:footer> <div class="footer"> <div class="confirm" @click="goBack">是,挥泪告别</div> <div class="cancel" @click="handleCancel">算了,手滑了</div> </div> </template> </el-dialog> </div> </template> <script setup> import { ref, onMounted, computed } from 'vue' import { useRouter, useRoute } from 'vue-router' import AudioView from '@/components/common/AudioView.vue' import { getHearingTest } from '@/services/HomeWork' import { ElLoading } from 'element-plus' import config from '@/config' // import { useUserInfoStore } from '@/store/user' // const userInfoStore = useUserInfoStore() const router = useRouter() const route = useRoute() const appId = config.APP_ID const session = localStorage.getItem('session') const userCode = localStorage.getItem('userCode') const currentHomeworkMsg = JSON.parse(localStorage.getItem('zyItem')) const dayId = route.query.id const source = route.query.source const audioPlay = ref([]) // 音频组件ref const visible = ref(false) const questions = ref([{ sub: [] }]) const currentIndex = ref(0) // 当前题目索引 onMounted(() => { getDetail() }) const getAnswerOptions = (item) => { return item.options || [] } const selectOption = (tItem, value) => { tItem.student_answer = value tItem.againSelect = true } // 获取当前题目 const currentQuestion = computed(() => questions.value[currentIndex.value]) const getDetail = () => { const loadingInstance = ElLoading.service({ lock: true, text: '加载中...', target: 'content' }) getHearingTest(appId, userCode, session, dayId, currentHomeworkMsg.task_type, source) .then(res => { questions.value = res.task_test || [] }) .catch(error => { console.log('error', error) if (error) { if (error.data) { console.log(error.data) } } }).finally(() => { // 关闭加载动画 loadingInstance.close() }) } // 下一题 const nextQuestion = () => { if (currentIndex.value < questions.value.length - 1) { currentIndex.value++ } } const showInstruction = () => { visible.value = true } const goBack = () => { visible.value = false router.go(-1) } const handleCancel = () => { visible.value = false router.push( { path: '/resultPage' } ) } </script> <style scoped> .pages { height: calc(100vh - 60px); padding:30px; background: #EEF7F7; position: relative; overflow-y: auto; } .pages::-webkit-scrollbar { width: 9px; height: 9px; margin-left: 10px; background-color: #f5f5f5; } /*定义滚动条轨道 内阴影+圆角*/ .pages::-webkit-scrollbar-track { box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3); -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3); border-radius: 10px; background-color: #f5f5f5; } /*定义滑块 内阴影+圆角*/ .pages::-webkit-scrollbar-thumb { border-radius: 10px; box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1); -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1); background-color: #c8c8c8; } .topic { background: #FFFFFF; box-shadow: 0px 6px 10px 0px rgba(233,243,243,0.8); border-radius: 16px; padding-top: 22px; padding-bottom: 20px; margin-top: 30px; } .topicTitle { font-family: 'myPingFang'; font-weight: 600; font-size: 16px; color: #000000; line-height: 22px; text-align: left; font-style: normal; margin-left: 27px; margin-bottom: 20px; } .optionBox { display: inline-block; width: 16px; height: 16px; border: 1px solid #E8E8E8; border-radius: 50%; margin-right: 24px; vertical-align: middle; } .topicCon:hover { color: #4DC34D; } .topicCon { margin-left: 29px; margin-bottom: 20px; font-family: 'myPingFang'; font-weight: 500; font-size: 14px; color: #666666; line-height: 20px; text-align: left; font-style: normal; cursor: pointer; } .topic .topicCon:last-child { margin-bottom: 0; } .topic .topicCon:hover { color: #4DC34D; } .optionBoxActive { display: inline-block; width: 16px; height: 16px; border: 1px solid #E8E8E8; border-radius: 50%; margin-right: 24px; background-image: url('@/assets/images/common/icon_select.png'); background-repeat: no-repeat; background-size: cover; } .pages-bottom { width: 100%; position: fixed; display: flex; justify-content: flex-end; bottom: 0; left: 0; padding:29px 0 19px 0; z-index: 9; background: #FFFFFF; } .back { width: 110px; height: 40px; background: linear-gradient( 180deg, #89E289 0%, #4DC34D 100%); border-radius: 20px; opacity: 0.35; font-family: 'myPingFang'; font-weight: 500; font-size: 15px; color: #FFFFFF; line-height: 40px; text-align: center; font-style: normal; margin-right: 75px; cursor: pointer; } .number { height: 40px; margin-right: 78px; line-height: 40px; } .in-progress { font-family: 'myPingFang'; font-weight: 600; font-size: 18px; color: #333333; text-align: center; font-style: normal; display: inline-block; margin-right: 2px; } .total-number { font-family: 'myPingFang'; font-weight: 400; font-size: 14px; color: #333333; text-align: center; font-style: normal; } .next { width: 110px; height: 40px; background: linear-gradient( 180deg, #89E289 0%, #4DC34D 100%); border-radius: 20px; font-family: 'myPingFang'; font-weight: 500; font-size: 15px; color: #FFFFFF; line-height: 40px; text-align: center; font-style: normal; cursor: pointer; } .dialog-title { height: 33px; font-weight: 500; font-size: 24px; color: #000000; line-height: 33px; text-align: left; font-style: normal; margin-left: 21px; background-image: url('@/assets/images/tasklist/border-icon.png'); background-size: 82px 6px; background-position: left bottom; background-repeat: no-repeat; margin-bottom: 39px; } .dialog-main { margin-left: 21px; margin-bottom: 72px; display: flex; } .dialog-main-img { width: 25px; height: 25px; margin-right: 9px; } .dialog-main-text { font-family: 'myPingFang'; font-weight: 500; font-size: 18px; color: #000000; line-height: 15px; text-align: left; font-style: normal; } .border-icon { width: 82px; height: 6px; margin-left: 21px; } .footer { display: flex; justify-content: flex-end; margin-right: 30px; } .confirm { width: 120px; height: 40px; border-radius: 20px; border: 1px solid; font-weight: 500; font-size: 15px; color: #4DC34D; line-height: 40px; text-align: center; font-style: normal; font-family: 'myPingFang'; } .cancel { width: 120px; height: 40px; background: linear-gradient( 180deg, #89E289 0%, #4DC34D 100%); border-radius: 21px; font-weight: 500; font-size: 15px; color: #FFFFFF; line-height: 40px; text-align: center; font-style: normal; margin-left: 30px; } </style> <style> .back-dialog .el-dialog { width: 625px; height: 300px; background: #FFFFFF; border-radius: 36px; }</style> 为什么currentQuestion一进页面能打印出来但是dom结构渲染不出来,如何有效解决该问题
11-18
内容概要:本文介绍了一个基于MATLAB实现的无人机三维路径规划项目,采用蚁群算法(ACO)与多层感知机(MLP)相结合的混合模型(ACO-MLP)。该模型通过三维环境离散化建模,利用ACO进行全局路径搜索,并引入MLP对环境特征进行自适应学习与启发因子优化,实现路径的动态调整与多目标优化。项目解决了高维空间建模、动态障碍规避、局部最优陷阱、算法实时性及多目标权衡等关键技术难题,结合并行计算与参数自适应机制,提升了路径规划的智能性、安全性和工程适用性。文中提供了详细的模型架构、核心算法流程及MATLAB代码示例,涵盖空间建模、信息素更新、MLP训练与融合优化等关键步骤。; 适合人群:具备一定MATLAB编程基础,熟悉智能优化算法与神经网络的高校学生、科研人员及从事无人机路径规划相关工作的工程师;适合从事智能无人系统、自动驾驶、机器人导航等领域的研究人员; 使用场景及目标:①应用于复杂三维环境下的无人机路径规划,如城市物流、灾害救援、军事侦察等场景;②实现飞行安全、能耗优化、路径平滑与实时避障等多目标协同优化;③为智能无人系统的自主决策与环境适应能力提供算法支持; 阅读建议:此资源结合理论模型与MATLAB实践,建议读者在理解ACO与MLP基本原理的基础上,结合代码示例进行仿真调试,重点关注ACO-MLP融合机制、多目标优化函数设计及参数自适应策略的实现,以深入掌握混合智能算法在工程中的应用方法。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值