orbankaroly
orbankaroly
JCHJava Community | Help. Code. Learn.
Created by orbankaroly on 11/19/2024 in #java-help
Hey guys,
I am trying to sort a String Array by the length of it's elements. I found only this solution: Arrays.sort(strs, Comparator.comparing(String::length)). Is there a more 'elegant' way to do it (strs is the name of the Array)?
7 replies
JCHJava Community | Help. Code. Learn.
Created by orbankaroly on 11/17/2024 in #java-help
Two Sum Problem on leetcode
Hi guys, I can't seem to figure out what the problem is here: class Solution { public int[] twoSum(int[] nums, int target) { int result [] = {0, 0}; List<Integer> original = new ArrayList<Integer>(); List<Integer> complement = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) { original.add(nums[i]); complement.add(target - nums[i]);
} for (int i = 0; i < nums.length; i++) {
if (complement.contains(nums[i])) { int second = complement.indexOf(nums[i]); result [0] = i; result [1] = second; }
} return result; } } It does work for 62 out of 63 test cases but gives a wrong answer with the sets of [2, 4, 11, 3]. Just started learning programming, maybe I am missing something obvious? Thanks in advance.
12 replies