zoukankan      html  css  js  c++  java
  • 社交关系中的共同好友数目计算(一度好友与二度好友)

     1 public class test1 {
     2     /**
     3      * 思路:如果A与某人有共同好友,则该人必出现在二度好友中
     4      *     不在二度好友中的,则与A的共同好友数必然为0
     5      *     故只需遍历所有的二度好友即可,但是不要将那些无共同好友的一度好友忘记!也就是初始化firstout 
     6      * @param args
     7      */
     8     
     9 
    10     // directs为A的一度好友list,indirects为Map<String,List<String>代表一度好友的一度好友(不包括A)映射关系表
    11     // firstout为A与其一度好友的共同好友数Map,secondout为A与其二度好友的共同好友数Map
    12     public static void main(String[] args) {
    13         
    14         String directsString = "BDEH";
    15         String bString = "CD";
    16         String dString = "B,E,G,G";
    17         String eString = "C,D";
    18         String hString = "I";
    19         List<String> directs = stringToList(directsString);
    20         
    21         
    22         Map<String, List<String>> indirects = new HashMap<String, List<String>>();
    23         indirects.put("B", stringToList(bString));
    24         indirects.put("D", stringToList(dString));
    25         indirects.put("E", stringToList(eString));
    26         indirects.put("H", stringToList(hString));
    27         
    28         Map<String, Integer> firstout = new HashMap<String, Integer>();//用于存放A与一度好友的共同好友数目
    29         Map<String, Integer> secondout = new HashMap<String, Integer>();//用于存放A与二度好友的共同好友数目
    30         
    31         //由于遍历所有二度好友,先去考虑在不在fistout中,如果不在则必为二度好友,然后才去考虑在不在secondout中做最后处理,
    32         //故firstout需要进行初始化,保证fistout+secondout包含所有A的一度好友与二度好友
    33         for(String str : directs) {
    34             firstout.put(str, 0);
    35         }
    36         
    37         for(Map.Entry<String, List<String>> entry : indirects.entrySet()) {
    38             for(String id2 : entry.getValue()) {
    39                 Integer value = firstout.get(id2);
    40                 if(value != null) {
    41                     firstout.put(id2, value+1);
    42                 }else {
    43                     if(secondout.get(id2) == null) {
    44                         secondout.put(id2, 1);
    45                     }else {
    46                         secondout.put(id2, secondout.get(id2) + 1);
    47                     }
    48                 }
    49             }
    50         }
    51         
    52         System.out.println("A与一度好友B的共同好友数: " + firstout.get("B"));
    53         System.out.println("A与一度好友D的共同好友数: " + firstout.get("D"));
    54         System.out.println("A与二度好友C的共同好友数: " + secondout.get("C"));
    55         System.out.println("A与二度好友I的共同好友数: " + secondout.get("I"));
    56     }
    57 
    58     public static List<String> stringToList(String str) {
    59         List<String> list = new ArrayList<String>();
    60         for (int i = 0; i < str.length(); i++) {
    61             list.add(str.charAt(i) + "");
    62         }
    63         return list;
    64     }
    65 }

    输出结果:

    A与一度好友B的共同好友数: 1
    A与一度好友D的共同好友数: 2
    A与二度好友C的共同好友数: 2
    A与二度好友I的共同好友数: 1

  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/leehfly/p/4957421.html
Copyright © 2011-2022 走看看