JAVA,思路简单,但没啥优化的方法~~

数组求和统计

http://www.nowcoder.com/questionTerminal/efc16ce46397436a8d1a0008c52093c1

public class Solution {
    /**
     * 计算有多少对符合条件的数对
     * @param a int整型一维数组 数组a
     * @param b int整型一维数组 数组b
     * @return int整型
     */
    public int countLR (int[] a, int[] b) {
        // write code here
        int count = 0;
        int len = a.length;
        for(int i = 0;i<len;i++){
            if(a[i] == 2*b[i]) count++;
        }
        for(int low = 0;low < len-1;low++){
            int x = a[low];
            for(int fast=low+1;fast<len;fast++){
                int y = b[low] + b[fast];
                x += a[fast];
                if(x == y) count++;
            }
        }
        return count;
    }
}

全部评论

相关推荐

评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务