|
亲!马上注册或者登录会查看更多内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
O(n^2), since in the while loop, k always increasing instead of reset when a new j comes...
- // http://www.geeksforgeeks.org/find-number-of-triangles-possible/
- static int countTriangleTripleIndex(int [] A) {
- Arrays.sort(A);
- int cnt = 0;
- for(int i=0; i<=A.length-3; i++) {
- int k = i + 2;
- for(int j=i+1; j<A.length; j++) {
- while(k<A.length && A【i】+A[j]>A[k])
- k++;
- cnt += Math.max(0, k-j-1);
- }
- }
- return cnt;
- }
复制代码
|
|