2023-09-30:用go语言,给你一个整数数组 nums 和一个整数 k 。 nums 仅包含 0 和 1, 每一次移动,你可以选择 相邻 两个数字并将它们交换。 请你返回使 nums 中包含 k 个 连续 1 的 最少 交换次数。 输入:nums = [1,0,0,1,0,1], k = 2。 输出:1。 来自左程云。 答案2023-09-30: 1.定义一个函数 minMoves(nums []int, k int),传入一个整数数组 nums 和一个整数 k。 2.如果 k 等于 1,直接返回 0。 3.获取数组 nums 的长度 n。 4.计算目标窗口中索引和的左半部分,即 (k - 1)/2 个索引的和,赋值给 leftAimIndiesSum。 5.计算目标窗口中索引和的右半部分,即 (k-1)*k/2 - leftAimIndiesSum,赋值给 rightAimIndiesSum。 6.初始化一个变量 ans,并将其赋值为最大整数。 7.初始化左边窗口的起始索引 l 为 0,中间位置索引 m 为 (k - 1)/2,右边窗口的结束索引 r 为 k - 1。...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Get in touch with rightWindow
Contact rightWindow, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack