LeetCode算法入门- Search Insert Position -day19

LeetCode算法入门- Search Insert Position -day19

  1. 题目描述
    Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0

  1. 题目分析
    利用二分法来进行查找

  2. Java实现

class Solution {public int searchInsert(int[] nums, int target) {if(nums == null || nums.length == 0)return 0;int left = 0;int right = nums.length - 1;int mid = 0;while(left <= right){//二分法mid = (left + right)/2;if(nums[mid] == target){//这里是返回midreturn mid;}else if(nums[mid] > target){right = mid - 1;}else{left = mid + 1;}}//记得最后返回left而不是midreturn left;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558849.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux——VMware虚拟机安装CentOS步骤

一、下载CentOS.iso镜像 最地道的下载方式就是通过官网&#xff0c;大多数的网上连接会直接抛出网易、华为的镜像连接&#xff0c;实际上这些连接都可以在官网找到&#xff1a; 官网地址&#xff08;可直接百度搜索CentOS&#xff09;&#xff1a;https://www.centos.org/ 1…

LeetCode算法入门- Remove Element -day20

LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array i…

Spring Boot——Redis安装配置与应用整合

引言 Spring Boot默认以ConcurrentHashMap作为缓存容器&#xff0c;但默认的缓存容器在简单的场景使用还是可以的&#xff0c;而作为NoSQL的代表&#xff0c;Redis可以做内存数据库、消息中间件都是不错的&#xff0c;而且有RedisDesktopManager作为可视化管理工具&#xff0c…

利用Aria2高速下载网盘文件

利用Aria2高速下载网盘文件 方法步骤&#xff1a; 下载文件 解压arial2&#xff0c;运行aria2启动.VBS添加插件&#xff0c;解压BaiduExporter-master.zip在Google浏览器扩展程序中chrome://extensions加载已经解压的扩展程序 选择BaiduExporter进行添加即可&#xff0c;打开…

MySQL——JSON_REPLACE()函数修改JSON属性值

引言 由于对mysql的函数并不了解&#xff0c;之前遇到了一个场景&#xff1a; mysql表中有一个字段res_content 是一个由longtext类型&#xff08;可以理解为一个更长的varchar&#xff09;保存的巨大的JSON对象&#xff0c;但是&#xff0c;由于录入的疏忽&#xff0c;导致这…

LeetCode算法入门- Remove Duplicates from Sorted Array -day21

LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do …

Spring Boot整合Redis——自定义RedisSerializer

引言 spring boot简单引入redis依赖&#xff0c;并使用RedisTemplate进行对象存储时&#xff0c;需要使存储对象实现Serializable接口&#xff0c;这样才能够成功将对象进行序列化。 RedisTemplate默认使用的序列化机制是JdkSerializationRedisSerializer&#xff0c;但实际开…

LeetCode算法入门- Implement strStr() -day22

LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack “hello”, needle “ll” Output: 2 Example …

交易系统如何确保账簿100%准确

转自廖雪峰老师的《交易系统如何确保账簿100%准确》 这篇文章阐述了一个交易系统中对账功能的关键&#xff0c;即&#xff1a;时刻保证资产负债表总额始终为 0。 交易系统中&#xff0c;对账是一个大问题。对账处理不好&#xff0c;不但需要花费大量的人力去处理账簿&#xff…

通俗易懂的SpringBoot教程---day1---Springboot入门教程介绍

通俗易懂的SpringBoot教程—day1—教程介绍 教程介绍&#xff1a; 初级教程&#xff1a; 一、 Spring Boot入门 二、 Spring Boot配置 三、 Spring Boot与日志 四、 Spring Boot与Web开发 五、 Spring Boot与Docker&#xff1a;Docker容器 六、 Spring Boot与数据访问&#x…

重装win11 23H2系统步骤及错误总结

1.分出大约13GB 的启动盘&#xff0c;下载win11 mul映像并解压进去 &#xff08;记得下载电脑相应的网卡驱动&#xff01;&#xff08;只要等下进入桌面要用&#xff09;&#xff09; 2.利用easybcd&#xff0c; 到add选项&#xff0c;点击Win1PE&#xff0c;命名启动项&…

Java 8中获取参数名称

本文转自廖雪峰老师的&#xff1a;《在Java 8中获取参数名称》 在Java 8之前的版本&#xff0c;代码编译为class文件后&#xff0c;方法参数的类型是固定的&#xff0c;但参数名称却丢失了&#xff0c;这和动态语言严重依赖参数名称形成了鲜明对比。现在&#xff0c;Java 8开始…

通俗易懂的SpringBoot教程---day2---Springboot配置文件

通俗易懂的SpringBoot教程—day2—Springboot配置文件 1、配置文件 SpringBoot使用一个全局的配置文件&#xff0c;配置文件名是固定的&#xff1b; •application.properties •application.yml 配置文件的作用&#xff1a;修改SpringBoot自动配置的默认值&#xff1b;Spring…

英语中数字表达总结

引言 对于英语中千、万、亿的表达法&#xff0c;始终是我英语路上的一个痛点&#xff0c;今天就来好好总结一下这东西的规律。 一句话总结 阅读和理解层面&#xff08;99%的使用情况&#xff09;&#xff1a;thousand后面跟 3 个 0 &#xff0c;million后面跟 6 个 0 &#…

Kibana 的安装(Windows版本)新手入门

Kibana 的安装&#xff08;Windows版本&#xff09;新手入门 参考博文&#xff1a;https://blog.csdn.net/weixin_34727238/article/details/81200071 目录 什么是Kibana? Kibana 6.3.1安装条件 JDK的安装 node的安装 Elasticsearch的安装 Kibana 的安装 什么是Kibana?…

价值50万年薪的Java面试题

《Java面试题全集&#xff08;上&#xff09;》 《Java面试题全集&#xff08;中&#xff09;》 《Java面试题全集&#xff08;下&#xff09;》 《关于Java并发编程的总结和思考》 《面试编程题拾遗&#xff08;01&#xff09; --- 不用算术运算符完成两个数求和》 《面试…

Could not resolve host: 'localhost 报错解决办法

Could not resolve host: localhost 报错解决办法 面向Windows的&#xff1a; 零基础的我一直卡在这一步骤下&#xff1a; 首先要先在Windows安装curl&#xff1a;安装方式参考&#xff1a;https://blog.csdn.net/weixin_41986096/article/details/86646365 按照完之后&…

当面试官问我————为什么String是final的?

面试官&#xff1a;你好&#xff0c;能看得清下面这张图吗&#xff1f; 我&#xff1a;可以的。 面试官&#xff1a;恩&#xff0c;好的。呃&#xff0c;你能不能说一说为什么String要用final修饰&#xff1f; 我&#xff1a;final意味着不能被继承或者被重写&#xff0c;Str…

当面试官问我————Java是值传递还是引用传递?

面试官&#xff1a;你好&#xff0c;你能说出下面个程序的执行结果吗&#xff1f; public class Test {public static void main(String[] args) {String name "Scott";int age 5;User user new User();user.setName(name);user.setAge(age);System.out.println(…

ubuntu系统下Jenkins和tomcat的安装与配置

ubuntu 安装 JDK ubuntu的安装我们采取最简单的方式安装 直接用apt-get的方式 sudo apt-get install openjdk-8-jdk 安装器会提示你同意 oracle 的服务条款,选择 ok 然后选择yes 即可 ubuntu 安装tomcat8 通过apt安装 tomcat8 sudo apt-get install tomcat8 tomcat8-docs t…