📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: love1point
跳转到指定楼层
上一主题 下一主题
收起左侧

总结能用上HashMap的题

🔗
 楼主| love1point 2015-6-13 09:09:47 | 只看该作者
全局:
adiggo 发表于 2015-6-13 02:27
lc 应该不管空间的。

嗯,lc不管空间,但lc note说 “Could you implement it without using extra memory?”
多谢回复
回复

使用道具 举报

🔗
jaly50 2015-6-19 05:17:05 | 只看该作者
全局:
第八题: 用hashmap: time o(n), space o(n)
  1. public class Solution {
  2.     /**
  3.      * @param nums: A list of integers
  4.      * @return: A list of integers includes the index of the first number
  5.      *          and the index of the last number
  6.      */
  7.     public ArrayList<Integer> subarraySum(int[] nums) {
  8.         // write your code here
  9.         // Store index and sum
  10.         Map<Integer,Integer> map = new HashMap<Integer, Integer>();
  11.         ArrayList<Integer> res = new ArrayList<Integer>();
  12.         int sum = 0;
  13.         map.put(0,-1);
  14.         for (int i=0; i<nums.length; i++) {
  15.             sum +=nums[i];
  16.             if (map.containsKey(sum)) {
  17.                 res.add(map.get(sum)+1);
  18.                 res.add(i);
  19.                 return res;
  20.             }
  21.             else
  22.             map.put(sum, i);
  23.         }
  24.         return res;
  25.     }
  26. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2015-6-19 08:39:30 | 只看该作者
全局:
jaly50 发表于 2015-6-19 05:17
第八题: 用hashmap: time o(n), space o(n)

是的哈。多谢参与哈
回复

使用道具 举报

全局:
谢谢分享。赞一个。。一起刷题努力!我也现在找工作。
回复

使用道具 举报

🔗
syjohnson 2015-9-12 05:10:44 | 只看该作者
全局:
回复

使用道具 举报

🔗
plich 2015-9-18 22:35:06 | 只看该作者
全局:
Single number用hash table会比直接xor快么?
回复

使用道具 举报

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

本版积分规则

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