最近刷刷leetcode,有些数据结构的常规操作,这个很啰嗦,很多细节所以要常常搜索。网上的东西总结的我觉得都不大好,详细的太啰嗦,简单的说不清。所以自己记录一下。
1.hashmap(lc1)哈希表
1.1. 创建 Map<Integer,Integer> hashMap=new HashMap<>(); 第一个是key,第二个是value,注意相同的key是存不进去的
1.2 查看是否包含对应的key:hashMap.containsKey(temp)
根据value找key了,当然有可能存在多个,效率想想就很低:hashMap.containsValue(value)
1.3 存键值对 :hashMap.put(key,value)
1.4 根据key取出对应的value:hashMap.get(key)
1. 5迭代hashMap这种事情尽量少做,先空着不管
2.数组
2.1创建:
int[] out=new int[2];
2.2返回匿名的数组
return new int[] {a.get(temp),i}可以减小空间复杂度
2.3 长度
nums.length//这个是属性不用()
2.4排序
Arrays.sort(nums);
3.ArrayList(lc15) 动态数组
3.1创建:List<List<Integer>> list = new ArrayList<>(); 注意创建的时候要用封装类,Integer ,String
3.2添加:list.add()
3.3去除指定元素:list.RemoveAt(5);//将第6个元素移除
3.4与数组之间相互转换
转数组:Int32[] values = new Int32[List.Count];
list.CopyTo(values);
转list:两种都可以,但是第二种会导致list变成固定大小的,再添加或者删除的时候报错
|
|