612 - K个最近的点

2017.10.30

就是先算出每个点到目标点的距离,然后按距离排序。

当距离相等的时候,按照x,y的值进行排序。

最后,输出前k个就可以了。

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */


public class Solution {
    /*
     * @param points: a list of points
     * @param origin: a point
     * @param k: An integer
     * @return: the k closest points
     */
	public Point[] kClosest(Point[] points, Point origin, int k) {
        // write your code here
		HashMap<Integer,LinkedList<Point>> map = new HashMap<>();
		for(Point p : points){
			//System.out.println("Point p = [" + p.x + "," + p.y +"]" );
			int distance = getDistance(p,origin);
			if(!map.containsKey(distance) == true){
				LinkedList<Point> list = new LinkedList<>();
				list.add(p);
				map.put(distance, list);
			}
			else{
				map.get(distance).add(p);
			}
		}
		//printMap(map);
		int[] key = new int[map.keySet().size()];
		int tmp = 0;
		for(int i : map.keySet()){
			key[tmp++] = i;
		}
		Arrays.sort(key);
		//System.out.println("zheli");
		int count = 0;
		Point[] res = new Point[k];
		for(int i = 0 ; i < key.length; i++){
			LinkedList<Point> listTmp = map.get(key[i]);
			listTmp = sort(listTmp);
			for(Point p :listTmp){
				res[count] = p;
				//System.out.println("加入[" + p.x + "," + p.y +"]" );
				count++;
				if(count >= k){	
					return res;
				}
			}
		}
		return res;
    }
	public LinkedList<Point> sort(LinkedList<Point> list){
		if(list.isEmpty() || list.size() == 1){
			return list;
		}
		LinkedList<Point> res = new LinkedList<>();
		for(Point p : list){
			if(res.isEmpty()){
				res.add(p);
			}
			else{
			    res.add(p);
				 for(int i = res.size() - 2; i >= 0; i--){
					Point p2 = res.get(i);
						if(p.x > p2.x){
						    continue;
						}
						if(p.x == p2.x && p.y >= p2.y){
						    continue;
						}
						res.set(i+1, p2);
						if(p.x < p2.x){
							res.set(i,p);
						}
						else if(p.x == p2.x && p.y <= p2.y){
							res.set(i,p);
						}
				}
			}
		}
		return res;
	}
		public int getDistance(Point point, Point origin) {
        // write your code here
		int a = point.x - origin.x;
		int b = point.y - origin.y;
		return a*a + b*b;
    }
}


通过slabtop -o 查询结果如下,帮忙继续分析 Active / Total Objects (% used) : 591128374 / 592465039 (99.8%) Active / Total Slabs (% used) : 9361515 / 9361515 (100.0%) Active / Total Caches (% used) : 89 / 120 (74.2%) Active / Total Size (% used) : 148822210.28K / 148962969.18K (99.9%) Minimum / Average / Maximum Object : 0.01K / 0.25K / 8.00K OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME 582804880 582804793 99% 0.25K 9154573 64 146473168K kmalloc-256 4343339 4343339 100% 0.57K 85236 56 2727552K radix_tree_node 3634605 2324743 63% 0.10K 93195 39 372780K buffer_head 165504 164840 99% 0.03K 1293 128 5172K kmalloc-32 152796 148883 97% 0.04K 1498 102 5992K selinux_inode_security 148480 145369 97% 0.02K 580 256 2320K kmalloc-16 140250 140250 100% 0.02K 825 170 3300K fsnotify_mark_connector 128384 127577 99% 0.06K 2006 64 8024K kmalloc-64 121212 120175 99% 0.19K 2886 42 23088K dentry 101376 101376 100% 0.01K 198 512 792K kmalloc-8 64512 64331 99% 0.19K 1536 42 12288K kmalloc-192 63899 62857 98% 0.21K 1727 37 13816K vm_area_struct 60256 58903 97% 0.50K 959 64 30688K kmalloc-512 48144 48144 100% 0.04K 472 102 1888K ext4_extent_status 41616 41616 100% 0.12K 612 68 4896K kernfs_node_cache 40860 37561 91% 0.66K 853 48 27296K proc_inode_cache 40420 40366 99% 0.94K 1195 34 38240K xfs_inode 35820 35820 100% 0.13K 597 60 4776K ext4_groupinfo_4k 33235 27367 82% 0.05K 391 85 1564K shared_policy_node 33102 33078 99% 4.00K 4794 8 153408K kmalloc-4096 27968 27968 100% 0.12K 437 64 3496K kmalloc-128 27795 26236 94% 0.08K 545 51 2180K anon_vma 22880 21320 93% 0.58K 416 55 13312K inode_cache 18144 18144 100% 0.07K 324 56 1296K avc_node 14742 14415 97% 0.19K 351 42 2808K cred_jar 12474 12474 100% 0.09K 297 42 1188K kmalloc-96 11680 11680 100% 0.05K 160 73 640K avc_xperms_node 10764 10764 100% 0.17K 234 46 1872K xfs_ili 10374 10374 100% 0.38K 247 42 3952K mnt_cache 9448 9256 97% 1.00K 304 32 9728K ext4_inode_cache 7524 7524 100% 0.11K 209 36 836K task_delay_info 7392 6925 93% 1.00K 231 32 7392K kmalloc-1024 6720 6528 97% 0.06K 105 64 420K ext4_free_data 6273 6273 100% 0.62K 123 51 3936K sock_inode_cache 5888 5888 100% 0.12K 92 64 736K pid 3672 3457 94% 0.11K 102 36 408K jbd2_journal_head 3584 3584 100% 0.07K 64 56 256K ext4_io_end 3408 3408 100% 0.66K 71 48 2272K shmem_inode_cache 3360 3360 100% 1.12K 120 28 3840K signal_cache 2608 2057 78% 2.00K 163 16 5216K kmalloc-2048 2560 2560 100% 0.03K 20 128 80K jbd2_revoke_record_s 2040 2040 100% 0.05K 24 85 96K jbd2_journal_handle 1728 1728 100% 0.06K 27 64 108K fs_cache 1664 1522 91% 1.19K 64 26 2048K RAWv6 1652 1629 98% 2.06K 112 15 3584K sighand_cache 1632 1632 100% 0.47K 24 68 768K xfs_da_state 1603 1485 92% 4.12K 229 7 7328K task_struct 1584 1584 100% 0.24K 24 66 384K posix_timers_cache
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值