Write this Code section different | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Write this Code section different

i already posted this , but my post had some mistakes publicĀ classĀ TestĀ { Ā Ā publicĀ staticĀ voidĀ main(String...Ā args)Ā { Ā  Ā Ā System.out.println(merge(newĀ int[]Ā {Ā 1,Ā 3,Ā 4,Ā 4,Ā 4,Ā 4,Ā 4,Ā 5Ā },Ā 0)); Ā Ā } Ā Ā  Ā Ā publicĀ staticĀ StringĀ merge(int[]Ā ns,Ā intĀ i)Ā { Ā  Ā Ā ifĀ (nsĀ ==Ā nullĀ ||Ā iĀ >=Ā ns.length)Ā { Ā  Ā  Ā Ā returnĀ ""; Ā  Ā Ā } ***START HERE with Code "re-writing"*** Ā  Ā Ā intĀ p=Ā 1; Ā  Ā Ā intĀ n=Ā i; Ā  Ā Ā doĀ { Ā  Ā  Ā  p *=Ā ns[i]; Ā  Ā  Ā  n++; Ā  Ā Ā }Ā whileĀ (nĀ <Ā ns.lengthĀ &&Ā ns[i]Ā ==Ā ns[n]); Ā  Ā Ā returnĀ p+Ā " "Ā +Ā merge(ns, n); Ā Ā } }

20th Nov 2016, 5:40 PM
newName
4 Respostas
+ 2
Finally had some time to do it :-) public static String mergeStreams(int[] ns, int i) { if (ns == null || i >= ns.length) { return ""; } Map<Integer, Integer> intMap = Arrays.stream(ns).skip(i).boxed().collect(Collectors.groupingBy(Integer::intValue, Collectors.reducing(1, (a, b) -> a * b))); return intMap.values().stream().map(String::valueOf).collect(Collectors.joining(" ")); } As for performance, it is much slower this way.
21st Nov 2016, 4:23 PM
Roland
+ 1
cool, now i understand it :-) hmmm i will give it a shot in about 2 hours or so. have to watch something now :-D
20th Nov 2016, 7:10 PM
Roland
0
well this one has mistakes too. power & next is undefined. :-)
20th Nov 2016, 6:47 PM
Roland
0
yeah... I am such a noob lol ._. I defined them :) now it should be at least without mistakes
20th Nov 2016, 7:01 PM
newName