摘要:思路原數(shù)組長度為,則返回原數(shù)組長度不為,則至少有個(gè)元素。將所有不重復(fù)的數(shù)值賦給,而當(dāng)和相等時(shí),不做處理。最后返回的就是不同元素的個(gè)數(shù),也是新數(shù)組的長度。只有在時(shí),才對賦值。注意,每次初始化的時(shí)候要分兩種情況,這就意味著從的時(shí)候開始遍歷。
Remove Duplicates from Sorted Array I Problem
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.
ExampleGiven input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
Note思路:原數(shù)組長度為0,則返回0;原數(shù)組長度不為0,則至少有index = 1個(gè)元素。循環(huán)整個(gè)數(shù)組,比較nums[i]和nums[i-1]。將所有不重復(fù)的數(shù)值賦給nums[index],而當(dāng)nums[i]和nums[i-1]相等時(shí),不做處理。最后返回的index就是不同元素的個(gè)數(shù),也是新數(shù)組的長度。
Solutionpublic class Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int index = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[i-1]) nums[index++] = nums[i]; } return index; } }Remove Duplicates from Sorted Array II Problem
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
Note相同的思路,加入一個(gè)count變量,每次循環(huán)比較前后元素是否重復(fù)時(shí),更新該元素出現(xiàn)的次數(shù)count。只有在count <= 2時(shí),才對nums[index]賦值。
注意,每次初始化count的時(shí)候要分兩種情況:i == 0 || nums[i] != nums[i-1],這就意味著從i = 0的時(shí)候開始遍歷。
public class Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int index = 0, count = 0; for (int i = 0; i < nums.length; i++) { if (i == 0 || nums[i] != nums[i-1]) count = 1; else count++; if (count <= 2) { nums[index] = nums[i]; index++; } } return index; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65830.html
摘要:排序數(shù)組中找最小值或最大值的題目,很明顯可以使用二分法。因此,只判斷終點(diǎn)和中點(diǎn)元素的大小關(guān)系即可。這里有一種情況是上述后三個(gè),中點(diǎn)值和末位相等。此時(shí),兩邊同時(shí)遞歸,并返回兩邊遞歸值的較小值。當(dāng)首位和末位重合,說明已夾逼得到最小值。 Find Minimum in Rotated Sorted Array Problem Suppose a sorted array is rotated...
Problem Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume NO duplicates in the a...
摘要:雙指針法復(fù)雜度時(shí)間空間思路我們可以將不重復(fù)的序列存到數(shù)列前面,因?yàn)椴恢貜?fù)序列的長度一定小于等于總序列,所以不用擔(dān)心覆蓋的問題。代碼雙指針法復(fù)雜度時(shí)間空間思路思路和上題一樣,區(qū)別在于記錄前兩個(gè)遍歷到的數(shù)字來幫助我們判斷是否出現(xiàn)了第三遍。 Remove Duplicates from Sorted Array I Given a sorted array, remove the dupl...
摘要:題目要求翻譯將鏈表中重復(fù)的元素全部刪除,返回新的頭結(jié)點(diǎn)。相比于,這里將重復(fù)的元素全部刪除。除此以外,我們還需要知道重復(fù)元素的前一個(gè)值和重復(fù)元素的最后一個(gè)值。如果存在重復(fù)值,則跳過重復(fù)值后,前節(jié)點(diǎn)不變,否則前節(jié)點(diǎn)跟隨后節(jié)點(diǎn)同時(shí)向后移動。 題目要求 Given a sorted linked list, delete all nodes that have duplicate number...
摘要:思路與代碼其實(shí)在這里我們?nèi)匀谎永m(xù)中的思路。在遇到非重復(fù)值以及非多余的重復(fù)值時(shí),將數(shù)值移動到當(dāng)前記錄的下標(biāo)上。保證該下標(biāo)前的值均為滿足題目條件的值。第一次我使用了來記錄某個(gè)值出現(xiàn)的次數(shù)。 題目要求 Follow up for Remove Duplicates: What if duplicates are allowed at most twice? For example, Giv...
閱讀 1060·2021-10-19 11:42
閱讀 3009·2021-09-10 10:51
閱讀 718·2021-09-09 09:33
閱讀 1800·2021-09-01 10:43
閱讀 2799·2019-08-30 12:43
閱讀 3545·2019-08-30 11:24
閱讀 2175·2019-08-30 10:56
閱讀 2804·2019-08-29 11:00