楼主: yimeichihuo2
跳转到指定楼层
上一主题 下一主题
收起左侧

字节跳动挂经

全局:
tanj 发表于 2020/05/14 07:21:46
他的解法好像没什么问题吧, 用他的方法解cbacbc的话先是 cba -> bac -> acb ->...
好像是…不过前面cdacd也过不了

补充内容 (2020-5-13 18:24):
cda - cda - cad ❌
正确答案acd
回复

使用道具 举报

🔗
ozox 2020-5-14 09:43:04 | 只看该作者
全局:

Follow the algorithm, result = acd
回复

使用道具 举报

全局:
ozox 发表于 2020/05/14 09:43:04
Follow the algorithm, result = acd
老哥 你的解法跑出来是cda - cda - cad
回复

使用道具 举报

全局:
第一题是刷题网316

评分

参与人数 2大米 +3 收起 理由
麻倉枼 + 1 很有用的信息!
yimeichihuo2 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
ozox 2020-5-14 12:30:09 | 只看该作者
全局:
qiaofengmarco 发表于 2020-5-14 10:18
老哥 你的解法跑出来是cda - cda - cad

sorry, the algorithm is not correct. turns out to be a hard one at lc
回复

使用道具 举报

🔗
 楼主| yimeichihuo2 2020-5-14 13:50:11 | 只看该作者
全局:
guzhiyan 发表于 2020-5-14 11:19
第一题是刷题网316

谢谢,我才发现这题我做过,呵呵。。。
回复

使用道具 举报

🔗
18321738697 2020-5-16 05:38:48 | 只看该作者
全局:
第一题https://leetcode.com/problems/re ... 99-Stack-Dictionary
第二题也是刷题网原题,
我面的也是相同的题2轮,请问楼主下一轮有消息了吗
回复

使用道具 举报

🔗
 楼主| yimeichihuo2 2020-5-16 06:45:57 来自APP | 只看该作者
全局:
18321738697 发表于 2020/05/16 05:38:48
第一题https://leetcode.com/problems/remove-duplicate-letters/di...
被拒了🤦‍♀️
第二题方便告知一下题号吗?谢谢了
回复

使用道具 举报

🔗
ozox 2020-5-17 09:32:53 | 只看该作者
全局:
第一题真是不容易bug free啊:

    string removeDuplicateLetters(string s) {
        
        if (s.empty() || s.size() == 1)
            return s;
        
        vector<int>  count(26, 0); // Count each char in the string
        vector<int>  used (26, 0); // 1 if used, 0 otherwise
        
        // Preprocess: get the count of each char and num of unique chars
        int unique = 0;
        for(char c: s)
        {
            if (count[c-'a'] == 0)
            {
                unique ++;
            }
            
            count[c-'a'] ++;
        }
        
        // Result vector
        vector<char> result(unique, 0);
        int cur = 0;
        for(char c: s)
        {
            while(cur > 0 && (used[c-'a'] == 0) && (c < result[cur-1]) && (count[result[cur-1] - 'a'] >= 1))
            {
                used[result[cur-1] - 'a'] = 0; // Unuse this char
                result[--cur] = c;
            }
            
            if (used[c-'a'] == 0)
            {
                used[c-'a'] = 1;
                result[cur++] = c;
            }
            
            count[c-'a'] --;   
        }
        
        string ret_s(result.begin(), result.end());
        return ret_s;      
    }
};
回复

使用道具 举报

🔗
melanthafu 2020-5-18 10:59:22 | 只看该作者
全局:
遇见 楼上们的答案可以 但是这么想 删除 然后保留下来的 最小最大或者 字典序最小最大 马上要反映到 这个一个匹配的问题从而想到单调栈
贴代码
class Solution {
    public String smallestSubsequence(String text) {
        Deque<Integer> stack = new LinkedList<>();
        int[] last =  new int[26];
        boolean[] used = new boolean[26];
        for (int i = 0; i < text.length(); i++) {
            last[text.charAt(i) - 'a'] = i;
        }
        for (int i = 0; i < text.length(); i++) {
            int ch = text.charAt(i) - 'a';
            if (used[ch]) {
                continue;
            }
            used[ch] = true;
            while (!stack.isEmpty() && stack.peekLast() > ch
                   && i < last[stack.peekLast()]) {
                int released = stack.pollLast();
                used[released] = false;
            }
            stack.offerLast(ch);
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append((char) (stack.pollFirst() + 'a'));
        }
        return sb.toString();
    }
}

还有一道比较好的单调栈 是LC 409 楼主可以感受一下 这里面的套路

class Solution {
    public String removeKdigits(String num, int k) {
        int n = num.length();
        if (n == k) {
            return "0";
        }
        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            while (!stack.isEmpty() && stack.peekLast() > num.charAt(i) && k > 0) {
                stack.pollLast();
                k--;
            }
            stack.offerLast(num.charAt(i));
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty() && stack.peekFirst() == '0') {
                stack.pollFirst();
        }
        while (!stack.isEmpty() && k > 0) {
            stack.pollLast();
            k--;
        }
        while (!stack.isEmpty()) {
            sb.append(stack.pollFirst());
        }
        return sb.length() == 0 ? "0" : sb.toString();
    }
}

第二题国内高频题
https://zhuanlan.zhihu.com/p/65065359

希望能帮到lz

评分

参与人数 3大米 +6 收起 理由
AIBilly + 2 给你点个赞!
yimeichihuo2 + 2 赞一个!
yanmoviola + 2 很有用的信息!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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