首页 > 其他 > 详细

Pivot Index--Google

时间:2016-11-12 07:47:15      阅读:217      评论:0      收藏:0      [点我收藏+]
return 3
 
One for loop to calculate sum, then another loop to compare (sum-nums[j]-leftsum)==leftsum?
if true return j, if false, then update leftsum=sum-nums[j]
 
 1 public int PivotIndex(int[] nums)
 2         {
 3             if (nums == null || nums.Length == 0)
 4             {
 5                 return -1;
 6             }
 7             int l = nums.Length;
 8             int sum = 0; int leftsum = 0;
 9             for (int i = 0; i < l; i++)
10             {
11                 sum +=nums[i];
12             }
13             for (int j=0; j< l; j++)
14             {
15                 if (sum - nums[j] - leftsum == leftsum)
16                 {
17                     return j;
18                 }
19                 else
20                     leftsum += nums[j];
21             }
22             return -1;
23         }

 

Pivot Index--Google

原文:http://www.cnblogs.com/MiaBlog/p/6056134.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!