2025-02-13:交替组Ⅰ。用go语言,给定一个整数数组 colors,它表

liftword4个月前 (02-28)技术文章22

2025-02-13:交替组Ⅰ。用go语言,给定一个整数数组 colors,它表示一个由红色和蓝色瓷砖组成的环。在这个数组中,colors[i] 的值为:

①.0 表示第 i 块瓷砖是红色。

②.1 表示第 i 块瓷砖是蓝色。

如果连续的三块瓷砖的颜色呈现交替状态(即中间的瓷砖颜色与两侧的瓷砖颜色不同),则称这些瓷砖构成一个“交替组”。

请你计算并返回这个环中交替组的数量。

需要注意的是,由于瓷砖呈环状排列,因此数组的第一块和最后一块瓷砖也是相邻的。

3 <= colors.length <= 100。

0 <= colors[i] <= 1。

输入:colors = [0,1,0,0,1]。

输出:3。

答案2025-02-13:

chatgpt[1]

题目来自leetcode3206。

大体步骤如下:

自然智慧即可。

1.初始化变量 n 为数组 colors 的长度,并初始化变量 res 为交替组的数量,初始值为 0。

2.使用一个循环遍历整个数组 colors

2.a.对于每个元素 colors[i],判断它与其前一个元素(考虑环形数组情况,需要使用 (i-1+n)%n)和后一个元素 (i+1)%n 的颜色是否不同。

2.b.如果当前元素与相邻的两个元素颜色都不同,则将交替组的数量 res 自增 1。

3.遍历完成后,返回交替组的数量 res

总体时间复杂度为 O(n),其中 n 为数组 colors 的长度。

空间复杂度为 O(1),只使用了常数级别的额外空间。

Go完整代码如下:

package main

import (
    "fmt"
)

func numberOfAlternatingGroups(colors []int) int {
    n := len(colors)
    res := 0
    for i := 0; i < n; i++ {
        if colors[i] != colors[(i-1+n)%n] && colors[i] != colors[(i+1)%n] {
            res++
        }
    }
    return res
}

func main() {
    colors := []int{0, 1, 0, 0, 1}
    result := numberOfAlternatingGroups(colors)
    fmt.Println(result)
}

在这里插入图片描述

Rust完整代码如下:

fn number_of_alternating_groups(colors: &[i32]) -> i32 {
    let n = colors.len() as i32;
    let mut res = 0;

    for i in 0..n {
        let prev_color = colors[((i - 1 + n) % n)as usize]; // Previous color in circular array
        let next_color = colors[((i + 1) % n) as usize]; // Next color in circular array

        if colors[i as usize] != prev_color && colors[i as usize] != next_color {
            res += 1;
        }
    }

    res
}

fn main() {
    let colors = vec![0, 1, 0, 0, 1];
    let result = number_of_alternating_groups(&colors);
    println!("{}", result);
}

在这里插入图片描述

Python完整代码如下:

# -*-coding:utf-8-*-

def number_of_alternating_groups(colors):
    n = len(colors)
    res = 0
    
    for i in range(n):
        prev_color = colors[(i - 1 + n) % n]  # Previous color in circular array
        next_color = colors[(i + 1) % n]      # Next color in circular array
        
        if colors[i] != prev_color and colors[i] != next_color:
            res += 1
            
    return res

def main():
    colors = [0, 1, 0, 0, 1]
    result = number_of_alternating_groups(colors)
    print(result)

if __name__ == "__main__":
    main()

在这里插入图片描述

引用链接

[1] chatgpt: https://chatbotsplace.com/?rc=nnNWSCJ7EP

相关文章

Golang 3、数组_golang new 数组

在 Go 语言中,数组是一种固定长度的、存储相同类型元素的数据结构。1.数组的基本概念固定长度:数组的长度在定义时就确定,不能动态改变。相同类型:数组中的所有元素必须是同一类型。索引访问:通过索引(从...

Bash Shell脚本中的数组使用实例_linux shell脚本 数组

数组是一个包含多个值的变量,这些值可以是相同类型或不同类型。没有数组大小限制,也没有要求成员变量被连续索引或连续分配的限制。数组索引从0开始。1.声明一个数组并赋值在bash中,使用以下格式的变量时会...

python实现数组操作代码_python数组操作方法

Python是一种功能强大的编程语言,尤其在处理数组和列表等数据结构时非常出色。它提供了许多有用的工具和库,使得数组操作变得轻松和高效。本文将详细介绍Python中实现数组操作的代码,并给出一些示例。...

2025-01-19:数组中的峰值。用go语言,在一个整数数组 nums 中,若

2025-01-19:数组中的峰值。用go语言,在一个整数数组 nums 中,若某个元素大于其左右相邻的元素,则称该元素为“峰值”元素。你会得到一个整数数组 nums 和一个二维数组 queries。...

Python 技巧讲解:numpy.array 操作使用简单总结(含示例代码)

今天为大家带来的内容是:Python 技巧讲解:numpy.array 操作使用简单总结(含示例代码)文章内容主要介绍了numpy.array 操作使用简单总结,文中通过示例代码介绍的非常详细,对大家...

详解Python中的列表推导式_python,列表

列表推导式(list comprehension)或者称为列表解析式,是基于现有的列表做一些操作,从而快速创建新列表的一种方法。它的工作方式类似于for循环,是一种功能强大、操作优雅的方法。在实际的项...