ArrayList<E> xx =new ArrayList<>(); //访问 xx.get(index); //add xx.add(ans); //size xx.size();
以下情况使用 LinkedList :
你需要通过循环迭代来访问列表中的某些元素。
需要频繁的在列表开头、中间、末尾等位置进行添加和删除元素操作。
1 2 3 4
//嵌套list List<List<Integer>> result = new LinkedList<>(); List<Integer> ans = new LinkedList<>(); //与 ArrayList 相比,LinkedList 的增加和删除对操作效率更高,而查找和修改的操作效率较低。
class Solution { public int lengthOfLongestSubstring(String s) { int left = 0; int right = 0; int answer = 0; HashMap<Character,Integer> map = new HashMap<Character,Integer>(); int strSize = s.length(); while(right<=strSize-1){ if(map.containsKey(s.charAt(right))){ //answer = Math.max(answer,right-left); left = Math.max(left,map.get(s.charAt(right))+1); }
class Solution { public int maxArea(int[] height) { int size = height.length; int left = 0; int right = size - 1; int ans = 0; int current = 0; while(left<right){ int _width; if(height[left]>height[right]){ _width = height[right]; } else{ _width = height[left]; } current = (right - left)* _width; if(current>=ans){ans = current;} // if(height[left]>height[right]){ right--; } else{ left++; } }