ZigZag
2014-06-27 17:14
453 查看
Problem Statement | ||||||||||||
A sequence of numbers is called a zig-zag 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 zig-zag sequence. For example, 1,7,4,9,2,5 is a zig-zag 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 zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero. Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Limits | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | sequence contains between 1 and 50 elements, inclusive. | |||||||||||
- | Each element of sequence is between 1 and 1000, inclusive. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
| ||||||||||||
5) | ||||||||||||
|
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
class ZigZag
{
public:int longestZigZag(vector <int> sequence )
{
int size=sequence.size();
cout<<"vector size:"<<size<<endl;
int maxsize=1;
int dp[100];
int alt[100];
for(int i=0;i<size;i++)
{
dp[i]=1;
alt[i]=0;
}
if(size==0||size==1)
return size;
else
{
dp[1]=2;
alt[1]=sequence.at(1)-sequence.at(0);
for(int i=2;i<size;i++)
{
int temp=sequence.at(i);
int temp_max=-100000;
for(int j=0;j<i;j++)
{
if(alt[j]<0&&(temp>sequence.at(j))&&dp[j]>=temp_max)
{
temp_max=dp[j]+1;
alt[i]=temp-sequence.at(j);
}
if(alt[j]>0&& (temp<sequence.at(j))&&dp[j]>=temp_max)
{
temp_max=dp[j]+1;
alt[i]=temp-sequence.at(j);
}
}
dp[i]=temp_max;
}
for(int i=0;i<size;i++)
{
if(maxsize<dp[i])
maxsize=dp[i];
}
return maxsize;
}
}
};
代码风格太差了,以后加强这方面的训练。
相关文章推荐
- zigzag
- LeetCode: Binary Tree Zigzag Level Order Traversal
- 生成 zigzag(JPEG编码里取像素数据的排列顺序) 样式的二维数组
- Binary Tree Zigzag Level Order Traversal
- 输出45°递增的ZigZag矩阵
- zigzag数组
- [LeetCode] Binary Tree Zigzag level order traversal
- LeetCode | Binary Tree Zigzag Level Order Traversal
- [leet code] Binary Tree Zigzag Level Order Traversal
- leetcode ZigZag Conversion
- LeetCode(Binary Tree Level Order Traversal, 2,Zigzag)二叉树的层次遍历
- leetcode第一刷_Binary Tree Zigzag Level Order Traversal
- LeetCode题解——ZigZag Conversion
- [leetcode] Binary Tree Zigzag Level Order Traversal
- zigzag数组
- 二叉树-Binary Tree Zigzag Level Order Traversal
- Leetcode Binary Tree Zigzag level Order Traversal
- [LeetCode] Binary Tree Zigzag Level Order Traversal
- LeetCode: ZigZag Conversion 解题报告
- ZigZag Conversion [Z 型字母转换]