📣 独立日限时特惠: VIP通行证立减$68
12
返回列表 发新帖
楼主: loserloser
跳转到指定楼层
上一主题 下一主题
收起左侧

bb 电面跪经

🔗
FF-Ti 2017-10-28 05:37:35 | 只看该作者
全局:
loserloser 发表于 2017-10-28 05:25
能给个用hashmap的完整解法吗

public static int find(int target, int[] nums) {
                if (nums == null || nums.length == 0) {
                        return 0;
                }
                Set<Integer> set = new HashSet<>();
                Map<Integer, Integer> map = new HashMap<>();
                boolean sameNum = false;
                for (int i: nums) {
                        if (set.contains(target - i)) {
                                if (i * 2 == target) {
                                        sameNum = true;
                                }
                                if (!map.containsKey(i)) {
                                        map.put(i, target - i);
                                        map.put(target -  i, i);
                                }
                        }
                        set.add(i);
                }
                return sameNum? (map.size() + 1) / 2: map.size() / 2;
        }
回复

使用道具 举报

🔗
bigteemo 2017-11-4 20:52:01 | 只看该作者
全局:
FF-Ti 发表于 2017-10-28 05:37
public static int find(int target, int[] nums) {
                if (nums == null || nums.length == 0) {
                        re ...

这是我写的
    private static int findPairs(int[] nums, int target) {
        Set<Integer> set = new HashSet<>();
        Set<Integer> res = new HashSet<>();

        for (int num : nums) {
            if (set.contains(target - num)) {
                if (!res.contains(num) && !res.contains(target - num)) {
                    res.add(num);
                }
            } else {
                set.add(num);
            }
        }
        return res.size();
    }
回复

使用道具 举报

🔗
844587076 2017-11-26 16:41:00 | 只看该作者
全局:

一个hash就够了,没必要两个,先查hash.contains(num), if not, then check whether the hash.contains(sum-num); if yes, continue. because u have check it already.  
回复

使用道具 举报

🔗
ZZZuko 2017-11-27 01:00:08 | 只看该作者
全局:
第一题  我之前面了一模一样的题。 不过 我面试时间就不到一小时。  没有 后面两道
回复

使用道具 举报

🔗
PepePls 2017-11-29 19:59:15 | 只看该作者
全局:
844587076 发表于 2017-11-26 16:41
一个hash就够了,没必要两个,先查hash.contains(num), if not, then check whether the hash.contains(s ...

一个hash什么时候insert, 什么时候update result啊
回复

使用道具 举报

🔗
844587076 2017-12-1 00:29:38 | 只看该作者
全局:
czcbangkai 发表于 2017-11-29 19:59
一个hash什么时候insert, 什么时候update result啊

lc two sum, 扫一遍的做法,跟这个一样原理,你看看discussion
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表