2019-10-12 刷题总结 -- hash table

2019-10-13  本文已影响0人  Leahlijuan
  1. Longest Substring Without Repeating Characters
    虽然这是一个hash table的题目,但是我的第一反应是用一个长为26的array来储存已经出现过的character信息的方法。但是我又想使用two pointer的方法,所以必须在挪动pointer的时候要知道start要挪向哪里。而只用array来记录信息的话只能记住在某个区间character出现的次数,无法记住character出现的位置。
    所以还是必须使用hash table的方法,用key来记录character,value来记录这个character最后出现的位置。
class Solution {
    public int lengthOfLongestSubstring(String s) {
        // key: character; value: the last appear index
        Map<Character, Integer> map = new HashMap<>();
        int maxCount = 0, count = 0, start = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                int pos = map.get(c);
                if (pos >= start) {
                    maxCount = Math.max(maxCount, count);
                    count = i - pos;
                    start = pos + 1;
                } else {
                    count++;
                }
            } else {
                count++;
            }
            map.put(c, i);
        }
        maxCount = Math.max(maxCount, count);
        return maxCount;
    }
}
  1. Copy List with Random Pointer

LinkedList的deep copy问题
这题想法挺简单的。先在每个node后面复制这个node,得到A -> A' -> B -> B'这种形式,在加上random node,最后把两个list分隔开。
但是实现过程中还是有很多坑的,因为是copy问题,所以最后的结果中不能将最初的list破坏。

/*
// Definition for a Node.
class Node {
    public int val;
    public Node next;
    public Node random;

    public Node() {}

    public Node(int _val,Node _next,Node _random) {
        val = _val;
        next = _next;
        random = _random;
    }
};
*/
class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return head;
        }
        // copy each node
        Node ptr = head;
        while (ptr != null) {
            Node newNode = new Node(ptr.val);
            Node next = ptr.next;
            ptr.next = newNode;
            newNode.next = next;
            ptr = next;
        }
        // add random to the new copied node
        ptr = head;
        while (ptr != null) {
            ptr.next.random = (ptr.random == null)? null: ptr.random.next;
            ptr = ptr.next.next;
        }
        // seperate these two node
        Node new_head = head.next, old_head = head;
        Node res = new_head;
        while (old_head != null && old_head.next.next != null) {
            old_head.next = new_head.next;
            new_head.next = new_head.next.next;
            old_head = old_head.next;
            new_head = new_head.next;
        }
        old_head.next = null;
        return res;
    }
}
  1. Happy Number
    这道题很明显应当用recursion的解法,但recursion需要base case,在这里bestcase 最小的即使单个数字,因此现自己计算个位数是否是happy number作为base case。
class Solution {
    public boolean isHappy(int n) {
        if (n <= 0) {
            return false;
        }
        // recursion 
        // base case: when n < 10, only when n = 1 or 7 it is happy number
        if (n < 10) {
            if (n == 1 || n == 7) {
                return true;
            }
            return false;
        }
        int sum = 0;
        while (n > 0) {
            sum += (n%10)*(n%10);
            n /= 10;
        }
        return isHappy(sum);
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读