Advertisement
Alexxik

Untitled

Sep 8th, 2023 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.65 KB | None | 0 0
  1. // может быть нужно отсортировать
  2.  
  3. func summaryRanges(_ nums: [Int]) -> [String] {
  4.     if nums.isEmpty {
  5.         return []
  6.     }
  7.    
  8.     var r = 0
  9.     var l = 0
  10.     var result = [String]()
  11.    
  12.     for i in 1..<nums.count {
  13.         if nums[i] != nums[r] + 1 {
  14.             if r == l {
  15.                 result.append("\(nums[l])")
  16.             } else {
  17.                 result.append("\(nums[l])->\(nums[r])")
  18.             }
  19.             l = i
  20.         }
  21.         r += 1
  22.     }
  23.     if r == l {
  24.         result.append("\(nums[l])")
  25.     } else {
  26.         result.append("\(nums[l])->\(nums[r])")
  27.     }
  28.     return result
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement