`
随便小屋
  • 浏览: 102674 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Leetcode-27-Remove Element

 
阅读更多

Remove Element

 

Given an array and a value, remove all instances of that value in place and return the new length.

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

题目解读:

给定一个数组和一个元素,删除该数组中所有的次元素并且返回新的长度,数组中元素的位置可以改变。

解析:两种解决方法

解法一:按照最常规的方法,从头到尾遍历整个数组,删除数组中所有元素value并且将后面的数据元素向前移动,记录数组元素中value的个数。

解法二:解法二是在解法一的基础之上,从数组两端进行进行遍历数组,将后面非value值移到前面value值所在的位置,并记录删除value值得个数。假如数组为[1,2.3,4,5,6],value=2,则按照如下方式进行删除。分别定义lowhigh指向数组的两端,从low端向后移,当low指向2的时候,如果此时high不等于2,则将high所指向的值赋值给low所在的位置,high--,low++。直到high==low为止。

 



 解法一代码:

public static int removeElement(int[] nums, int val) {
        int k=0;
        for (int i=0; i<nums.length; i++) {
        	if(nums[i] == val) {
        		k++;
        		continue;
        	}
        	if(i-k>=0)
        		nums[i-k] = nums[i];
        }
        return nums.length-k;

    }

 

解法二代码:

	public static int removeElement(int[] nums, int val) {
		//数组长度size
		int size = nums.length;
		int low =0;
		int high = size-1;
		
		//记录数组中val的个数
		int count = 0;
		while(low <= high) {
			
			if(nums[high] == val) {
				count ++;
				high --;
				continue;
			} 
			if(nums[low] == val) {
				count ++;
				nums[low]=nums[high];
				low ++;
				high --;
			} else {
			    low ++;
			}
		}
		return size-count;
	}

 

分享到:
评论

相关推荐

    leetcode双人赛-leetcode-solution:没事可做的时候,就来刷刷题吧

    leetcode双人赛 ...remove-element 搜索插入位置 search-insert-position 最大子序和 maximum-subarray 加一 plus-one 合并两个有序数组 merge-sorted-array 杨辉三角 pascals-triangle 杨辉三角 II pa

    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答案-LeetCode-practice:记录在leetcode练习的代码&总结

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

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last Position of Element in ...

    gasstationleetcode-leetcode-rust:莱特代码休息

    加油站 leetcode 力码锈 问题 # 标题 命令 1 cargo run --bin 1-two-sum 2 cargo run --bin 2-add-two-numbers 3 cargo run --bin 3-longest-substring-without-repeating-characters ...27 ...27-remove-element 28

    圆和矩形是否重叠leetcode-leetcode_solutions:leetcode_solutions

    27.Remove Element -&gt; 两个指针,前后,交换268.Missing Number -&gt; 数字之和 [0,1,2,...,n] 是一个常数,所以可以计算出缺失的那个169.Majority Element -&gt; Hashtable | Boyer-Moore 多数投票算法283. 移零 -&gt; 27. ...

    2sumleetcode-leetcode:leetcode

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

    Leetcode-Algorithm-Exercise

    1_TwoSum 9_PalindromeNumber 13_RomanToInteger 14_LongestCommonPrefix 20_ValidParentheses 21_MergeTwoSortedLists 26_RemoveDuplicatesFromSortedArray 27_RemoveElement 28_ImplementStrStr() 35_Search...

    LeetCode-1:LeetCode题集

    这是我在的刷题集,详情参见__注释__。 未完待续!!! 已完成 27. Remove Element 14. Longest Common Prefix 9. Palindrome Number 7. Reverse Integer 1. Two Sum License The MIT License (MIT)

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

    leetcode2-Algorithms-Practice:创建此repo是为了跟踪我在解决问题方面的进展

    leetcode 2 LeetCode-练习 我的 Leetcode“解决方案”(在解决方案/文件夹中)来解决 leetcode 问题。 它用于练习和跟踪进度不是 100% 优化的。 我的账户链接 问题名称 ...Remove ...27. Remove Element

    leetcode1004-leetcode:leetcode

    27. Remove Element (E) 31. Next Permutation (M) * -&gt; index 주의, 부등호 하나 틀림 33. Search in Rotated Sorted Array (M) * -&gt; 부등호 주의, 부등호 하나 틀림 34. Find First and Last Position of Element ...

    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 27. 移除元素

    题目来源:https://leetcode-cn.com/problems/remove-element 题目 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于val 的元素,并返回移除后数组的新长度。 不要使用额外的数组空间,你必须仅使用 ...

    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叫数-leetcode:leetcode

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

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

    27 移除元素 | Remove Element 链表 Linked List 力扣 203 移除链表元素 | Remove Linked List Elements 力扣 206 反转链表 | Reverse Linked List 队列 Queue 力扣 933 最近的请求次数 | Number of Recent Calls ...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays ...Remove ...27. Remove Element 28. Implement strStr() 3

    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()操作...

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

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

Global site tag (gtag.js) - Google Analytics