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 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.

  1. 题目分析:
    这里题目的意思是指返回一个数组,数组里面只有值不等于target的元素,最后返回数组的长度

  2. Java实现

class Solution {public int removeElement(int[] nums , int val) {int index = -1;int len = nums.length;for(int i = 0; i < len; i++){if(nums[i] != val){nums[++index] = nums[i];}}return index  + 1;}
}

LeetCode算法入门- Remove Element -day20相关推荐

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

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  2. LeetCode算法入门- Remove Nth Node From End of List -day17

    LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...

  3. LeetCode算法入门- Longest Substring Without Repeating Characters-day4

    LeetCode算法入门- Longest Substring Without Repeating Characters-day4 Longest Substring Without Repeatin ...

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

    LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...

  5. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  6. LeetCode算法入门- Multiply Strings -day18

    LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...

  7. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

  8. LeetCode算法入门- Merge Two Sorted Lists -day15

    LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述: Merge two sorted linked lists and return it as a n ...

  9. LeetCode算法入门- Compare Version Numbers -day14

    LeetCode算法入门- Compare Version Numbers -day14 题目描述: Compare two version numbers version1 and version2 ...

最新文章

  1. matlab怎么求残余误差,误差理论与数据处理实验报告.doc
  2. 《梦醒黄金城》主创暨明星见面会举办 预计2019年完成拍摄
  3. Cocos2d-x列表嵌套裁剪bug
  4. day07 数据类型间的相互转化及字符编码
  5. new LayoutParams 使用
  6. 完善Library的管理方式
  7. hive 去重 字符串_hive函数
  8. 调研了下 AI 作曲,顺便做了期视频...快进来听歌!
  9. 学习《图解HTTP》有感
  10. 【英语学习】【WOTD】scavenger 释义/词源/示例
  11. java面试算法总结_面试10大算法汇总——Java篇
  12. 存储过程中ORA-01031: insufficient privileges的解决方法
  13. NYOJ ~ 58 ~ 最小步数(DFS)
  14. Android仿IOS有弹性ScrollView
  15. JVM系列之:日志分析工具:GCViewer、VisualVM、GCeasy
  16. Java中使用Atomic*实现原子锁线程安全
  17. 实验二十三——RPL协议仿真实验
  18. Java Audio Video Encoder
  19. TIOBE 2015年6月编程语言排行榜
  20. 可视化神器Plotly绘制热力图

热门文章

  1. 工作中 99% 会用到的 Git 命令
  2. 阿里《Java手册》做一个有技术情怀的人
  3. ReactNative环境搭建扩展篇——安装后报错解决方案
  4. C# Winform 窗体美化(二、LayeredSkin 界面库)
  5. java word转pdf linux_Linux平台中使用PHP把word转pdf的实现方法
  6. 使用Jquery插件bTabs实现多页签打开效果
  7. 用户态和核心态的转换
  8. 矩阵分析理论在实际工程中的应用_【顶管技术在市政给排水工程中的应用分析】...
  9. 小程序中ajax返回数据,微信小程序调用接口返回数据或提交数据
  10. linux vps 运行exe文件夹,在centos环境下运行.exe文件