
Do you need help with
Sorted two pointer two sum with a outter loopThe key is to avoid duplicates, in the outer loop when num[i] == num[i - 1], continue, so you only use the first element of duplicated element. In the inner loop also, i and j need to avoid duplicates using a while loop for (int i = 0; i < n; i++) { if (i != 0 && numbers[i] == numbers[i - 1]) { continue; } int j = i + 1, k = n - 1; while (j < k) { if (numbers[j] + numbers[k] > -numbers[i]) { int number = numbers[k]; while (k > j && numbers[k] == number) { k--; } } else if (numbers[j] + numbers[k] < -numbers[i]) { int number = numbers[j]; while (j < k && numbers[j] == number) { j++; } } else { ArrayList<Integer> tmp = new ArrayList<Integer>(Arrays.asList(numbers[i], numbers[j], numbers[k])); Collections.sort(tmp); res.add(tmp); int number = numbers[k]; while (k > j && numbers[k] == number) { k--; } number = numbers[j]; while (j < k && numbers[j] == number) { j++; } } }
Then try StudyFetch, the AI-powered platform that can answer your questions and teach you more about it!


How StudyFetch Helps You Master This Topic
AI-Powered Explanations
Get in-depth, personalized explanations on this topic and related concepts, tailored to your learning style.
Practice Tests
Take adaptive quizzes that focus on your weak areas and help reinforce your understanding of the subject.
Interactive Flashcards
Review key concepts and terms with AI-generated flashcards, optimizing your retention and recall.
Educational Games
Engage with fun, interactive games that reinforce your learning and make studying more enjoyable.
Start mastering this topic and many others with StudyFetch's comprehensive learning tools.