当前位置: 首页 > news >正文

闯关leetcode——3206. Alternating Groups I

大纲

  • 题目
    • 地址
    • 内容
  • 解题
    • 代码地址

题目

地址

https://leetcode.com/problems/alternating-groups-i/description/

内容

There is a circle of red and blue tiles. You are given an array of integers colors. The color of tile i is represented by colors[i]:

  • colors[i] == 0 means that tile i is red.
  • colors[i] == 1 means that tile i is blue.

Every 3 contiguous tiles in the circle with alternating colors (the middle tile has a different color from its left and right tiles) is called an alternating group.

Return the number of alternating groups.

Note that since colors represents a circle, the first and the last tiles are considered to be next to each other.

Example 1:

Input: colors = [1,1,1]
Output: 0
Explanation:
在这里插入图片描述
Example 2:

Input: colors = [0,1,0,0,1]
Output: 3
Explanation:
在这里插入图片描述
Alternating groups:
在这里插入图片描述

Constraints:

  • 3 <= colors.length <= 100
  • 0 <= colors[i] <= 1

解题

这题就是要求两个相同颜色夹一个其他颜色的组合个数。这题唯一需要注意的就是使用vector的下标表达环形结构。

#include <vector>
using namespace std;class Solution {
public:int numberOfAlternatingGroups(vector<int>& colors) {int result = 0;for (int i = 0; i < colors.size(); i++) {if (colors[i] == colors[(i + 2) % colors.size()] && colors[i] != colors[(i + 1) % colors.size()]) {result++;}}return result;}
};

在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/3206-Alternating-Groups-I/cplusplus

http://www.lryc.cn/news/483386.html

相关文章:

  • 多个摄像机画面融合:找到同一个目标在多个画面中的伪三维坐标,找出这几个摄像头间的转换矩阵
  • Three.js性能优化和实践建议
  • C#入门 023 什么是类(Class)
  • 一篇Spring Boot 笔记
  • 一生一芯 预学习阶段 NEMU代码学习(2)
  • 《手写Spring渐进式源码实践》实践笔记(第二十章 实现简单ORM框架)
  • AI技术赋能电商行业:创新应用与未来展望
  • windows 11编译安装ffmpeg(包含ffplay)
  • 系统启动时将自动加载环境变量,并后台启动 MinIO、Nacos 和 Redis 服务
  • [ACTF2020 新生赛]Upload 1--详细解析
  • power bi中的related函数解析
  • 目前区块链服务商备案支持的区块链技术类型
  • CatBoost中的预测偏移和排序提升
  • python: postgreSQL using psycopg2 or psycopg
  • 从 MySQL 5.7 到 8.0:理解 GROUP BY 的新规则与实战优化20241112
  • npm完整发包流程(亲测可验证)
  • 学习threejs,使用JSON格式保存和加载模型
  • 中国首部《能源法》正式问世,它的亮点有哪些呢?
  • 【外包】软件行业的原始形态,项目外包与独立开发者
  • 工程数学线性代数(同济第七版)附册课后习题答案PDF
  • 【Ubuntu24.04】部署服务(基础)
  • Linux符号使用记录
  • 初阶C++之C++入门基础
  • ODOO学习笔记(7):模块化架构(按需安装)
  • Java的dto,和多表的调用
  • 时序数据库TimescaleDB安装部署以及常见使用
  • MG算法(英文版)题解
  • 2-UML概念模型测试
  • 人工智能(AI)对于电商行业的变革和意义
  • 智能病历xml提取