Wiggle Subsequence

problem description:

A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.

For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast,[1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.

Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.

解决方案:

假设前i个数已经找到了他们的最大摇摆子序列,若下一个数该选择一个小于前面的数,则应该选择第i个数后面中从大到小排列中最小的数;若下一个数该选择大于前面的数,则选择第i个数后面有小到大排列子序列中最大的数。

第一个数应该选择连续三个有序中中间的数。

显然这种方法对每个数判断一次,复杂度为O(n)。

不过用leetcode提交显示当输入序列为[33,53,12,64,50,41,45,21,97,35,47,92,39,0,93,55,40,46,69,42,6,95,51,68,72,9,32,84,34,64,6,2,26,98,3,43,30,60,3,68,82,9,97,19,27,98,99,4,30,96,37,9,78,43,64,4,65,30,84,90,87,64,18,50,60,1,40,32,48,50,76,100,57,29,63,53,46,57,93,98,42,80,82,9,41,55,69,84,82,79,30,79,18,97,67,23,52,38,74,15]时,正确结果应该是67,我的结果是63.还没找到问题所在。

在网上找了找别人的方法后才发现自己的方法真复杂,我太蠢了,把问题搞得真复杂

代码如下:

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
		vector<int>::iterator it1=nums.begin();
		vector<int>::iterator it2=nums.end();
		int i,len,d1,d2,d3,d4,result;
		int temp;
		len=it2-it1;
		if(len<=1)//序列中只有一个数或0个数
			return len;
		else if(len==2)
		{
			if(*it1==*(it1+1))//输入两个相同的数值
				return 1;
			else//输入两个不同的值
				return 2;
		}
		else
		{
			i=1;
			while(*it1==*(it1+i)&&i<len)
			{
				i++;
			}
			if(i==len)
			{
				if(*it1==*(it1+i-1))
					return 1;
				else
					return 2;
			}
			else
			{
				/*d1=*(it1+i)-*it1;
				result=2;
				it1=it1+i;
				temp=*it1;
				it1++;*/
				
				/*d2=*(it1+i)-*it1;
				d1=-d2;
				temp=*it1;
				it1=it1+i;
				result=1;*/
				bool m,n;
				while((it1+2)!=it2)
				{
					m=(*it1<=*(it1+1))&&(*(it1+1)<=*(it1+2));
					n=(*it1>=*(it1+1))&&(*(it1+1)>=*(it1+2));
						
					//if((*it1<=*(it1+1)<=*(it1+2))||(*it1>=*(it1+1)>=*(it1+2)))
					if(m||n)
					{
						//temp=*(it1+1);
						it1=it1+1;
					}
					else 
						break;
					
				}
				d2=*(it1+1)-*it1;
				d1=-d2;
				temp=*it1;
				it1=it1+1;
				result=1;

				while(it1!=it2)
				{
					d2=*it1-temp;
					if(d1*d2<0)
					{
						result++;
						if((it1+1)!=it2)
						{
							d3=*(it1+1)-temp;
							if(d1*d3<0)
							{
								d4=*(it1+1)-*it1;
								if(abs(d3)>abs(d2)&&(d4*d2>0))
								{
									it1++;
									temp=*it1;
									d1=d3;
								}
								else
								{
									temp=*it1;
								    it1++;
								    d1=d2;

								}
								

							}
							else
							{
							    temp=*it1;
							    d1=d2;
								it1++;
								//break;
							}
						}
						else
							it1++;
						
					}
					else
						it1++;

				}
				return result;
			

			}
			
			

		}

    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值