Algorithm

剑指Offer 51 数组中的逆序对

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。
输入一个数组,求出这个数组中的逆序对的总数。
示例 1:
输入: [7,5,6,4]
输出: 5
限制:0 <= 数组长度 <= 50000

方法一:每个数和剩下的数比大小

image.png

比如上图中的[9,6,8,4],先让9和剩下三个比大小,得出[9,6][9,8][9,4],满足逆序对。
再让6和剩下两个比大小,只有[6,4]满足逆序对。
再让8和剩下的4比,[8,4]满足逆序对。
所以一共是5组。

方法二:merge sort归并排序

以下为Python 3解法

  1. class Solution:
  2. def reversePairs(self, nums: List[int]) -> int:
  3. if not nums: return 0
  4. # 如果把这个数组分成两个部分,则最后结果有三个部分:
  5. # 1.前半部分的逆序对数目;2.后半部分的逆序对数目
  6. # 3.前半部分数字比后半部分数字大的部分。这部分需要在二分的时候进行归并来计算。
  7. def binarySearch(i,j):
  8. if i==j: return [nums[i]],0
  9. mid = (i+j)//2
  10. left, r1 = binarySearch(i,mid)
  11. right, r2 = binarySearch(mid+1,j)
  12. # 做归并排序,同时统计第三种情况的数目
  13. arr = []
  14. res, s = 0, 0
  15. l1, l2, s1, s2 = len(left), len(right), 0, 0
  16. while s1<l1 and s2<l2:
  17. if left[s1]>right[s2]:
  18. s += 1
  19. arr.append(right[s2])
  20. s2 += 1
  21. else:
  22. res += s
  23. arr.append(left[s1])
  24. s1 += 1
  25. if s2<l2:
  26. arr.extend(right[s2:])
  27. elif s1<l1:
  28. res += (s*(l1-s1))
  29. arr.extend(left[s1:])
  30. return arr,r1+r2+res
  31. _, r = binarySearch(0,len(nums)-1)
  32. return r

Review

区块链分类

这篇文章好在解答了我存疑了几个月的困惑,联盟链和公链在技术特性和应用场景方面的区别。和分布式系统相似,不同的区块链技术也是在考虑认证效率和可信度之间的平衡,鱼和熊掌不可得兼。

公链属于无需许可的区块链,要获得交易记账权,首先得满足共识协议的条件,没有单独的个体可以掌控它。所以天然的使用场景就是比特币、加密数字货币这种用户超级无敌多,超级分散,可信度要求非常高的情况。缺点是可伸缩性不好,这个和分布式系统的特性看起来很像。

与之相对的就是私有链,联盟链(特权节点数量稍多一些)处于其中的一种,这种模式只允许少数几个节点拥有特权,只有它们有资格控制共识过程,执行交易验证。但这也导致这种系统易受外部攻击,因为只要打垮那几个特权节点就搞定了。而在公链里,要在超多节点中,同时干掉半数以上节点,这个难度是很大的。联盟链适合用于国际贸易之类的场景中,因为很多组织并不愿意公布他们的商业机密,而联盟链刚好采取了效率和可信度的折中模式。

文章还提到了智能合约,它是类似法律合同一样,但无需外界干预就会自动化执行的程序。它规定了每个“合同”参与者的义务、利益和惩罚。

Technique/Tips

Maven

犹太语中知识积累的意思,试图简化Jakarta Turbine项目的构建过程,是个类似于Git的项目管理工具,给JAVA开发者提供了更简单易用的开发工具。

pom.xml

=Project Object Model,是Maven中的一个基础单元,xml格式表示它包含了项目和配置细节,包含了绝大多数项目中的默认值。在一个Spring Boot框架下,src表示source,src/main/java放源目录, src/test/java放测试的源目录。
image.png

Spring Boot模板里的mvnw、mvnw.cmd是干什么的?

image.png
“This allows you to run the Maven project without having Maven installed and present on the path. It downloads the correct Maven version if it’s not found (as far as I know by default in your user home directory).
The mvnw file is for Linux (bash) and the mvnw.cmd is for the Windows environment.”

为什么要把src/main/resources目录下默认的application.properties改为.yml?这两种格式有什么区别?

image.png
它们是两种不同的配置属性,让应用程序跑起来,比如作为一个授权文件连接到数据库。yml是一种配置语言,它在Python, Ruby, Java中重度使用,在Elastic Search实例和MongoDB数据库里是默认的配置格式。

YAML(.yml) .properties
Spec can be found here It doesn’t really actually have a spec. The closest thing it has to a spec is actually the javadoc.
Human Readable (both do quite well in human readability) Human Readable
Supports key/val, basically map, List and scalar types (int, string etc.) Supports key/val, but doesn’t support values beyond the string
Its usage is quite prevalent in many languages like Python, Ruby, and Java It is primarily used in java
Hierarchical Structure Non-Hierarchical Structure
Spring Framework doesn’t support @PropertySources with .yml files supports @PropertySources with .properties file
If you are using spring profiles, you can have multiple profiles in one single .yml file Each profile need one separate .properties file
While retrieving the values from .yml file we get the value as whatever the respective type (int, string etc.) is in the configuration While in case of the .properties files we get strings regardless of what the actual value type is in the configuration

**

学会了通过端口号查找进程号,解决dispatcherServlet初始化失败的问题。

启动application.java的时候,控制台少了三行初始化 ‘dispatcher servlet’成功的提示?
image.png
然后查了Tomcat,果然启动就有问题……catalina协议初始化失败 Caused by: java.net.BindException: Address already in use: bind,好像是端口号占用冲突。
image.png
按照网络教程结束占用8080端口号的进程,同时修改Tomcat里的配置文件,默认端口号为8080,重新启动成功。
image.png
确认Tomcat启动正常后,再重新运行application.java,
image.png

Share

智能合约一旦采用,意味着合同条款执行过程中,不再有灰色的中间地带。因为程序逻辑只有是与非。

区块链和智能合约:机遇与局限
商机 局限性
代码确定性 没有蓄意模棱两可的空间
重复性低价值交易的成本效益 与集中式系统相比,分散计算能力可能效率更低(且成本更高)
打破垄断的金融体系,增加竞争 对具有足够计算能力的区块链的需求,即“完成”
真正互联互通的世界,消除了系统之间的低效率 说服现有系统过渡到区块链的巨大障碍
分散化降低了永久性数据丢失和损坏的风险 缺乏集中控制可能会引起监管机构的关注
透明度可能有助于有效监管市场 透明度将涉及金融投资者
更快地结算交易,并删除多层账簿 尚不清楚如何实现在区块链上交易的证券的实时结算最终性