find peak or drop

2024-01-04 11:48
文章标签 find peak drop

本文主要是介绍find peak or drop,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=156077&extra=page%3D2%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3046%5D%5Bvalue%5D%3D2%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311
找打peak or drop,原来用的是递归,这次改为迭代

public static int helper1(int[] nums) {if (nums.length == 0)return -1;int len = nums.length;if (Math.abs(nums[len - 1] - nums[0]) == len - 1)return -1;// There is no such a valley or peakint start = 0, end = nums.length - 1;while (start < end) {int mid = start + (end - start) / 2;if ((nums[mid - 1] - nums[mid]) * (nums[mid + 1] - nums[mid]) > 0)return nums[mid];int diffOfIndex = mid - start;int diffOfValue = Math.abs(nums[mid] - nums[start]);//因为如果不相差,则下标和值是对应的,则没有发生波峰或波谷if (diffOfIndex == diffOfValue)start = mid;elseend = mid;}return -1;}public static void main(String[] args) {int[] nums1 = { 1, 2, 3, 4, 3, 2 };int[] nums2 = {};int[] nums3 = { 1, 2, 3, 4, 5, 6, 7, 8, 7 };int[] nums4 = { 1, 2, 3, 4, 5 };int[] nums5 = { 5, 4, 3, 2, 1, 2, 3 };int[] nums6 = { 9, 8, 7, 6, 7 };System.out.println(find(nums1));System.out.println(find(nums2));System.out.println(find(nums3));System.out.println(find(nums4));System.out.println(find(nums5));System.out.println(find(nums6));System.out.println(helper1(nums1));System.out.println(helper1(nums2));System.out.println(helper1(nums3));System.out.println(helper1(nums4));System.out.println(helper1(nums5));System.out.println(helper1(nums6));}


这篇关于find peak or drop的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/569240

相关文章

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

Linux find 命令完全指南及核心用法

《Linuxfind命令完全指南及核心用法》find是Linux系统最强大的文件搜索工具,支持嵌套遍历、条件筛选、执行动作,下面给大家介绍Linuxfind命令完全指南,感兴趣的朋友一起看看吧... 目录一、基础搜索模式1. 按文件名搜索(精确/模糊匹配)2. 排除指定目录/文件二、根据文件类型筛选三、时间

Rust中的Drop特性之解读自动化资源清理的魔法

《Rust中的Drop特性之解读自动化资源清理的魔法》Rust通过Drop特性实现了自动清理机制,确保资源在对象超出作用域时自动释放,避免了手动管理资源时可能出现的内存泄漏或双重释放问题,智能指针如B... 目录自动清理机制:Rust 的析构函数提前释放资源:std::mem::drop android的妙

MongoDB学习—(6)MongoDB的find查询比较符

首先,先通过以下函数向BookList集合中插入10000条数据 function insertN(obj,n){var i=0;while(i<n){obj.insert({id:i,name:"bookNumber"+i,publishTime:i+2000})i++;}}var BookList=db.getCollection("BookList")调用函数,这样,BookList

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

访问controller404:The origin server did not find a current representation for the target resource

ider build->rebuild project。Rebuild:对选定的目标(Project),进行强制性编译,不管目标是否是被修改过。由于 Rebuild 的目标只有 Project,所以 Rebuild 每次花的时间会比较长。 参考:资料

mybatis错误——java.io.IOException Could not find resource comxxxxxxMapper.xml

在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:

MACS bdgdiff: Differential peak detection based on paired four bedGraph files.

参考原文地址:[http://manpages.ubuntu.com/manpages/xenial/man1/macs2_bdgdiff.1.html](http://manpages.ubuntu.com/manpages/xenial/man1/macs2_bdgdiff.1.html) 文章目录 一、MACS bdgdiff 简介DESCRIPTION 二、用法

Linux 中的 find 命令介绍以及使用

文章目录 Linux 中的 `find` 命令详解及使用示例`find` 命令的基本语法常用的 `find` 命令选项按文件名搜索:`-name`按文件类型搜索:`-type`按文件大小搜索:`-size`按修改时间搜索:`-mtime`按权限搜索:`-perm`按所有者搜索:`-user` 和 `-group` `find` 命令的常见操作删除找到的文件:`-exec` 和 `rm`查找并