leetCode 27.Remove Element (删除元素) 解题思路和方法
2015-07-07 20:32
579 查看
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.
思路:此题和26题一脉相承,算法上不难,具体如代码所示:
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.
思路:此题和26题一脉相承,算法上不难,具体如代码所示:
public class Solution { public int removeElement(int[] nums, int val) { int len = nums.length; int tempLen = len; int step = 0;//每个元素需要向前转移的距离 for(int i = 0; i < len; i++){ if(nums[i] == val){ step++;//若相等步长+1 tempLen--;//每一个相等的元素长度减少1 }else{ nums[i-step] = nums[i];//元素前移n个步长 } } return tempLen; } }
相关文章推荐
- Wildcard Matching
- The Windy's - POJ 3686 KM算法
- 第4章 更多的 bash shell命令 - df、du、sort、grep、zip、tar等
- Binary Search Tree Iterator
- 密码学基础知识(六)Hash函数与消息认证
- 总结eclipse中安装maven插件
- 自己练习写的Procedure,两种方法遍历cursor,代码已验证
- select、poll、epoll之间的区别总结
- 世纪互联云和华为共同打造的数据中心是一个很好的一步标志!
- Python快速教程 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢! 怎么能快速地掌握Python?这是和朋友闲聊时谈起
- 啊哈!图的广度优先遍历
- 如何在Linux下实现设备的配置(上)
- JSP整理总结--JSP指令、动作标签
- php 上传文件失败$_FILES tmp_name 为空
- matlab神经网络 -基础入门 视频1
- mysql 字符串转换为日期 日期格式化
- TCP协议与UDP协议的区别
- HADOOP基本操作命令
- How do I reset Windows Update components?
- C#调用WebService实例和开发