Java——HashMap
- 获取数组长度
- 数组.length
- 获取下标
- HashMap
HashMap 构造函数
1 // 默认构造函数。 2 HashMap() 3 4 // 指定“容量大小”的构造函数 5 HashMap(int capacity) 6 7 // 指定“容量大小”和“加载因子”的构造函数 8 HashMap(int capacity, float loadFactor) 9 10 // 包含“子Map”的构造函数 11 HashMap(Map<? extends K, ? extends V> map)
HashMap API
1 void clear() 2 Object clone() 3 boolean containsKey(Object key) 4 boolean containsValue(Object value) 5 Set<Entry<K, V>> entrySet() 6 V get(Object key) 7 boolean isEmpty() 8 Set<K> keySet() 9 V put(K key, V value) 10 void putAll(Map<? extends K, ? extends V> map) 11 V remove(Object key) 12 int size() 13 Collection<V> values()
HashMap例子
1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 12 13 public TreeNode reConstructBinaryTree(int [] pre,int [] in) { 14 if(pre==null||in==null){ 15 return null; 16 } 17 18 java.util.HashMap<Integer,Integer> map= new java.util.HashMap<Integer, Integer>(); 19 for(int i=0;i<in.length;i++){ 20 map.put(in[i],i); 21 } 22 return preIn(pre,0,pre.length-1,in,0,in.length-1,map); 23 } 24 25 public TreeNode preIn(int[] p,int pi,int pj,int[] n,int ni,int nj,java.util.HashMap<Integer,Integer> map){ 26 27 if(pi>pj){ 28 return null; 29 } 30 TreeNode head=new TreeNode(p[pi]); 31 int index=map.get(p[pi]); 32 head.left=preIn(p,pi+1,pi+index-ni,n,ni,index-1,map); 33 head.right=preIn(p,pi+index-ni+1,pj,n,index+1,nj,map); 34 return head; 35 } 36 37 38 39 }
posted on 2019-02-26 22:34 Pusteblume2018 阅读(113) 评论(0) 编辑 收藏 举报