设为首页 加入收藏

TOP

[LeetCode] Summary Ranges
2015-11-21 00:58:03 来源: 作者: 【 】 浏览:1
Tags:LeetCode Summary Ranges

Summary Ranges

?

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return [0->2,4->5,7].

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

解题思路:

这道题题意是,将相邻的一组数据用区间表示。虽然mark为easy,但是麻雀虽小,却五脏俱全,有很多陷阱的问题。

1、最开始想的是如下代码。但有几个问题需要注意一下。一个就是可能溢出问题,这体现在两个方面,一个是自行编写的数字转字符串的代码中,另一个是在判断两个数是否相邻。因为对c++不够了解,因此竟然不知道如何将数字转化成字符串,于是动手写了一个。?。这个版本的代码就像懒婆娘的裹脚布一样。

?

class Solution {
public:
    vector
  
    summaryRanges(vector
   
    & nums) { int len = nums.size(); vector
    
      result; if(len == 0){ return result; } int left = nums[0]; nums.push_back(nums[len-1]); len++; for(int i=1; i
     
       1)){ if(left == nums[i-1]){ result.push_back(intToString(left)); }else{ result.push_back(intToString(left) + -> + intToString(nums[i-1])); } left = nums[i]; } } return result; } string intToString(int n){ if(n==0){ return 0; } long long lN = n; string s = ; if(lN<0){ s = -; lN = -lN; } stack
      
        stacks; while(lN!=0){ stacks.push(lN%10 + '0'); lN = lN/10; } while(!stacks.empty()){ s += stacks.top(); stacks.pop(); } return s; } };
      
     
    
   
  
2、其实c++中可以直接将数字转化成字符串的。在 头文件中,有这么一个方法to_string。另外,判断是否相邻应该用不等号。因此,精简后的代码如下。以上两个版本都是先在后面添加一个哨兵数据。

?

?

class Solution {
public:
    vector
   
     summaryRanges(vector
    
     & nums) { int len = nums.size(); vector
     
       result; if(len == 0){ return result; } int left = nums[0]; nums.push_back(nums[len-1]); len++; for(int i=1; i
      
        + to_string(nums[i-1])); } left = nums[i]; } } return result; } };
      
     
    
   
3、在网上找到另外一种解决方案,非常优雅,其答题思路就是找到每个区间的左右两个端点,且无需哨兵。

?

?

class Solution {
public:
    vector
   
     summaryRanges(vector
    
     & nums) { int len = nums.size(); int i=0; vector
     
       result; while(i
      
        + to_string(nums[i + j - 1])); i += j; } return result; } };
      
     
    
   


?

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1011 Starship Troopers(树.. 下一篇输入一行字符,统计有多少个单词

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: