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

2016.3.11. google一轮电面

🔗
 楼主| WayneBit 2016-3-21 00:40:07 | 只看该作者
全局:
guixi107 发表于 2016-3-20 15:06
lz这样好像不行啊

现在给的是 iterator的接口,一旦访问过某个char,没法再访问一次的。

存local中存在的char和其对应出现的次数
回复

使用道具 举报

🔗
bobzhang2004 2016-3-25 22:56:51 | 只看该作者
全局:
请问楼主面完几天后出的结果?
回复

使用道具 举报

🔗
bobzhang2004 2016-3-25 23:36:59 | 只看该作者
全局:
slashGu 发表于 2016-3-19 05:05
hash table 加双指针,维持hash table的尺寸小于等于2,更新每个字符的下标。

这个是stream, 得存下从start开始的数据吧
回复

使用道具 举报

🔗
bobzhang2004 2016-3-26 04:45:53 | 只看该作者
全局:
写了下code, 感觉应该是要记录下部分string的
  1. public class LongestSubstringWithAtMostKDistinctCharactersStream {

  2.         static class StreamReader {
  3.                 int pos = 0;
  4.                 String str;

  5.                 public StreamReader(String str) {
  6.                         this.str = str;
  7.                 }

  8.                 char next() {// return next character
  9.                         return str.charAt(pos++);
  10.                 }

  11.                 boolean hasNext() {
  12.                         return pos < str.length();
  13.                 }
  14.         }

  15.         public static String lengthOfLongestSubstringKDistinct(StreamReader s, int k) {
  16.                 if (s == null || !s.hasNext()) {
  17.                         return "";
  18.                 }
  19.                 HashMap<Character, Integer> map = new HashMap<Character, Integer>();
  20.                 int max = 0;
  21.                 StringBuilder sb = new StringBuilder();
  22.                 String res = "";
  23.                 while (s.hasNext()) {
  24.                         char c = s.next();
  25.                         sb.append(c);
  26.                         if (!map.containsKey(c)) {
  27.                                 map.put(c, 1);
  28.                                 while (sb.length() > 0 && map.size() > k) {
  29.                                         char ch = sb.charAt(0);
  30.                                         sb.deleteCharAt(0);
  31.                                         map.put(ch, map.get(ch) - 1);
  32.                                         if (map.get(ch) == 0) {
  33.                                                 map.remove(ch);
  34.                                         }
  35.                                 }
  36.                         } else {
  37.                                 map.put(c, map.get(c) + 1);
  38.                         }
  39.                         if (sb.length() > max) {
  40.                                 max = sb.length();
  41.                                 res = sb.toString();
  42.                         }
  43.                 }

  44.                 return res;
  45.         }
  46.        
  47.         public static void main(String[] args) {
  48.                 StreamReader sr = new StreamReader("ababcbcbaaabbdef");
  49.                 System.out.println(lengthOfLongestSubstringKDistinct(sr, 2));
  50.                 StreamReader sr1 = new StreamReader("ababc");
  51.                 System.out.println(lengthOfLongestSubstringKDistinct(sr1, 2));
  52.                
  53.         }
  54. }
复制代码
回复

使用道具 举报

🔗
 楼主| WayneBit 2016-3-26 12:04:05 | 只看该作者
全局:
bobzhang2004 发表于 2016-3-25 22:56
请问楼主面完几天后出的结果?

面完一周催了一下,没有回复。估计默剧了。
回复

使用道具 举报

🔗
bobzhang2004 2016-3-26 20:40:24 | 只看该作者
全局:
[quote][url=forum.php?mod=redirect

请问现在都没消息吗?
回复

使用道具 举报

🔗
tigercode 2016-9-14 13:13:59 | 只看该作者
全局:
出题出成streamReader这个样子,估计是给不用java的人吧。。。anyway, 如果需要return substring,那就queue+map<char, count>, 如果不需要substring,只需要size,就可以two point+map<char, lastIdx>,另外如果想更快的找到最小lastIdx,可以加个minHeap
回复

使用道具 举报

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

本版积分规则

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