Rumah > Java > javaTutorial > LeetCode & Q26-Remove Duplicates from Sorted Array-Easy

LeetCode & Q26-Remove Duplicates from Sorted Array-Easy

PHP中文网
Lepaskan: 2017-07-09 18:12:13
asal
1480 orang telah melayarinya

Descriptions:

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.

我写的一直有问题...用了HashSet集合,没有研究过这个类型,[1,1,2]输出结果一直是[1,1]

(在小本本上记下,要研究HashSet)

<code class="sourceCode java"><span class="kw">import java.util.HashSet;</span>

<span class="kw">import java.util.Set;</span>

<span class="kw">public</span> <span class="kw">class</span> Solution {

    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">int</span> <span class="fu">removeDuplicates</span>(<span class="dt">int</span>[] nums) {

        Set<Integer> tempSet = <span class="kw">new</span> HashSet<>();

        <span class="kw">for</span>(<span class="dt">int</span> i = <span class="dv">0</span>; i < nums.<span class="fu">length</span>; i++) {

            Integer wrap = Integer.<span class="fu">valueOf</span>(nums[i]);

            tempSet.<span class="fu">add</span>(wrap);

        }

        <span class="kw">return</span> tempSet.<span class="fu">size</span>();

    }

}</code>
Salin selepas log masuk

下面是优秀答案

Solutions:

<code class="sourceCode java"><span class="kw">public</span> <span class="kw">class</span> Solution {

    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">int</span> <span class="fu">removeDuplicates</span>(<span class="dt">int</span>[] nums) {

        <span class="dt">int</span> j = <span class="dv">0</span>;

        <span class="kw">for</span>(<span class="dt">int</span> i = <span class="dv">0</span>; i < nums.<span class="fu">length</span>; i++) {

            <span class="kw">if</span>(nums[i] != nums[j]) {

                nums[++j] = nums[i];

            }

        }

        <span class="kw">return</span> ++j;

    }

}</code>
Salin selepas log masuk

有两个点需要注意:

  1. 因为重复的可能有多个,所以不能以相等来做判定条件
  2. 注意j++++j的区别,此处用法很巧妙,也很必要!

Atas ialah kandungan terperinci LeetCode &amp; Q26-Remove Duplicates from Sorted Array-Easy. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan