标签:
1 /// <summary> 2 /// Solution A 3 /// 通过率 100% 4 /// </summary> 5 /// <param name="A"></param> 6 /// <returns></returns> 7 public static int solution(int[] A) 8 { 9 int sumL = A[0]; 10 int sumR = 0; 11 int min = 0; 12 for (int i = 1; i < A.Length; i++) 13 sumR += A[i]; 14 min = Math.Abs(sumL - sumR); 15 for (int i = 1; i < A.Length - 1; i++) 16 { 17 sumL += A[i]; 18 sumR -= A[i]; 19 if (Math.Abs(sumL - sumR) < min) 20 min = Math.Abs(sumL - sumR); 21 } 22 return min; 23 } 24 25 /// <summary> 26 /// Solution B 27 /// 通过率 66% 28 /// 暂不清楚问题在哪里,也看不到未通过的测试用例 29 /// </summary> 30 /// <param name="A"></param> 31 /// <returns></returns> 32 public static int solution(int[] A) 33 { 34 int i = 0; 35 int j = A.Length - 1; 36 int sum = 0; 37 while (i < j) 38 { 39 if (Math.Abs(sum + A[i]) < Math.Abs(sum - A[j])) 40 { 41 sum += A[i]; 42 i++; 43 } 44 else if (Math.Abs(sum + A[i]) > Math.Abs(sum - A[j])) 45 { 46 sum -= A[j]; 47 j--; 48 } 49 else 50 { 51 sum += A[i]; 52 sum -= A[j]; 53 i++; 54 j--; 55 } 56 } 57 if (i == j) 58 sum = Math.Abs(sum + A[i]) < Math.Abs(sum - A[i]) ? Math.Abs(sum + A[i]) : Math.Abs(sum - A[i]); 59 return Math.Abs(sum); 60 }
标签:
原文地址:http://www.cnblogs.com/HuoAA/p/4649936.html