https://leetcode.com/problems/find-all-duplicates-in-an-array/
The number of repetitions in a typical array. This is through the jump method, a jump to troubleshoot. The complexity is also O (n) because the check is not repeated.
Behind the discovery of someone else a better way ... As follows:
Public classSolution {//When find a number I, flip the number at position i-1 to negative. //if the number at position i-1 is already negative, I am the number that occurs twice. PublicList<integer> Findduplicates (int[] nums) {List<Integer> res =NewArraylist<>(); for(inti = 0; i < nums.length; ++i) {intindex = Math.Abs (Nums[i])-1; if(Nums[index] < 0) Res.add (Math.Abs (Index+1)); Nums[index]= -Nums[index]; } returnRes; }}
My approach:
PackageCom.company;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;classSolution { PublicList<integer> Findduplicates (int[] nums) {List<Integer> list =NewArraylist<>(); intindex = 1; while(Index <=nums.length) {intNext = Nums[index-1]; Nums[index-1] = 1; while(Next! =-1 && Next! = Index &&-1! = Nums[next-1] && next! = Nums[next-1]) { intTMP = Nums[next-1]; Nums[next-1] =Next; Next=tmp; } if(Next = =-1) { } if(Next = =index) {Nums[index-1] =Next; } Else if( -1 = = Nums[next-1]) {Nums[next-1] =Next; } Else{list.add (next); } Index++; } returnlist; }} Public classMain { Public Static voidMain (string[] args) {System.out.println ("Hello!"); Solution Solution=Newsolution (); int[] Nums = {}; List<Integer> ret =solution.findduplicates (nums); System.out.printf ("Ret Len is%d\n", Ret.size ()); Iterator ITER=Ret.iterator (); while(Iter.hasnext ()) {System.out.printf ("%d,", Iter.next ()); } System.out.println (); }}
Find-all-duplicates-in-an-array (the number of repetitions in a typical array, yes, I did it, but found that someone else had a better idea)