首页 > 其他分享 >LeetCode 2903.找出满足差值条件的下标I

LeetCode 2903.找出满足差值条件的下标I

时间:2024-06-04 13:34:42浏览次数:14  
标签:2903 下标 如图所示 nums 差值 等于 大于 LeetCode

1.题目要求如图所示:

由题意可知我们如果要满足差值条件,我们可以使用迭代法,让下标i从零开始,我们再设一个变量j,然后让j等于下标加上indexDifference,再用for循环遍历j,再采用abs(nums[i] - nums[j])是否大于等于valueDifference,如果大于则break;

以上就是代码算法思路

接下来的实操如图所示:

好了,这就是这个题的解了,我这个其实也不算最优解,但如果各位看官看得满意,那就给个免费的赞吧^_^

标签:2903,下标,如图所示,nums,差值,等于,大于,LeetCode
From: https://blog.csdn.net/m0_54244065/article/details/139339195

相关文章

  • Leetcode 151.反转字符串中的单词
    ​此题是非常经典的字符串的颠倒问题,但这个更复杂一些,但也不其本质,我此次写的方式是用双指针问题完成的,虽然算不上什么好方法,但如果各位看官觉得满意的话,请各位给我个点个免费的赞吧,谢谢了_1.题目要求如图所示:2.接下来是做题的步骤:我们先把字符串的颠倒函数写好,如图......
  • Leetcode 313. Super Ugly Number
    ProblemAsuperuglynumberisapositiveintegerwhoseprimefactorsareinthearrayprimes.Givenanintegernandanarrayofintegersprimes,returnthenthsuperuglynumber.Thenthsuperuglynumberisguaranteedtofitina32-bitsignedintege......
  • LeetCode 1748. Sum of Unique Elements
    原题链接在这里:https://leetcode.com/problems/sum-of-unique-elements/description/题目:Youaregivenanintegerarray nums.Theuniqueelementsofanarrayaretheelementsthatappear exactlyonce inthearray.Return the sum ofalltheuniqueelementso......
  • (nice!!!)LeetCode 3097. 或值至少为 K 的最短子数组 II(位运算、滑动窗口)
    3097.或值至少为K的最短子数组II思路:既然求的是区间,那么我们自然就想到前缀和、滑动窗口、双指针。结合本题的特点:或运算,会发现如果一段连续的区间进行或运算,最多只会有32次运算可以改变,这是因为int型的二进制范围是-2^31~2^31-1,每次增加一个二进制形式的1。所......
  • (nice!!!)LeetCode 3067. 在带权树网络中统计可连接服务器对数目(深度优先搜索dfs、树)
    3067.在带权树网络中统计可连接服务器对数目思路:节点数最多1000,那么我们0(n^2)的时间复杂度就ok了。我们可以用一层for循环遍历每一个点i,然后第二层for循环遍历每一条可能的边j,通过用dfs来找到符合“到根节点i的距离可以被signalSpeed整除”的点。不同子节点之间两两组......
  • [leetcode 3171] 解法列表
    线段树解法+二分classSolution{publicintminimumDifference(int[]nums,intk){this.nums=nums;this.n=nums.length;returncheck(k);}publicstaticvoidmain(String[]args){Solutionsolution=newSol......
  • leetcode 377. 组合总和 Ⅳ(dp)
    377.组合总和Ⅳ-力扣(LeetCode)dp,跟完全背包反着来,可以当作是爬楼梯来做,相当于每次爬的楼梯数是从数组种选的。1#defineIOstd::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)2#definebug(x)cout<<#x<<"is"<<x<<endl;3#include<bits/stdc++.h>4usin......
  • Floyd判圈算法 leetcode
    龟兔赛跑/Floyd判圈算法概述判断一个链表是否存在环画图演示两个指针相遇的情况:查找链表中环的首个节点在这里插入图片描述数学公式表示为:(对应力扣142.环形链表II,141.环形链表I)判断一个链表是否存在环龟兔赛跑/Floyd判圈算法转换成判断链表是否存......
  • LeetCode 1168. Optimize Water Distribution in a Village
    原题链接在这里:https://leetcode.com/problems/optimize-water-distribution-in-a-village/description/题目:Thereare n housesinavillage.Wewanttosupplywaterforallthehousesbybuildingwellsandlayingpipes.Foreachhouse i,wecaneitherbuildaw......
  • LeetCode 720. Longest Word in Dictionary
    原题链接在这里:https://leetcode.com/problems/longest-word-in-dictionary/description/题目:Givenanarrayofstrings words representinganEnglishDictionary,return thelongestwordin words thatcanbebuiltonecharacteratatimebyotherwordsin wor......