↓ 跳过正文
  1. Posts/

leetcode 228. Summary Ranges

·412 字·1 分钟

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

题意:把连续的数连续表示

思路:模拟。注意有负数,注意有-2147483648这种数据。

本来还想着,可能是leetcode加数据的审核机制太松,导致被人加了奇怪的数据。。。

结果发现出题人和加数据的人是一个人啊?

不给数据范围,加这种奇怪的数据很有意思? 分分钟卡掉你的标程啊?

感觉像吃了苍蝇一样恶心。。一句话,出题人傻逼

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月14日 星期五 16时26分01秒
 4File Name :228.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    string int2st(long long x)
11    {
12	if (x==0) return "0";
13	string ret = "";
14	int val;   //md还有负数
15	long long sign = 1;
16	if (x<0) sign = -1;
17	x*=sign;
18	while (x)
19	{
20	    val = x % 10;
21	    ret = ret + char(val+'0');
22	    x/=10;
23	}
24	if (sign==-1) ret +="-";
25	reverse(ret.begin(),ret.end());
26	return ret;
27    }
28    string solve(vector<int>& vec)
29    {
30	string ret = "";
31	int siz = vec.size();
32	if (siz==1) ret = ret + int2st(vec[0]);
33	else ret = ret + int2st(vec[0]) + "->" + int2st(vec[siz-1]);
34	return ret;
35    }
36    vector<string> summaryRanges(vector<int>& nums) {
37	int siz = nums.size();
38	vector<string>res;
39	if (siz==0) return res;
40	vector<int>tmp;
41	for ( int i = 0 ; i < siz ; i++)
42	{
43	    if (tmp.size()==0)
44	    {
45		tmp.push_back(nums[i]);
46	    }else if (nums[i]==nums[i-1]+1)
47	    {
48		tmp.push_back(nums[i]);
49	    }else
50	    {
51		res.push_back(solve(tmp));
52		tmp.clear();
53		tmp.push_back(nums[i]);
54	    }
55	}
56	res.push_back(solve(tmp));
57
58
59
60        return res;
61
62    }
63
64};

相关文章

leetcode 75. Sort Colors

·628 字·2 分钟
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

leetcode 11. Container With Most Water (two pointer)

·328 字·1 分钟
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.