Given an int array length 2, return True if it contains a 2 or a 3. has23([2, 5]) → True has23([4, 3]) → True has23([4, 5]) → False
时间: 2023-06-25 18:05:26 浏览: 60
以下是 Python 代码实现:
```python
def has23(nums):
return 2 in nums or 3 in nums
```
使用方法:
```python
print(has23([2, 5])) # True
print(has23([4, 3])) # True
print(has23([4, 5])) # False
```
相关问题
Given an array of ints, return True if the array contains a 2 next to a 2 somewhere.
可以使用一个循环遍历整个数组,检查每个元素是否为 2,如果是,则检查下一个元素是否也为 2。如果两个元素都是 2,则返回 True,否则继续循环。如果循环结束后仍未找到相邻的 2,则返回 False。
以下是 Python 代码实现:
```python
def has22(nums):
for i in range(len(nums)-1):
if nums[i] == 2 and nums[i+1] == 2:
return True
return False
```
示例输入和输出:
```python
>>> has22([1, 2, 2])
True
>>> has22([1, 2, 1, 2])
False
>>> has22([2, 2, 1, 2])
True
```
Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.
以下是 Python 代码实现:
```python
def middle_way(a, b):
return [a[1], b[1]]
```
这个函数接受两个长度为 3 的整数数组 `a` 和 `b` 作为输入,返回一个新的长度为 2 的数组,其中包含两个输入数组的中间元素。
例如,`middle_way([1, 2, 3], [4, 5, 6])` 的返回值为 `[2, 5]`。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)