LeetCode--Array--Remove Element && Search Insert Position(Easy)
27. Remove Element (Easy)# 2019.7.7
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 in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.
solution##
class Solution {
public int removeElement(int[] nums, int val) {
int newlength = 0;
for (int i = 0 ; i < nums.length; i++)
{
if (nums[i] != val)
nums[newlength++] = nums[i];
}
return newlength;
}
}
总结##
此题很水,没什么技术含量,不做过多解释!!
35. Search Insert Position (Easy)#2019.7.7
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
solution##
class Solution {
public int searchInsert(int[] nums, int target) {
for (int i = 0; i < nums.length; i++)
{
if (nums[i] >= target)
return i;
}
return nums.length;
}
}
总结##
此题很水,没什么技术含量,同样不做过多解释!!
LeetCode--Array--Remove Element && Search Insert Position(Easy)的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- LeetCode(35) Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode记录之35——Search Insert Position
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...
- LeetCode_35. Search Insert Position
35. Search Insert Position Easy Given a sorted array and a target value, return the index if the tar ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
随机推荐
- SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...
- intellij idea 设置用真机测试android
android自带的模拟器是不容置疑的慢,genymontion虽然快,但是觉得有点怪的感觉,哈哈,其实这些都不是重点. 之前是用myeclipse开发android的,虽然一直很想用eclipse来 ...
- 聊聊Disruptor 和 Aeron 这两个开源库
Disruptor The best way to understand what the Disruptor is, is to compare it to something well under ...
- PDF各种骚操作如何用python实现
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: wLsq PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- Linux提权-suid提权
0x1 suid概念 通俗理解为其他用户执行这个程序是可以用该程序所有者/组的权限 0x2 suid提权 简单理解为,一个文件有s标志(权限中包含s),并且对应的是root权限,那么当运行这个程序时就 ...
- file_put_contens小trick
file_put_contents tricks 0x01 trick1 来自于P神的实例: <?php $text = $_GET['text']; if(preg_match('[<& ...
- redis: 乐观锁(十)
监视:watch 正常业务(单线程): 127.0.0.1:6379> set money 100 #模拟存款100元 OK 127.0.0.1:6379> set moneyout 0 ...
- C# XML相关操作
XML是一种意见单文本格式存储数据的方式,这意味着它可以被任何计算机读取.XML中完整的数据集就是XML文档. 在名称空间System.Xml下面大部分类型都是用来支持DOM处理模型的.其中很多类型配 ...
- mysql插入数字都变成2147483647的解决方法
2147483647是int类型的最大值,所以插入11位的数字都会变成2147483647,把int改为bigint即可
- webpack4 图片加载
图片处理(file-loader) 引用时出现的问题 在js中引入图片并添加到页面 let img = new Image(); img.src = './logo.png' document.bod ...