LeetCode 记录(c#版)
来源:互联网 发布:秦丝生意通 mac os 编辑:程序博客网 时间:2024/06/07 10:27
LeetCode 记录(c#版)
- 26. Remove Duplicates from Sorted Array(Tag—-Array)
原题:
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.
求已排序数组的长度:
遍历长度为n的数组(时间复杂度O(n)), 由于是已排序数组,每次遇到nums[i]!=nums[j],计数器i++
并且使得:nums[i]=nums[j]
空间复杂度0(1);
代码
:
public int RemoveDuplicates(int[] nums) { if (nums.Length == 0) return 0; int i = 0; for (int j = 1; j < nums.Length; j++) { if (nums[i] != nums[j]) { i++; nums[i] = nums[j]; } } return i+1; }
脚注
reference:https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
阅读全文
0 0
- LeetCode 记录(c#版)
- LeetCode记录
- leetcode记录-Two Sum
- [LeetCode] Triangle 解题记录
- Leetcode记录7-单链表
- 记录LeetCode的一点一滴
- leetcode题目记录
- LeetCode bug记录
- leetcode记录-字符串系列
- leetcode记录 31-60
- leetcode 记录61-90
- leetcode 91-120记录
- leetcode-esay类型记录
- leetcode记录1
- LeetCode解题记录
- LeetCode代码记录
- 刷leetCode记录贴
- leetcode 7: Reverse integer (C#版)
- Struts2面试题
- 类和对象
- Android 6.0 NavigationView
- Unity2D游戏入门教程(一)宝石迷阵(2)生成宝石地图
- 【编译错误解决方案】提示:“make mrproper”
- LeetCode 记录(c#版)
- 持续集成
- vue的常用的属性和方法及生命钩子
- 打包自己写的python库(Betubedl)并且发布到pypi上
- linux习题
- PAT乙级1022
- Java--map的遍历方式
- [LCT维护树的权值与形态]BZOJ3159.决战
- 【JAVASE_学习笔记】数据类型