题解 | #草原上的牛群分布#
草原上的牛群分布
https://www.nowcoder.com/practice/25752634aa444758843eed6ff227703a?tpId=354&tqId=10587751&ru=/exam/oj/ta&qru=/ta/interview-202-top/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D354
知识点:
快慢指针
解题思路:
一个在前面找,一个在后面接,当前牛群数量大于2时,就不接了,知道遇到新的牛群
语言:
Golang
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ func remove_duplicates_v3( nums []int ) int { // write code here n:=len(nums) if n==0{ return 0 } // 当前的牛群 cur:=nums[0] // 当前的位置 index:=0 // 当前牛群的数量 count:=0 for _,v:=range nums{ if cur == v && count>2{ continue }else if cur!=v{ cur = v count = 0 } count++ nums[index] = v index++ } return index }