码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-10-24 13:17:41      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:tin   put   数据   map   unique   dex   contain   contains   star   

区间类问题

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

 

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

标签:tin   put   数据   map   unique   dex   contain   contains   star   

原文地址:http://www.cnblogs.com/whesuanfa/p/7722837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!