in reply to Re: LeetCode Problem #1 - Two Sum - Improve and/or discuss.
in thread LeetCode Problem #1 - Two Sum - Improve and/or discuss.
G'day jdporter,
I liked the general thinking behind this; however, I see two issues.
"# assumption: @nums are already sorted."
There is no requirement for the input list to be sorted. As an example, see "1. Two Sum" Example 2:
Input: nums = [3,2,4], target = 6 Output: [1,2]
If you did "add a sorting step", you could potentially change the result:
Input: nums = [2,3,4], target = 6 Output: [0,2]
"# caveat: the nums in the list might not be uniq!!!"
Again, there's no requirement for this uniqueness. See Example 3:
Input: nums = [3,3], target = 6 Output: [0,1]
As far as I can tell (by inspection) your code produces a correct result with this input. An input of [3,1,1,1,3] would produce a correct result: [0,4]. An input of [3,3,3] would be invalid from the outset, because there's more than one possible solution: [0,1], [0,2] and [1,2] — whatever code you (or anyone) wrote would be immaterial for this input.
— Ken
|
|---|