leetcode 27. Remove Element(C语言,快速排序思想,剔除数组中与目标值相等的数)20
来源:互联网 发布:自己朗读软件 编辑:程序博客网 时间:2024/04/27 10:16
贴原题:、
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.
解析:
本题是给出一个数组和一个目标值,让把数组中与目标值相等的数都剔除,并返回剔除后的数组的大小。
我利用了快速排序的思想,我之前在写另一道leecode的时候便自己写过快速排序,那么写这道题的时候自然是轻车驾熟。我只要把与目标值相等的数排序到数组的最后,然后返回不与目标值相等的数量便可以了。
贴出我之前写的快速排序算法:
http://blog.csdn.net/m0_37454852/article/details/78067025
http://blog.csdn.net/m0_37454852/article/details/78062540
贴代码:
int removeElement(int* nums, int numsSize, int val) { if(!numsSize)//空数组则直接返回0 { return 0; } int i=0, j=numsSize-1; while(i!=j) { if(*(nums+i)==val)//若该数与目标值相等,则交换该数与最后一位 { int temp=*(nums+i); *(nums+i)=*(nums+j); *(nums+j)=temp; j--;//使最后一位前进一步 } else//否则继续判断下一个数 { i++; } } //跳出循环后,j之前的数都!=val,其后的数都==val if(*(nums+j)==val)//判断最后的那一位数 { j--; } return j+1;}
- leetcode 27. Remove Element(C语言,快速排序思想,剔除数组中与目标值相等的数)20
- leetcode 1 Two Sum(在无序数组中找两个数之和与目标值相等)
- 27. Remove Element(移除数组中与指定值相等的元素)
- leetcode 1 Two Sum(在无序数组中找两个数之和与目标值相等,两种方法)
- leetcode 27 Remove Element(移除数组中的与val相等的元素)
- 18.四数相加与目标值相等的组合
- [leetcode]Remove Element C语言
- Leetcode c语言-Remove Element
- [leetcode] 【数组】27. Remove Element
- c++——将数组中重复的数剔除
- 【C++】【LeetCode】27. Remove Element
- LeetCode-27-Remove Element(消除数组中给定的元素)
- [leetcode-27]Remove Element(C语言)
- [C语言][LeetCode][27]Remove Element
- 利用快速排序思想找出数组中第K大的数
- LeetCode Remove Element 快速删除数组中的某一值
- 【LeetCode算法练习(C++)】Remove Element
- LeetCode_Easy心得:27. Remove Element(C语言)
- 《C++ Concurrency in Action》笔记27 基于锁的并行数据结构
- Java编程思想读书笔记——访问权限控制
- VS添加第三方库的配置方法及相对路径介绍
- Vision_数据结构_RMQ
- rabbitmq 结合php的安装配置使用
- leetcode 27. Remove Element(C语言,快速排序思想,剔除数组中与目标值相等的数)20
- 收藏一个JS中for循环机制的文章
- JavaScript数据结构与算法--开篇立意
- leetcode 318. Maximum Product of Word Lengths 位向量 + 交集
- java
- 49. Group Anagrams
- Java for Web学习笔记(八一):RESTful(1)设置Rest Context
- 反射与内省_JavaBean与内省
- tagName nodeName