Leetcode - task schedule

http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=167349&extra=&highlight=facebook&page=1

题目在这个面经里。

My code:

import java.util.HashMap;

public class Solution {
    private String task(String s, int cold) {
        if (s == null || s.length() == 0)
            return null;
        int time = 0;
        HashMap<Character, Integer> tracker = new HashMap<Character, Integer>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char curr = s.charAt(i);
            time++;
            if (tracker.containsKey(curr)) {
                int oldTime = tracker.get(curr);
                if (time - oldTime > cold) {
                    tracker.put(curr, time);
                    sb.append(curr);
                }
                else {
                    int temp = oldTime + cold + 1;
                    for (int j = time; j < temp; j++) {
                        sb.append("_");
                    }
                    sb.append(curr);
                    tracker.put(curr, temp);
                    time = temp;
                }
                    
            }
            else {
                tracker.put(curr, time);
                sb.append(curr);
            }
        }
        return sb.toString();
    }
    
    
    
    public static void main(String[] args) {
        Solution test = new Solution();
        System.out.println(test.task("ABAABB", 2));
    }
}

我只写了一个简单版本,没有实现最优。
Anyway, Good luck, Richardo!

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/52328f8b28e7#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞