↓ 跳过正文
  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 209. Minimum Size Subarray Sum (尺取法)

·248 字·1 分钟
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length under the problem constraint 思路:尺取即可。。好久没写,竟然调了半天。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 20时48分00秒 4File Name :209.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 int ruler(vector<int>nums,int tar,int n) 11 { 12 int head = 0; 13 int tail = 0; 14 int sum = 0 ; 15 int res = 0x3f3f3f3f; 16 while (tail<n&&head<=tail) 17 { 18 sum = sum + nums[tail]; 19 if (sum>=tar) 20 { 21 res = min(res,tail-head+1); 22 while (sum>=tar&&head<tail) 23 { 24 sum-=nums[head]; 25 head++; 26 } 27 if (sum>=tar) 28 { 29 res = min(res,tail-head+1); 30 } 31 else 32 { 33 head--; 34 sum+=nums[head]; 35 res = min(res,tail-head+1); 36 } 37 } 38 39 40 41 tail++; 42 } 43 return res==0x3f3f3f3f?0:res; 44 } 45 46 47 int minSubArrayLen(int s, vector<int>& nums) { 48 int n = nums.size(); 49 int res = ruler(nums,s,n); 50 return res; 51 52 53 } 54 55};

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. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. 题意:一个数组,由0,1,2组成,现在要求升序排列 思路:无脑做法就是计数排序,扫两遍,时间复杂度O(n),空间复杂度O(1)

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. Note: You may not slant the container and n is at least 2. 题意:n条竖直的线段 (i,0)->(i,a[i]),从中选2条,和x轴共同组成一个开口的容器,问容器的最大面积。

leetcode 16. 3Sum Closest (k-sum问题,two pointer)

·224 字·1 分钟
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 思路: 排序,然后two pointer,复杂度 O(n^2) 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 16时24分28秒 4File Name :16.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 int n; 11 int threeSumClosest(vector<int>& nums, int target) { 12 n = nums.size(); 13 sort(nums.begin(),nums.end()); 14 int mn = 0x3f3f3f3f; 15 int x,y,z; 16 for ( int i = 0 ; i <=n-3 ; i++) 17 { 18 int head = i+1; 19 int tail = n-1; 20 int tar = target - nums[i]; 21 while (head<tail) 22 { 23 int cur = target-nums[i]-nums[head]-nums[tail]; 24 if (abs(cur)<mn) 25 { 26 mn = abs(cur); 27 x = nums[i]; 28 y = nums[head]; 29 z = nums[tail]; 30 } 31 if (nums[head]+nums[tail]==tar) return target; 32 if (nums[head]+nums[tail]<tar) head++; 33 else tail--; 34 } 35 } 36 return x + y + z; 37 38 39 40 41 } 42 43};