Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

本周作业

简单:

  • 写一个关于 HashMap 的小总结。 说明:对于不熟悉 Java 语言的同学,此项作业可选做。
  • 有效的字母异位词(亚马逊、Facebook、谷歌在半年内面试中考过)
  • 两数之和(近半年内,亚马逊考查此题达到 216 次、字节跳动 147 次、谷歌 104 次,Facebook、苹果、微软、腾讯也在近半年内面试常考)
  • N 叉树的前序遍历(亚马逊在半年内面试中考过)
  • HeapSort :自学 https://www.geeksforgeeks.org/heap-sort/

中等:

下周预习

预习题目:

本周作业

简单:

  • 写一个关于 HashMap 的小总结。 说明:对于不熟悉 Java 语言的同学,此项作业可选做。

HashMap是以键值对的形式存储的,如:<Key,Value>;

HashMap如何快速的找到元素存放的位置并得到数据呢?

  • 是因为HashMap的内部是由Hash实现的,用Hash来计算要存储的位置,可以快速的拿到数据存放的位置。
  • 如果多个元素都在同一个HashMap的位置的话,就会自动形成一个链接的形式,一个接着一个。

HashMap取出复杂度是O(1)的;

class Solution {
    public boolean isAnagram(String s, String t) {
        Map<Character,Integer> map = new HashMap<Character,Integer>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            map.put(c,map.getOrDefault(c,0) + 1);
        }

        for (int i = 0; i < t.length(); i++) {
            char c = t.charAt(i);

            map.put(c,map.getOrDefault(c,0) - 1);

            if(map.get(c) < 0){
                return false;
            }else if(map.get(c) == 0){
                map.remove(c);
            }

        }

        return map.isEmpty();
    }
}
  • 两数之和(近半年内,亚马逊考查此题达到 216 次、字节跳动 147 次、谷歌 104 次,Facebook、苹果、微软、腾讯也在近半年内面试常考)
 public int[] twoSum(int[] nums, int target) {

        Map<Integer,Integer> map = new HashMap<Integer,Integer>();

        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],i);
            if(map.containsKey(target - nums[i])){
                return new int[]{map.get(target - nums[i]),i};
            }
            map.put(nums[i],i);
        }
        return new int[]{};
    }
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<Integer> preorder(Node root) {
        List<Integer> result = new ArrayList<>();

        build(result,root);

        return result;
    }

    public void build(List<Integer> result,Node root){

        if(root==null) return;
         result.add(root.val);

        for(int i=0;i<root.children.size();i++){
            build(result,root.children.get(i));
        }
    }
}

中等:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        build(result,root);
        return result;
    }

    public void build(List result,TreeNode root){
        if(root==null) return;
        
        build(result,root.left);
        result.add(root.val);
        build(result,root.right);
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        build(result,root);
        return result;
    }

    public void build(List list,TreeNode root){
        
        if(null == root) return;
        list.add(root.val);
        build(list,root.left);
        build(list,root.right);
        
    }

}