📣 4th of July限时特惠: VIP通行证立减$68
查看: 971| 回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

在职准备换工作,记录刷题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
工作一年半,看着地里的大包裹,甚是羡慕。开贴记录刷题换工作

上一篇:在职跳槽刷题
下一篇:每日刷题-随时保持可以换工作面试状态
推荐
 楼主| Ellachen 2019-1-1 11:19:29 | 只看该作者
全局:
google 399. Evaluate Division
回复

使用道具 举报

🔗
 楼主| Ellachen 2018-12-10 15:39:36 | 只看该作者
全局:
今天刷了787 Cheapest Flights within K stops. 这题以前刷过,并通过了Leetcode的测试;可是最近Leetcode加了新的test cases, 原来的算法无法通过测试。用BFS + dijkstra
回复

使用道具 举报

🔗
 楼主| Ellachen 2018-12-12 16:38:50 | 只看该作者
全局:
今天刷了Google 的904 Fruit Into Baskets
  1. class Solution {
  2.     public int totalFruit(int[] tree) {
  3.         int len = tree.length;
  4.         if (len <= 2) return tree.length;
  5.         int max = 2;
  6.         int start = 0;
  7.         int ind1 = 0;
  8.         int ind2 = 1;
  9.         while(ind2 < len && tree[ind2] == tree[ind1]) ind2++;
  10.         if (ind2 >= len - 1) return len;
  11.         for (int i = ind2 + 1; i <= len; i++) {
  12.             if (i == len || (tree[i] != tree[ind1] && tree[i] != tree[ind2])) {
  13.                 if (i - start > max) max = i - start;
  14.                 start = Math.max(ind1, ind2);
  15.                 ind1 = start;
  16.                 ind2 = i;
  17.             } else if (tree[i] == tree[i - 1]) {
  18.                 // pass;
  19.             } else if (tree[i] == tree[ind1]) {
  20.                 ind1 = i;
  21.             } else if (tree[i] == tree[ind2]) {
  22.                 ind2 = i;
  23.             }
  24.         }
  25.         return max;
  26.     }
  27. }
复制代码
回复

使用道具 举报

🔗
 楼主| Ellachen 2018-12-13 15:59:11 | 只看该作者
全局:
Google 482. License Key Formatting
  1. class Solution {
  2.     public String licenseKeyFormatting(String S, int K) {
  3.         int NAlphanumeric = 0;
  4.         for (char c : S.toCharArray()) {
  5.             if (c != '-') NAlphanumeric++;
  6.         }
  7.         if (NAlphanumeric == 0) return "";
  8.         int NFirst = NAlphanumeric % K;
  9.         if (NFirst == 0) NFirst = K;
  10.         int len = NFirst + (NAlphanumeric - NFirst) / K * (K + 1);
  11.         char[] chs = new char[len];
  12.         int i = 0;
  13.         for (char c : S.toCharArray()) {
  14.             if (c == '-') continue;
  15.             char C = c;
  16.             if ('a' <= c && c <= 'z') C = Character.toUpperCase(c);
  17.             if ((i - NFirst) % (K + 1) == 0) {
  18.                 chs[i] = '-';
  19.                 i++;
  20.             }
  21.             chs[i] = C;
  22.             i++;
  23.         }
  24.         return new String(chs);   
  25.     }
  26. }
复制代码
回复

使用道具 举报

🔗
 楼主| Ellachen 2018-12-19 15:18:34 | 只看该作者
全局:
刷了上周六Leetcode Contest的四道题目。
回复

使用道具 举报

🔗
 楼主| Ellachen 2019-1-4 14:04:19 | 只看该作者
全局:
google 135, 299, 686
回复

使用道具 举报

🔗
 楼主| Ellachen 2019-1-7 04:07:22 | 只看该作者
全局:
周六的Leetcode contest

注意java细节:
1.  result of `10 ^ 1` is 11.  `^` means bit wise xor. Use `(int) Math.pow(10, 1)` means power.
2. result of `string.split(".") ` is an array of empty strings. The signature of this function is `String.split(regular_expression)`. If we'd like to split a string by ".", we have to escape it by `string.split("\\.")`.
3. bound of integer is [-2^31, 2^31-1).
回复

使用道具 举报

🔗
 楼主| Ellachen 2019-1-10 16:36:11 | 只看该作者
全局:
307 Range Sum Query - Mutable
自己想出来了一种 binary encoding 的算法

  1. class NumArray {
  2.     List<int[]> list;

  3.     public NumArray(int[] nums) {
  4.         list = new ArrayList<>();
  5.         int[] arr = nums;
  6.         while (true) {
  7.             list.add(arr);
  8.             if (arr.length <= 1) break;
  9.             int[] sub = new int[(arr.length + 1) / 2];
  10.             for (int i = 0; i < arr.length / 2; i++) sub[i] = arr[i * 2] + arr[i * 2 + 1];
  11.             if (arr.length % 2 == 1) sub[arr.length / 2] = arr[arr.length - 1];
  12.             arr = sub;
  13.         }
  14.     }
  15.    
  16.     // sum of nums[0], nums[1], ..., nums[k - 1];
  17.     public int sumUntil(int k) {
  18.         if (k == 0) return 0;
  19.         int result = 0;
  20.         int ord = k;
  21.         for (int i = 0; i < list.size(); i++) {
  22.             if (ord == 0) break;
  23.             if (ord % 2 == 1) result += list.get(i)[ord - 1];
  24.             ord = ord >> 1;
  25.         }
  26.         return result;
  27.     }
  28.    
  29.     public void update(int i, int val) {
  30.         int delta = val - list.get(0)[i];
  31.         int ord = i;
  32.         for (int level = 0; level < list.size(); level++) {
  33.             list.get(level)[ord] += delta;
  34.             ord = ord >> 1;
  35.         }
  36.     }
  37.    
  38.     public int sumRange(int i, int j) {
  39.         return sumUntil(j + 1) - sumUntil(i);
  40.     }
  41. }
复制代码
回复

使用道具 举报

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

本版积分规则

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