当前位置: 首页> 教育> 培训 > 郑州网站排名优化公司_全球推广_北京网站设计公司_网站制作的费用

郑州网站排名优化公司_全球推广_北京网站设计公司_网站制作的费用

时间:2025/7/11 18:16:53来源:https://blog.csdn.net/wang__12300/article/details/146717963 浏览次数:0次
郑州网站排名优化公司_全球推广_北京网站设计公司_网站制作的费用

学习记录

2025.3.29

题目:

在这里插入图片描述

思路:

遍历判断。

解题步骤:

1.遍历数组中的每个元素 colors[i]。
2.计算其左邻居的索引 left = (i - 1 + n) % n。
计算其右邻居的索引 right = (i + 1) % n。
3.检查 colors[i] 是否不等于 colors[left] 且不等于 colors[right]。
如果满足条件,则计数器 res 加一
4.返回res。

代码:

int numberOfAlternatingGroups(int* colors, int colorsSize) {int res = 0;for (size_t i = 0; i < colorsSize; i++) {if (colors[i] != colors[(i - 1 + colorsSize) % colorsSize] && colors[i] != colors[(i + 1) % colorsSize]) {res += 1;}}return res;
}
class Solution {public int numberOfAlternatingGroups(int[] colors) {int n = colors.length;int res = 0;for (int i = 0; i < n; i++) {if (colors[i] != colors[(i - 1 + n) % n] && colors[i] != colors[(i + 1) % n]) {res += 1;}}return res;}
}

复杂度:

N(N)
N(1)

关键字:郑州网站排名优化公司_全球推广_北京网站设计公司_网站制作的费用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: