Summary Ranges | LeetCode 228 | C++, Java, Python

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

Search a 2D Matrix | Leetcode 228
Facebook Coding Interview question,
google coding interview question,
leetcode,
Summary Ranges,
Summary Ranges C++,
Summary Ranges Java,
Summary Ranges python,
Summary Ranges solution,
228. Summary Ranges,

#CodingInterview #LeetCode #Google #Amazon
Рекомендации по теме
Комментарии
Автор

Thankyou so much for this video :)
The reason why i subscribed You is solved the code in c++, and you explained your code with the help of some examples...
This is all what i want....
Thanks <3

shashanksabharwal
Автор

Amazing work Knowledge Centre; Providing solution in all three mainstream languages requires guts; Keep it up; Also - in this question may be if add a continue block when number is in range would make the code clearer;
vector<string> summaryRanges(vector<int>& nums) {
vector<string> result;
if(nums.size() == 0) return result;

int a = nums[0];

for(int i = 0; i < nums.size(); i++) {
//case when we actually do not have to process something
if(i != nums.size() - 1 && nums[i] + 1 == nums[i+1]) {
continue;
}

//either we reached at the end || or end of range detected
if(nums[i] != a) {
+ "->" + to_string(nums[i])));
} else {

}
//move a only if we are not at last index;
if(i != nums.size() - 1) {
a = nums[i+1];
}
}
return result;
}

RG-yjcb
Автор

Very nice -- thanks for these videos!!

jimwoodward
Автор

Python 3 easy code:

class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
res = []
n = len(nums)
i = 0

while i < n:
start = nums[i]
while i + 1 < n and nums[i + 1] == nums[i] + 1:
i += 1
if start != nums[i]:
res.append(str(start) + '->' + str(nums[i]))
else:
res.append(str(start))
i += 1

return res

shubhampawar
Автор

Your explanation is really great sir!!!

ashwinvarma
visit shbcf.ru