`
frank-liu
  • 浏览: 1666092 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: Remove Element

 
阅读更多

问题描述:

Given an array and a value, 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 in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

原问题链接:https://leetcode.com/problems/remove-element/

 

问题分析

  这个问题需要的是移除给定数组里一个特定的值。和前一个问题类似,用两个数值一个表示当前遍历到的索引,一个用来记录保存当前合法值的索引。具体的实现如下:

 

public class Solution {
    public int removeElement(int[] nums, int val) {
        if(nums == null || nums.length == 0) return 0;
        int i, j = 0;
        for(i = 0, j = 0; i < nums.length; i++) {
            if(nums[i] != val) nums[j++] = nums[i];
        }
        return j;
    }
}

 

 

 

1
4
分享到:
评论
1 楼 shihengli2010 2017-06-20  
厉害厉害 mmm

相关推荐

    leetcode1004-leetcode:leetcode

    leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...

    leetcode提交记录怎么看-LeetCode:力码

    removeElement(vector&lt;int&gt;& nums, int val) { int len = 0; for (int i = 0; i &lt;= nums.size() - 1; ++i) { if (nums[i] != val) { nums[len++] = nums[i]; } } return len; } }; 报错原因: vector的size()操作...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode 力码 LeetCode 问题的解决方案。 个人资料在这里:。 简单的 binary_search.py​​: ...remove_element.py: reverse_vowels.py: 千位分隔符.py: transpose_matrix.py: trim_bst.py: two

    leetcode算法题主函数如何写-leetcode:leetcode

    element appear only once 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. Example: Given nums = ...

    leetcode答案-LeetCode:Swift中的LeetCode

    leetcode ...Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 Climbing Stairs Easy #83 Remove Duplicates from Sorted L

    leetcode叫数-leetcode:leetcode

    Element 这题用ruby简直是开挂。一个Array#delete方法即解决问题 Runtime: 84 ms, beats 33.33% 更新:结果效率就炸了,只有33.33%,后来查了下文档和源码,发现原因是因为delete的实现是一个遍历中做判断,如果等于...

    2sumleetcode-leetcode:leetcode

    removeelement.cpp,删除特定元素 removeup.cpp,从排序数组中删除重复项 removeup2.cpp,从排序数组中删除重复项,重复项最多分配两次 pascaltriangle.cpp, 给定行数生成一个帕斯卡三角形 pascaltriangle2.cpp, ...

    leetcode双人赛-Leetcode:leetcode中问题的解答

    leetcode双人赛力码 你可以在leetcode中找到一些问题的答案,你可以在leetcode中搜索问题名称,然后就会找到解决方案的代码 leetcode 链接 如果你对我的 leetcode 个人资料感兴趣,你可以去 ...removeElement(num

    LeetCode:LeetCode题解

    Remove_Element Java 简单的 75 排序颜色 Java 中等的 80 Remove_Duplicates_From_Sorted_Array_II Java 中等的 88 合并排序数组 Java 简单的 121 Best_Time_To_Buy_And_Sell_Stock Java 简单的 122 Best_...

    leetcode:leetcode回购

    密码 leetcode回购 树 - - - - 杂凑 - - 二元搜寻 0004_findMedianSortedArrays - SLN 0033_search - SLN 0034_searchRange - SLN ... 0027_removeElement - SLN 种类 0031_nextPermutation - SLN

    leetcode跳跃-LeetCode:力扣刷题70道!

    leetcode 跳跃 LeetCode 力扣刷题70道! 数组 Array 力扣 485 最大连续1的个数 | Max Consecutive One 力扣 283 移动零 | Move Zeroes 力扣 27 移除元素 | Remove Element 链表 Linked List 力扣 203 移除链表元素 ...

    leetcode答案-LeetCode:LeetCode解决方案作者:LiYiji。可能不是最好的,仅供参考。README位于本页底部

    leetcode 答案 #LeetCode LeetCode solution By Li Yiji. Maybe not ...每个cpp文件的文件名中前边的数字代表着我做题的顺序,与题目本身、LeetCode网站等都没有关系 ...Remove Element 我的解法不太好,而且不严密

    lrucacheleetcode-LeetCode:这个库用于总结leetcode中遇到的习题,期望按照数据结构和常用方法分成2类,进行总结,

    lru cache leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 ...Element 12 Next Permutation 公式 13 Permutation Sequence 公式 14 Valid Sudoku 15 Trapping Rain W

    leetcode530-LeetCodeKotlin:LeetCode的Kotlin解决方案

    Easy_27_RemoveElement :check_mark_button: Med_209_MinSizeSubArray :check_mark_button: Med_325_MaxSizeSubArray :check_mark_button: Med_560_SubArrayEqualK :check_mark_button: Med_523_...

    LeetCode最全代码

    27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) [Python](./Python/remove-element.py) | _O(n)_ | _O(1)_ | Easy || 31 | [Next Permutation]...

    gasstationleetcode-LeetCode_Practice:我的LeetCode练习从2020年开始

    27_Remove_element 26_Remove_Duplicates 80_Remove_Duplicates_From_Sorted_Array_II 189_Rotate_array 41_First_Missing_Positive 299_Bulls_and_Cows 134_Gas_Station 118_Pascal's_Triangle_I 119_Pascal's_...

    leetcode答案-LeetCode-practice:记录在leetcode练习的代码&总结

    leetcode 答案 LeetCode-practice 记录在leetcode练习的...#27RemoveElement #35SearchInsertPosition 最佳答案未解 Git使用练习 练习下分支切换&合并 解决冲突 master&feature1 禁用fast forward --no-ff 熟悉stash

    leetcode摇摆-Leetcode-Learning:Leetcode-学习

    leetcode摇摆Leetcode-学习 2020/02/02 完成 Q29 & 35 2020/02/05 成品 Q58 split() function: str.split( ) means Space-separated Q69 math.sqrt() 2020/02/11 成品 Q299 Remove all rigth position element and ...

    LeetCode判断字符串是否循环-LeetCodeForCZY:LeetCode的解决方案

    LeetCode判断字符串是否循环 LeetCodeForCZY Solution For LeetCode 题目说明: 1、两数之和:(TwoSum) 给定一个整数数组和一个目标值,找出数组...27、移除元素:(RemoveElement) 给定一个数组 nums 和一个值 val

Global site tag (gtag.js) - Google Analytics