Want to know:
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++; } } }
Get a detailed, AI-powered explanation for this question and thousands more on StudyFetch.
Get the Answer for FreeHow StudyFetch Helps You Master This Topic
AI-Powered Answers
Get instant, detailed explanations powered by AI that understands your course material.
Deep Understanding
Go beyond surface-level answers with step-by-step breakdowns and examples.
Personalized Learning
Spark.E adapts to your learning style and helps you connect ideas.
Practice & Test
Turn any question into flashcards, quizzes, and practice tests to solidify your knowledge.
Explore More Questions
- les juridictions sont engorgées et le développement des phénomènes de non recours
- Quelle est la moyenne de cette distribution 2; 2; 2; 2; 2; 2; 2; 10
- 6: Which of the following department managers would be best suited to oversee the development of an information security policy? A.) Information Systems B.) Human Resources C.) Business operations D.) Security administration