zoukankan      html  css  js  c++  java
  • 26. 移除排序数组中的重复元素 Remove Duplicates from Sorted 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.

    题意:移除排序数组中的重复元素,修改原数组,把不重复的元素移到前面

    1. public class Solution {
    2. public int RemoveDuplicates(int[] nums) {
    3. List<int> list = new List<int>();
    4. for (int i = 0; i < nums.Length; i++) {
    5. if (i >= 1 && nums[i] != nums[i - 1]) {
    6. list.Add(nums[i]);
    7. }else if (i == 0) {
    8. list.Add(nums[i]);
    9. }
    10. }
    11. for (int i = 0; i < list.Count; i++) {
    12. nums[i] = list[i];
    13. }
    14. return list.Count;
    15. }
    16. }






  • 相关阅读:
    K近邻法
    决策树
    朴素贝叶斯
    Git学习笔记
    【原】maven web项目eclipse搭建
    三道面试题
    72-74 流的考点
    57-71 容器考点
    四 java web考点
    五 数据库考点
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/6486987.html
Copyright © 2011-2022 走看看