Report the indices of two numbers which sum up to the target value in a sorted list
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi community,
I have a question as below:
How do you report the indices of two numbers which sum up to the target value in a sorted list (no repeating numbers).
Example would be like
nums = [1,2,3,4,7,8,10,11], target = 10, ans = [(1, 5), (2, 4)]
def uniqueSum(nums -> List[int], target -> int):
return
댓글 수: 0
답변 (1개)
Walter Roberson
2023년 2월 16일
nums = [1,2,3,4,7,8,10,11];
target = 10
[r,c] = find(triu(nums + nums.' == target));
[r,c]
You would use a different strategy if the array was significantly longer.
Note: there is a completely different strategy available for the case of sorted inputs only which involes looping and has a much lower computational complexity.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!