码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构笔记

时间:2016-10-01 14:54:26      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

1. min/max heap

看到K神马的基本上就是min/max heap.

(1) Find the K closest points to the origin in a 2D plane, given an array containing N points.

技术分享
 1     public static List<Point> findKClosest(Point[] p, int k) {
 2         
 3         List<Point> res = new ArrayList<Point>();
 4         Comparator<Point> comparator = new Comparator<Point>(){
 5             //@Override
 6             public int compare(Point a, Point b) {
 7                 return (int) ( a.x*a.x + a.y*a.y- b.x*b.x -b.y*b.y);
 8             }
 9         };
10                 
11         PriorityQueue<Point> minheap = new PriorityQueue<Point>(comparator);
12         for (Point temp : p) {
13             minheap.add(temp);
14         }
15         for (int i = 0; i < k; i++) {
16             res.add(minheap.poll());
17         }
18     
19         return res;
20     }
version1

如果只能用k个位置保留在有线队列里面的话,那么前k个可以直接加入,后面k个的话,如果比最后一个小,那么把最后1个移除,然后放入当前的点.

技术分享
 1 public List<Point> findKClosest(Point[] p, int k) {  
 2     PriorityQueue<Point> pq = new PriorityQueue<>(10, new Comparator<Point>() {  
 3         @Override  
 4         public int compare(Point a, Point b) {  
 5             return (b.x * b.x + b.y * b.y) - (a.x * a.x + a.y * a.y);  
 6         }  
 7     });  
 8        
 9     for (int i = 0; i < p.length; i++) {  
10         if (i < k)  
11             pq.offer(p[i]);  
12         else {  
13             Point tmp = pq.peek();  
14             if ((p[i].x * p[i].x + p[i].y * p[i].y) - (tmp.x * tmp.x + tmp.y * tmp.y) < 0) {  
15                 pq.poll();  
16                 pq.offer(p[i]);  
17             }  
18         }  
19     }  
20        
21     List<Point> x = new ArrayList<>();  
22     while (!pq.isEmpty())  
23         x.add(pq.poll());  
24        
25     return x;  
26 }  
version2

 

------------------------我是分割线----------------------------------------

 

 2. 队列 queue
支持操作 o(1) push, o(1) pop, o(1) top
BFS的主要数据结构 多做做bfs题就好了
 
 
 
 
 
 
 

------------------------我是分割线----------------------------------------

 

 3. 栈 stack
支持操作 o(1) push, o(1) pop, o(1) top
非递归实现dfs的重要数据结构
 (1) min stack
技术分享
 1 public class MinStack {
 2 
 3     /** initialize your data structure here. */
 4     static Stack<Integer> numStack; 
 5     static Stack<Integer> minStack;
 6 
 7     public MinStack() {
 8         numStack = new Stack<Integer>();
 9         minStack = new Stack<Integer>();
10     }
11     
12     public void push(int x) {
13         numStack.push(x);
14         if (minStack.empty() || x <= minStack.peek()) {
15             minStack.push(x);
16         }
17     }
18     
19     public void pop() {
20         if (numStack.peek().equals(minStack.peek())) {
21             //note!!! because Stack contains integer which is an object, is use == will test its pointer!!!
22             minStack.pop();
23             numStack.pop();
24 
25         } else {
26             numStack.pop();
27         }
28     }
29     
30     public int top() {
31         return numStack.peek();
32     }
33     
34     public int getMin() {
35         return minStack.peek();
36     }
37 }
min stack

(2) implement queue by two stack

技术分享
 1 class MyQueue {
 2     private Stack<Integer> stack1 = new Stack<Integer>();
 3     private Stack<Integer> stack2 = new Stack<Integer>();
 4     
 5     // Push element x to the back of queue.
 6     public void push(int x) {
 7         if (!stack2.empty()){
 8             move();
 9         }
10         stack1.push(x);
11 
12     }
13     private void move() {
14         if (!stack2.empty()) {
15             while (!stack2.empty()) {
16               stack1.push(stack2.pop());
17             }
18             return;
19         } else {
20             while (!stack1.empty()) {
21               stack2.push(stack1.pop());
22             }
23             return;
24         }
25     }
26 
27     // Removes the element from in front of queue.
28     public void pop() {
29         if (!stack1.empty()) {
30             move();
31         }
32         stack2.pop();
33     }
34 
35     // Get the front element.
36     public int peek() {
37         if (!stack1.empty()) {
38             move();
39         }
40         return stack2.peek();
41     }
42 
43     // Return whether the queue is empty.
44     public boolean empty() {
45         if (!stack1.empty() || !stack2.empty()) {
46             return false;
47         }
48         return true;
49     }
50 }
View Code

(3)Largest rectangle in histogram

Given n non-negative integers representing the histogram‘s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

技术分享

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

 

技术分享

The largest rectangle is shown in the shaded area, which has area = 10 unit.

 

For example,
Given heights = [2,1,5,6,2,3],
return 10.

暴力方法: 

for矩阵的起点

技术分享 

 

需要用到“单调栈” 

这道题的核心点是:最矮的木头!

1. //for矩阵的最爱的那根木头,也就是矩阵的高度

2.记住,单调栈能够做到找到左边比一个比当前小的点,能找到右边第一个比它小的点

3. 单调栈里实际上存的是下标,在比较增序关系的时候才用值去比较

 

 

 

 

 

 

 

 

 

 

------------------------我是分割线----------------------------------------

 

数据结构笔记

标签:

原文地址:http://www.cnblogs.com/jiangchen/p/5925717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!