题解 | 谐距下标对

谐距下标对

https://www.nowcoder.com/practice/12fd032361704c978bcb9c2c2b3bb93d

将条件转化转化为ai-i=aj-j

使用HashMap存储相同的,然后组合。

参考:https://blog.nowcoder.net/n/8d233209d79f4a6297725d9f6f1c91d2

import java.util.Scanner;

import java.util.Map;
import java.util.HashMap;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        Map<Integer,Integer> map=new HashMap<>();
        for(int i=0;i<n;i++){
            int a=in.nextInt();
            int diff=a-i;
            map.put(diff,map.getOrDefault(diff,0)+1);
        }
        long  result=0;
        for(int count:map.values()){
            if(count>1){
                result+=(long)count*(count-1)/2;
            }

        }
        System.out.println(result);
    }
}

全部评论

相关推荐

LZHR:老哥你从投递简历测评完到一面中间隔了多久呀,我这边已经过了五天了仍显示简历筛选中是不是就是挂了
腾讯求职进展汇总
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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