第三章 基础算法和数据结构高频题 I

区间类问题

1 Missing Interval

    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> res = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return res;
        }
        addRange(res, lower, (long)nums[0] - 1);
        for (int i = 1; i < nums.length; i++) {
            addRange(res, (long)nums[i - 1] + 1, (long)nums[i] - 1);
        }
        addRange(res, (long)nums[nums.length - 1] + 1, upper);
        return res;
    }
    
    void addRange(List<String> res, long l, long r) {
        if (l > r) {
            return;
        }
        if (l == r) {
            res.add(l + "");
            return;
        }
        res.add(l + "->" + r);
    }
View Code

2 Merge intervals

    public List<Interval> merge(List<Interval> inte) 
    {  
        List<Interval> res = new ArrayList<>();
        inte.sort(Comparator.comparing(i -> i.start));
        Interval last = null;
        for (Interval item : inte) {
            if (last == null || last.end < item.start) {
                res.add(item);
                last = item;
            } else {
                last.end = Math.max(item.end, last.end);
            }
        }
        return res;
    }
View Code

3 Insert interval

    public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) 
    {  
        ArrayList<Interval> res = new ArrayList<Interval>();
        int id = 0;
        while (id < intervals.size() && intervals.get(id).start < newInterval.start) {
            id++;
        }
        intervals.add(id, newInterval);
        Interval last = null;
        for (Interval item : intervals) {
            if (last == null || last.end < item.start) {
                res.add(item);
                last = item;
            } else {
                last.end = Math.max(item.last, last.end);
            }
        }
        return res;
    }
View Code

Hash  字符/ 字符串统计类问题

4 First Position Unique Character

    public int firstUniqChar(String s) {
        // write your code here\
        int[] num = new int[256];
        for (char c : s.toCharArray()) {
            num[c]++;
        }
        for (int i = 0; i < s.length(); i++) {
            if (num[s.charAt(i)] == 1) {
                return i;
            }
        }
        return -1;
    }
View Code

5 Substring Anagrams

  public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList<>();
        if (s.length() < p.length()) {
            return res;
        }
        char[] cs = s.toCharArray();
        char[] ps = p.toCharArray();
        int[] det = new int[256];
        int absSum = 0;
        for (int i = 0; i < p.length(); i++) {
            det[cs[i]]++;
            det[ps[i]]--;
        }
        for (int i = 0; i < 256; i++) {
            absSum += Math.abs(det[i]);
        }
        if (absSum == 0) {
            res.add(0);
        }
        for (int i = p.length(); i < s.length(); i++) {
            char l = cs[i - ps.length];
            char r = cs[i];
            absSum = absSum - Math.abs(det[l]) - Math.abs(det[r]);
            
            det[l]--;
            det[r]++;
            
            absSum = absSum + Math.abs(det[l]) + Math.abs(det[r]);
            if (absSum == 0) {
                res.add(i - ps.length + 1);
            }
        }
        return res;
    }
View Code

6 Word Abbreviation Set

class VaildWordAbbr {
    private Map<String, Integer> dict = new HashMap<>();
    private Map<String, Integer> abbr = new HashMap<>();
    public VaildWordAbbr(String[] dic) {
        for (String str : dic) {
            dict.put(str, dict.getOrDefault(str, 0) + 1);
            String abb = getAbbr(str);
            dict.put(abb, dict.getOrDefault(abb, 0) + 1);
        }
    }
    
    public boolean isUnique(String word) {
        String a = getAbbr(word);
        return dict.get(word) == abbr.get(a);
    }
    
    String getAbbr(String word) {
        if (word.length() < 3) {
            return word;
        }
        return "" + word.charAt(0) + (word.length() - 2) + word.charAt(word.length() - 1);
    }
View Code

7 Longest Consecutive Sequence

    public int longestConsecutive(int[] num) 
    {
        // write you code here
        if (num == null || num.length == 0)
        {
            return 0;
        }
        HashSet<Integer> set = new HashSet<Integer>();
        for (int i : num) {
            set.add(i);
        }
        int res = 0;
        for (int item : num) {
            if (set.contains(item)) {
                set.remove(item);
                int l = item - 1;
                int r = item + 1;
                while (set.contains(l)) {
                    set.remove(l);
                    l--;
                }
                while (set.contains(r)) {
                    set.remove(r);
                    r++;
                }
                res = Math.max(res, r - l - 1);
            }
        }
        return res;
    }
View Code

8 Load Balancer

 

class LoadBalance {
    private List<Integer> list = new ArrayList<>();
    private Map<Integer, Integer> map = new HashMap<>();
    private int n = 0;
    private Random r = new Random();
    public LoadBalance() {
        
    }
    
    public void add(int id) {
        if (!map.containsKey(id)) {
            list.add(id);
            map.put(n, id);
            n++;
        }
    }
    
    public void remove(int id) {
        if (map.containsKey(id)) {
            int lastItem = list.get(n - 1);
            int removeIndex = map.get(id);
            
            map.put(lastItem, removeIndex);
            list.set(removeIndex, lastItem);
            list.remove(n - 1);
            map.remove(id);
            n--;
        }
    }
    
    public int pick() {
        return list.get(r.nextInt(n));
    }
    
}
View Code

 

posted on 2017-10-24 12:10  wheleetcode  阅读(208)  评论(0编辑  收藏  举报