zoukankan      html  css  js  c++  java
  • leetCode(50):Summary Ranges 分类: leetCode 2015-07-24 11:17 122人阅读 评论(0) 收藏

    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"].

    本题我采用的是最直观的算法,但整数转化为字符还有些坎坷。应该还有简单的算法,但目前没有时间去研究了。

    class Solution {
    public:
        void intToString(int num, string& tmp)
        {
        	bool flag = false;
        	bool full = false;
        	if (num < 0)
        	{
        		flag = true;
        	}
        
        	if (num == 0)
        		tmp.push_back('0');
        	while (num!=0)
        	{	
        		tmp.push_back('0' + abs(num % 10));
        		num = num / 10;
        	}
        	if (flag)
        	{//最后压入负号
        		tmp.push_back('-');
        	}
    //起初是将负数转换为正数,再取余,这样就不用取绝对值了(负数取余还是负数),但有一个问题,就是补码表示的正负数区间是不对称的,如-128是没问题的,可能128就不在表示范围之内了。<strong>所以不能直接将负数转换为正数</strong>。
        	reverse(tmp.begin() , tmp.end());
        }
    
    
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> result;
        	if (nums.empty())
        		return result;
        	int tmpValue;
        	string tmp;
        	bool change = false;
        	for (int i = 0; i < nums.size(); ++i)
        	{
        		if (tmp.empty())
        		{
        			intToString(nums[i], tmp);
        			tmpValue = nums[i];
        			change = false;
        		}
        		else
        		{
        			if (tmpValue + 1 == nums[i])
        			{//连续
        				tmpValue = nums[i];
        				change = true;
        			}
        			else
        			{//不连续
        				if (change)
        				{//有连续情况出现
        					tmp.push_back('-');
        					tmp.push_back('>');
        					string tmp1;
        					intToString(tmpValue, tmp1);
        					tmp+=tmp1;
        					result.push_back(tmp);
        					tmp.clear();
        					i--;
        				}
        				else
        				{//没有连续的数
        					result.push_back(tmp);
        					tmp.clear();
        					i--;
        				}
        			}
        		}
        	}
        	if (change)
        	{//最后一个string
        		tmp.push_back('-');
        		tmp.push_back('>');
        		string tmp1;
    			intToString(tmpValue, tmp1);
    			tmp+=tmp1;
        		result.push_back(tmp);
        		tmp.clear();
        	}
        	else
        	{
        		result.push_back(tmp);
        		tmp.clear();
        
        	}
        
        	return result;
        }
    };

    网上看的别人的解法,很屌!


       class Solution {
        public:
            vector<string> summaryRanges(vector<int>& nums) {
                vector<string> res;
                nums.push_back(INT_MIN);
                long long start = nums[0], last = nums[0];
                for (int i = 1; i < nums.size(); i++) {
                    if (last + 1 != (long long)nums[i]) {
                        res.push_back(to_string(start));
                        if (last != start) {//to_string的使用,to_string可以将数值类型的变量(int,double,long等)转换为string类型
                            res[res.size() - 1] = res[res.size() - 1] + "->" + to_string(last);
                        }
                        start = nums[i];
                    }
                    last = nums[i];
                }
                return res;
            }
        };



  • 相关阅读:
    集合类--容器
    《学习之道》第十一章理解
    文件操作引出流(一)Stream和File.Create(path)
    整理文件操作(五)
    整理文件操作(四)Image.FromFile(path)
    整理文件操作(三)File.Exists(path)和new FileInfo(path).Exists
    整理文件操作(二)File和FileInfo
    整理文件操作(一)逻辑流程
    《学习之道》第十一章先理解再去记忆
    《学习之道》第十一章再次强调激发感官
  • 原文地址:https://www.cnblogs.com/zclzqbx/p/4687046.html
Copyright © 2011-2022 走看看