招行信用卡中心-数据方向:

编程题:信用卡推荐用户列表,求大神解答~#腾讯##华为#
全部评论
我一开始也是83.33%,然后发现我把第一个出现的节点当成根节点了,实际上根节点就是"A",改完之后就100%,不知道大家是不是也这样。 我的代码已经开了一帖: https://www.nowcoder.com/discuss/41237?type=0&order=0&pos=45&page=0
点赞 回复 分享
发布于 2017-09-14 00:28
def find_child(node):     temp_list = recommend.get(node)     if temp_list is not None:         temp_result = len(temp_list) + find_child(temp_list[0])         if len(temp_list) > 1:             temp_result += find_child(temp_list[1])         recommend_num[node] = temp_result         return temp_result     recommend_num[node] = 0     return 0 这里是递归部分的代码。recommend 是一个字典,key是推荐人,value是被推荐人的list。 recommend_num 是存放推荐个数的字典。key是推荐人,value是被推荐人个数。 AC-100%
点赞 回复 分享
发布于 2017-09-14 00:00
package SomeQuizs; import java.util.HashMap; import java.util.Scanner; /** * 使用HashMap<Character,String>的数据结构,近似地实现了一个二叉树 * */ public class Zhaohang1 { static HashMap<Character,String> map = new HashMap<>(); public static void main(String[] args) { //接受输入,String数组arr和阈值num solution(arr,num); } private static void solution(String[] array,int num){ //构建Map for(String x:array){ char key = x.charAt(0); //A B C,取出A String value = x.substring(2); //A B C,取出B C map.put(key, value); } //遍历Key for(Character c:map.keySet()){ int count = count(c); if(count >= num){ System.out.println(c+" "); } } } private static int count(Character c){ if(c=='*' || !map.containsKey(c)) return 0; int count = 0; String child = map.get(c); if(child.charAt(0) != '*') count++; if(child.charAt(2) != '*') count++; //访问左右子树 count += count(child.charAt(0)) + count(child.charAt(2)); return count; } }
点赞 回复 分享
发布于 2017-09-13 23:47
我也是,我觉得是题目有问题!
点赞 回复 分享
发布于 2017-09-13 23:06
#include <iostream> #include <vector> #include <map> using namespace std; int main() { int m, n; cin >> m >> n; map<char, int> res; //记录发展的下线的总人数 map<char, char> _map; //记录是被谁推荐的 for (int i = 0; i < m; i++) { char c1, c2, c3; cin >> c1 >> c2 >> c3; if (c2 != '*') { _map[c2] = c1; res[c1]++; } if (c3 != '*') { _map[c3] = c1; res[c1]++; } int temp = res[c1]; while (_map.find(c1) != _map.end()) { res[_map[c1]] += temp; c1 = _map[c1]; } } vector<char> resList; for (map<char, int>::iterator it = res.begin(); it != res.end(); it++) { if (it->second >= n) resList.push_back(it->first); } if (resList.empty()) cout << "None"; else cout << resList[0]; for (int i = 1; i < resList.size(); i++) { cout << ' ' << resList[i]; } return 0; } 只通过了83.33%。。。
点赞 回复 分享
发布于 2017-09-13 22:24
递归a的长度=a的元素个数+每个元素长度
点赞 回复 分享
发布于 2017-09-13 22:12
没做出来,求解答
点赞 回复 分享
发布于 2017-09-13 22:09
只过了86.333%,不知道有什么边界没考虑
点赞 回复 分享
发布于 2017-09-13 22:04
我也没做出来,同求解答,感觉是建立二叉树,再求树的深度,奈何树建不出来,还耽误了问答题的时间,心好塞😭
点赞 回复 分享
发布于 2017-09-13 22:02

相关推荐

04-28 22:33
已编辑
门头沟学院 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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