go + 排序,前后元素比较
package main
import "sort"
/**
* max increasing subsequence
* @param arr int整型一维数组 the array
* @return int整型
*/
func MLS( arr []int ) int {
// write code here
if len(arr) == 0 {
return 0
}
// 先排序
sort.Ints(arr)
max := 1
count := 1
// 分三种情况来决定 count 是否++
for i:=1; i<len(arr); i++{
// 当前值 +1 等于 前面的值
if arr[i]-1 == arr[i-1] {
count++
max = maxs(max, count)
}else if arr[i] == arr[i-1]{
// 当前值等于前面的值,跳过
continue
}else{
// 其他情况,设置count等于1,重新开始
count = 1
}
}
return max
}
func maxs(a, b int) int {
if a>b{return a}
return b
}
京公网安备 11010502036488号