赞
踩
个人思想:每检测到0时,与最后元素互换。
参考答案:慢指针存放非0,快指针遍历。
个人思想:设置一个计数器,当检测到1时,i++. 检测到非1时,计数器值归0.
1 创建链表
LinkedList<Interger> list = new LinkedList<>();
2 添加元素
list.add(1) //在末尾插入元素
list.add(2,99) //在索引为2的位置上插入元素99
3 访问元素
int element = list.get(2); //访问索引为2 的元素
4 搜索元素
int index = list.indexOf(99); // 找到元素 返回索引
5 更新元素
list.set(2,88); // 将索引为2的位置的元素更新为88
6 删除元素
list.remove(2);
7 长度
int length = list.size();
dummy = ListNode(0); //设置头结点
dumy.next = head;
while(head != null && head.next != null){
设置两个保存指针
}
return dummy.next;
1 创建队列 Queue<Integer> queue = new LinkedList<>(); 2 添加元素 queue.add(1); 3 获取即将出队的元素 int temp = queue.peek(); // 不常见peek()函数 4 删除即将出队的元素 int temp = queue.poll(); 5 判断队列是否为空 queue.isEmpty(); //返回 1 or 0 6 队列的长度 int i = queue.size(); 7 遍历队列 while(!queue.isEmpty){ System.out.println(queue.poll()); }
class RecentCounter {
Queue<Integer> q;
public RecentCounter() {
q = new LinkedList(); //构造函数就是初始化这个队列Q
}
public int ping(int t) {
q.add(t);
while (q.peek() < t - 3000)
q.poll();
return q.size();
}
}
1 创建栈Stack
Stack<Integer> stack = new LinkedList<>();
2 添加元素
stack.add(1);
3 获取栈顶元素
stack.peek();
4 删除栈顶元素
stack.pop();
5 判断栈是否为空
stack.isEmpty();
6 栈的遍历
while(!stack.isEmpty()){
int num = stack.pop();
//输出num;
}
class Solution { public boolean isValid(String s) { if(s.length()%2==0){ Stack<Character> stack=new Stack<>(); for(int i=0;i<s.length();i++){ char c=s.charAt(i); if(c=='('||c=='['||c=='{'){ stack.push(c); } else if(c==')'){ if(!stack.isEmpty()&&stack.peek()=='('){stack.pop();} else{return false;} } else if(c=='}'){ if(!stack.isEmpty()&&stack.peek()=='{'){stack.pop();} else{return false;} } else{ if(!stack.isEmpty()&&stack.peek()=='['){stack.pop();} else{return false;} } } return stack.isEmpty(); } return false; } }
两个栈实现,删除一个栈的元素 存在另一个临时栈中
class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] res = new int[nums1.length]; Stack<Integer> stack1 = new Stack<Integer>(); for(int i = 0; i < nums2.length; i++){ stack1.push(nums2[i]); } for(int i = 0; i < nums1.length; i++){ boolean isFound = false; int max = -1; Stack<Integer> stack2 = new Stack<>(); while(!stack1.isEmpty()&&!isFound){ int temp = stack1.pop(); if(nums1[i] < temp){ max = temp; }else if(nums1[i] == temp){ isFound = true; } stack2.push(temp); } res[i] = max; while(!stack2.isEmpty()){ stack1.push(stack2.pop()); } } return res; } }
哈希表==散列表 键值对 在java中是HashMap结构
1 创建哈希表 HashMap<Integer, String> map = new HashMap<>(); 2 添加元素 map.put(1,"suncunwei"); 3 更新元素 map.put(1,"zhonghailing"); 4 删除元素 map.remove(1); 5 获取元素 map.get(1) //1表示键 获取的是value 6 查询key存在? map.containsKey(3); 7 长度 map.size(); 8 是否含元素 map.isEmpty();
key: 是数组元素,value: 元素出现次数。 HashMap
class Solution { public char findTheDifference(String s, String t) { int[] cnt = new int[26]; for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); cnt[ch - 'a']++; } for (int i = 0; i < t.length(); ++i) { char ch = t.charAt(i); cnt[ch - 'a']--; if (cnt[ch - 'a'] < 0) { return ch; } } return ' '; } }
class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] res = new int[nums1.length]; Stack<Integer> stack = new Stack<>(); HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0; i < nums2.length; i++){ while(stack.size() != 0 && nums2[i] > stack.peek()){ int temp = stack.pop(); map.put(temp,nums2[i]); } stack.push(nums2[i]); } while(stack.size() != 0){ int temp = stack.pop(); map.put(temp,-1); } for(int j = 0; j < nums1.length; j++){ res[j] = map.get(nums1[j]); } return res; } }
1 创建集合
HashSet<Integer> set = new HashSet<>();
2 添加元素
set.add(10);
3 搜索元素
set.contains(10);
4 删除元素
set.remove(10);
5 长度
set.size();
比较长度: 集合和数组元素长度一样 则不存在重复元素。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。