码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构学习(一)

时间:2015-01-28 23:57:04      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

开始看网易公开课中的数据结构了,于是就打算写一些记录

 1 int MaxSubseqSum1(int A[],int N)    //时间复杂度   T(N)=O(N*N*N)
 2 {
 3     int thisSum,maxSum=0;
 4     for(int i=0;i<N;i++)//i 是子列的左端
 5     {
 6         for(int j=i;j<N;j++)// j 是子列的右端
 7         {
 8             thisSum=0;    //是从A[i]到A[j]的子列和
 9             for(int k=0;k<=j;k++)
10             {
11                 thisSum+=A[k];
12                 if(thisSum>maxSum)//如果得到的这个子列和更大
13                 {
14                     maxSum=thisSum;//则更新结果
15                 }
16             }
17         }
18     }
19     return maxSum;
20 }
21 
22 
23 int MaxSubseqSum2(int A[],int N)   //时间复杂度   T(N)=O(N*N)
24 {
25     int thisSum,maxSum=0;
26     for(int i=0;i<N;i++)//i 是子列的左端
27     {
28         thisSum=0;
29         for(int j=i;j<N;j++)// j 是子列的右端
30         {
31             thisSum+=A[j];
32             if(thisSum>maxSum)
33             {
34                 maxSum=thisSum;
35             }
36         }
37     }
38 
39     return maxSum;
40 }
41 
42 
43 int MaxSubseqSum3(int A[],int N) 
44 {
45     //分而治之,运用递归的思想,视频中没有代码,我也就先跳过吧。。。
46 }
47 
48 int MaxSubseqSum4(int A[],int N) //运用在线处理的思想
49 {
50     int thisSum,maxSum=0;
51     for(int i=0;i<N;i++)
52     {
53         thisSum+=A[i];
54         if(thisSum>maxSum)
55         {
56             maxSum=thisSum;
57         }
58         else if(thisSum<0)//若当前和小于0,那么就不可能使后面的总和增大,弃之
59         {
60             thisSum=0;
61         }
62     }
63     return maxSum;
64 }

晚安,好梦!

数据结构学习(一)

标签:

原文地址:http://www.cnblogs.com/yinmo/p/4257432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!