题解 | #牛的品种排序II#
牛的品种排序II
https://www.nowcoder.com/practice/43e49fbb98b4497ba46e185918188b1c
package main
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cows int整型一维数组
* @return int整型一维数组
*/
func sortCows( cows []int ) []int {
// write code here
if len(cows)==0||len(cows)==1{
return cows
}
var value int
for i :=0;i<len(cows);i++{
value =cows[i]
switch value{
case 0:cows = append(cows[:i],cows[i+1:]...)
cows =append([]int{0}, cows...)
case 2:{cows = append(cows[:i],cows[i+1:]... )
cows = append(cows,2)
if cows[i]!=2{
i--
}
}
}
}
return cows
}
查看13道真题和解析