026 - Remove Duplicates from Sorted Array
2015-11-11 18:32
295 查看
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 =
Your function should return length =
nums being
把数组中重复的元素移除
int removeDuplicates(int* nums, int numsSize)
{
int k = 1, i;
if (numsSize < 2) return numsSize;
for (i = 1; i < numsSize; i++) {
if (nums[i] != nums[k-1])
nums[k++] = nums[i];
}
return k;
}
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
1and
2respectively. It doesn't matter what you leave beyond the new length.
把数组中重复的元素移除
int removeDuplicates(int* nums, int numsSize)
{
int k = 1, i;
if (numsSize < 2) return numsSize;
for (i = 1; i < numsSize; i++) {
if (nums[i] != nums[k-1])
nums[k++] = nums[i];
}
return k;
}
相关文章推荐
- Android学习路线图
- 学习嵌入式的捷径
- 关于systemd的service超时时间设定
- 主库磁盘满,从库io线程报错Got fatal error 1236 from master when reading data from binary log
- 用纯JavaScript替代jQuery的技巧
- 025 - Reverse Nodes in k-Group
- 系统功能在用户测试阶段被推翻
- SQL server数据库内置账户SA登录设置
- 数值分析 反幂法求矩阵按模最小特征值 MATLAB实现
- JSON.parse()和JSON.stringify()
- 024 - Swap Nodes in Pairs
- Visual Studio 2010 安装 MSDN Library
- Git SSH Key 生成步骤
- SQLite的基本使用 与FMDB框架的使用
- android studio 提示"Gradle project sync failed."
- Unity修改自动生成的代码模板
- 023 - Merge k Sorted Lists
- sqlite3 中执行带变量的命令
- dex & oat & ELF & art
- iOS笔记UI--ipad分页控件