Summary Ranges
LeetCode 228 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Extend consecutive runs.
நான் Summary Ranges (LeetCode 228) ஐ இப்படித் தீர்த்தேன் — Intervals பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Walk the sorted list and extend a run while each next number is exactly one more. When the run breaks, write it as 'a->b' (or just 'a' for a single number).
Approach
- 1
start = nums[i].
- 2
Advance while nums[i+1] == nums[i] + 1.
- 3
Record start->end or start.
நான் எப்படித் தீர்த்தேன்
Run extensionPythonO(n) time
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
i=0
res=[]
n = len(nums)
while i<n:
start = nums[i]
while i+1<n and nums[i+1]==nums[i]+1:
i+=1
end = nums[i]
if start!=end:
res.append(f"{start}->{end}")
else:
res.append(f"{start}")
i+=1
return resSummary Ranges — அடிக்கடி கேட்கப்படும் கேள்விகள்
Summary Ranges பின்னாலுள்ள intuition என்ன?
Walk the sorted list and extend a run while each next number is exactly one more. When the run breaks, write it as 'a->b' (or just 'a' for a single number).
Summary Ranges (LeetCode 228) ஐ எப்படித் தீர்ப்பது?
1. start = nums[i]. 2. Advance while nums[i+1] == nums[i] + 1. 3. Record start->end or start.
Summary Ranges இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(1) extra. (Accepted approach: Run extension.)
Summary Ranges இல் பொதுவான தவறுகள் என்ன?
Empty input returns [].