博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Count of Smaller Number After Itself
阅读量:6937 次
发布时间:2019-06-27

本文共 1125 字,大约阅读时间需要 3 分钟。

1 public class Solution { 2     public List
countSmaller(int[] nums) { 3 List
result = new ArrayList<>(); 4 List
record = new ArrayList<>(); 5 6 if (nums.length == 0) { 7 return result; 8 } 9 result.add(0);10 record.add(nums[nums.length - 1]);11 for (int i = nums.length - 2; i >= 0; i--) {12 int start = 0, end = result.size() - 1, mid = 0;13 while (start <= end) {14 mid = (end - start)/2 + start;15 if (record.get(mid) >= nums[i]) {16 end = mid - 1;17 } else {18 start = mid + 1;19 }20 }21 result.add(0, start);22 record.add(start, nums[i]);23 }24 return result;25 }26 }

1. This binary search is find the insertion position. Since it starts from end of array. So it must be greater and equal to mid number.

 

The merge sort method is pretty tricky

转载于:https://www.cnblogs.com/shuashuashua/p/5643635.html

你可能感兴趣的文章
MyEclipse对Struts2配置文件较检异常 Invalid result location value/parameter
查看>>
cocos2d-x 3.0 final 中文显示
查看>>
page_address()函数分析--如何通过page取得虚拟地址
查看>>
关于C#基类和子类函数调用问题
查看>>
性能测试知多少:性能分析与调优的原理
查看>>
js 正则之 控制字符 \cX
查看>>
由 12306.cn 谈谈高并发+高负载网站性能技术
查看>>
u3d 加密资源并缓存加载
查看>>
html5本地存储
查看>>
在css加载完毕后执行后续代码
查看>>
iOS: 学习笔记, 透过Boolean看Swift(译自: https://developer.apple.com/swift/blog/ Aug 5, 2014 Boolean)...
查看>>
db4o种纯对象数据库引擎
查看>>
人可以做自己的领导者。最好的领导者绝不是诸葛亮那样鞠躬尽瘁,而是司马懿那样耐得住寂寞,审时度势...
查看>>
安卓开发笔记——TabHost组件(一)(实现底部菜单导航)
查看>>
使用ajax和window.history.pushState无刷新改变页面内容和地址栏URL
查看>>
TCP的那些事儿(上)
查看>>
公布一个软件,轻新视频录播程序,H264/AAC录制视音频,保存FLV,支持RTMP直播...
查看>>
LeetCode - Jump Game
查看>>
UIAlertController Changes in iOS 8
查看>>
Service-stack.redis 使用PooledRedisClientManager 速度慢的原因之一
查看>>