给定由N个正整数组成的数组arr [] ,任务是对仅由一位数字元素组成的子数组进行计数。
例子:
Input: arr[] = {0, 1, 14, 2, 5}
Output: 6
Explanation: All subarrays made of only single digit numbers are {{0}, {1}, {2}, {5}, {0, 1}, {2, 5}}. Therefore, the total count of subarrays is 6.
Input: arr[] ={12, 5, 14, 17}
Output: 1
Explanation: All subarrays made of only single digit numbers are {5}.
Therefore, the total count of subarrays is 1.
天真的方法:最简单的方法是遍历数组并生成所有可能的子数组。对于每个子数组,检查其中的所有整数是否都是一位整数。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是找到连续的单个数字整数的每个块的大小,并以该长度的总子数组递增计数。请按照以下步骤解决问题:
- 初始化一个变量,例如res = 0和c = 0 ,以存储子数组的总数和子数组中一位整数的总数。
- 遍历数组并执行以下操作:
- 如果arr [i] <10,则将c的计数增加1,将res的计数增加c。
- 否则,将c设置为0。
- 最后,打印一位整数整数子数组的总数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count of subarrays made
// up of single digit integers only
int singleDigitSubarrayCount(int arr[],
int N)
{
// Stores count of subarrays
int res = 0;
// Stores the count of consecutive
// single digit numbers in the array
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
if (arr[i] <= 9) {
// Increment size of block by 1
count++;
// Increment res by count
res += count;
}
else {
// Assign count = 0
count = 0;
}
}
cout << res;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 0, 1, 14, 2, 5 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
singleDigitSubarrayCount(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to count of subarrays made
// up of single digit integers only
static void singleDigitSubarrayCount(int arr[],
int N)
{
// Stores count of subarrays
int res = 0;
// Stores the count of consecutive
// single digit numbers in the array
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
if (arr[i] <= 9)
{
// Increment size of block by 1
count++;
// Increment res by count
res += count;
}
else
{
// Assign count = 0
count = 0;
}
}
System.out.print(res);
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 0, 1, 14, 2, 5 };
// Size of the array
int N = arr.length;
singleDigitSubarrayCount(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to count of subarrays made
# up of single digit integers only
def singleDigitSubarrayCount(arr, N):
# Stores count of subarrays
res = 0
# Stores the count of consecutive
# single digit numbers in the array
count = 0
# Traverse the array
for i in range(N):
if (arr[i] <= 9):
# Increment size of block by 1
count += 1
# Increment res by count
res += count
else:
# Assign count = 0
count = 0
print (res)
# Driver Code
if __name__ == '__main__':
# Given array
arr = [0, 1, 14, 2, 5]
# Size of the array
N = len(arr)
singleDigitSubarrayCount(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to count of subarrays made
// up of single digit integers only
static void singleDigitSubarrayCount(int[] arr,
int N)
{
// Stores count of subarrays
int res = 0;
// Stores the count of consecutive
// single digit numbers in the array
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
if (arr[i] <= 9)
{
// Increment size of block by 1
count++;
// Increment res by count
res += count;
}
else
{
// Assign count = 0
count = 0;
}
}
Console.Write(res);
}
// Driver Code
public static void Main(string[] args)
{
// Given array
int[] arr = { 0, 1, 14, 2, 5 };
// Size of the array
int N = arr.Length;
singleDigitSubarrayCount(arr, N);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出:
6
时间复杂度: O(N)
辅助空间: O(1)