[LeetCode]习题3
来源:互联网 发布:怎样可以联系淘宝店主 编辑:程序博客网 时间:2024/05/16 11:56
[LeetCode]习题
首先贴上这次LeetCode题的链接https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
根据题目的要求在一个已排序的数组中找出不同的元素的个数且不许创建新的数组,而原始数组能不能改动题目没说所以我们可以改动它。因为数组已经排序了所以我们可以使用遍历数组的方法,设定一个用于比较不同的位置,该位置与之前的位置的元素的值都不同,这样我们最后只要范围这个位置+1就是答案了。因此我们令I = 0,然后从j = 1开始遍历数组,当i位置与j位置相同时,j直接+1,当i与j位置不同时把j的位置的值赋给i+1位置上,就实现了i位置前面的值都不同。最后返回i+1就是答案。
附上代码
class Solution { public int removeDuplicates(int[] nums) { int i = 0; for(int j = 1;j<nums.length;j++){ if(nums[i]!=nums[j]){ i++; nums[i] = nums[j]; } } return i+1; }}
阅读全文
0 0
- Leetcode习题:3Sum
- [LeetCode]习题3
- leetcode习题解答: 15. 3Sum
- leetcode习题解答:16. 3Sum Closest
- Leetcode习题: SingleNumber II
- Leetcode习题: SingleNumber II
- Leetcode习题: Two Sum
- Leetcode习题:Distinct Subsequences
- [LeetCode]习题1
- [LeetCode]习题2
- [LeetCode]习题4
- [LeetCode]习题5
- Leetcode习题(1)
- Leetcode习题记录——3Sum Closest
- LeetCode习题记录(3)Longest Substring Without Repeating Characters
- 第3章 栈和队列综合习题(leetcode+vjudge)
- Leetcode习题:Divide Two Integers
- Leetcode习题-Reverse Integer
- java保留两位小数4种方法
- js中call用法
- 【转】web测试常用的用例及知识(全)
- 2017大二第十一~十三周学习笔记
- 字符串匹配的KMP算法
- [LeetCode]习题3
- 异常数据点检测
- centos的硬盘操作日记
- 动画播放问题(动画播放完在执行后面的程序)
- 搜狐畅游测试开发一面试题..
- ActiveMq 集群部署 三种方案 + 负载均衡+其他细节点
- 基于DNN的semi-supervised learning
- ADB命令备份
- Docker:初识docker及工具介绍(一)