📜  生成备用的递增和递减数组

📅  最后修改于: 2021-04-27 20:23:51             🧑  作者: Mango

给定大小为N的字符串str ,其中仅包含两种字符“ I”“ D” 。任务是生成数组arr [0,1,,。 。 N]的大小N + 1满足以下条件:

  • 如果str [i] ==“ I”,则arr [i]
  • 如果str [i] ==“ D”,则arr [i]> arr [i + 1]

例子:

方法:

  1. 将变量START初始化为0 ,将END初始化为N。
  2. 遍历整个数组。
  3. 如果str [i] == “ I”,则在arr [i]处分配START并递增START
  4. 如果str [i] == “ D”,则在arr [i]处分配END ,并减小END
  5. 现在未分配数组的最后一个元素,因此在A [N]处分配START

下面是该方法的实现。

C++
// C++ program to Generate an increasing
// and decreasing array
  
#include 
using namespace std;
  
// Function that returns generated array
int* DiStirngMatch(string Str)
{
    int N = Str.length();
  
    // Dynamically allocate array
    int* arr = new int[N];
  
    // START=0, END=N
    int START = 0, END = N;
  
    // iterate over array
    for (int i = 0; i < N; i++) {
  
        // if Str[i]=='I' assign arr[i]
        // as START and increment START
        if (Str[i] == 'I')
            arr[i] = START++;
  
        // if str[i]=='D' assign arr[i]
        // as END and decrement END
        if (Str[i] == 'D')
            arr[i] = END--;
    }
  
    // assign A[N] as START
    arr[N] = START;
  
    // return starting
    // address of array A
    return arr;
}
  
// Driver Program
int main()
{
    string Str = "IDID";
    int N = Str.length();
    int* ptr = DiStirngMatch(Str);
    for (int i = 0; i <= N; i++)
        cout << ptr[i] << " ";
  
    return 0;
}


Java
// Java program to generate an increasing
// and decreasing array
class GFG{
  
// Function that returns generated array
static int []DiStirngMatch(String Str)
{
    int N = Str.length();
  
    // Dynamically allocate array
    int []arr = new int[N + 1];
  
    // START=0, END=N
    int START = 0, END = N;
  
    // Iterate over array
    for(int i = 0; i < N; i++)
    {
          
        // if Str[i]=='I' assign arr[i]
        // as START and increment START
        if (Str.charAt(i) == 'I')
            arr[i] = START++;
  
        // if str[i]=='D' assign arr[i]
        // as END and decrement END
        if (Str.charAt(i) == 'D')
            arr[i] = END--;
    }
  
    // Assign A[N] as START
    arr[N] = START;
  
    // Return starting
    // address of array A
    return arr;
}
  
// Driver code
public static void main(String[] args)
{
    String Str = "IDID";
    int N = Str.length();
    int[] ptr = DiStirngMatch(Str);
      
    for(int i = 0; i <= N; i++)
        System.out.print(ptr[i] + " ");
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 program to generate an 
# increasing and decreasing array
  
# Function that returns generated array
def DiStirngMatch(Str):
  
    N = len(Str)
  
    # Dynamically allocate array
    arr = (N + 1) * [0]
  
    # START, END= 0 ,N
    START, END = 0, N
  
    # Iterate over array
    for i in range (N):
  
        # If Str[i]=='I' assign arr[i]
        # as START and increment START
        if (Str[i] == 'I'):
            arr[i] = START
            START += 1
  
        # If str[i]=='D' assign arr[i]
        # as END and decrement END
        if (Str[i] == 'D'):
            arr[i] = END
            END -= 1
  
    # Assign A[N] as START
    arr[N] = START
      
    # Return starting
    # address of array A
    return arr
  
# Driver code
if __name__ == "__main__":
  
    Str = "IDID"
    N = len(Str)
    ptr = DiStirngMatch(Str)
      
    for i in range (N + 1):
        print(ptr[i], end = " ")
  
# This code is contributed by chitranayal


C#
// C# program to generate an increasing
// and decreasing array
using System;
  
class GFG{
  
// Function that returns generated array
static int []DiStirngMatch(String Str)
{
    int N = Str.Length;
  
    // Dynamically allocate array
    int []arr = new int[N + 1];
  
    // START=0, END=N
    int START = 0, END = N;
  
    // Iterate over array
    for(int i = 0; i < N; i++)
    {
          
        // if Str[i]=='I' assign arr[i]
        // as START and increment START
        if (Str[i] == 'I')
            arr[i] = START++;
  
        // if str[i]=='D' assign arr[i]
        // as END and decrement END
        if (Str[i] == 'D')
            arr[i] = END--;
    }
  
    // Assign A[N] as START
    arr[N] = START;
  
    // Return starting
    // address of array A
    return arr;
}
  
// Driver code
public static void Main(String[] args)
{
    String Str = "IDID";
    int N = Str.Length;
    int[] ptr = DiStirngMatch(Str);
      
    for(int i = 0; i <= N; i++)
        Console.Write(ptr[i] + " ");
}
}
  
// This code is contributed by amal kumar choubey


输出:
0 4 1 3 2

时间复杂度: O(N)
辅助空间: O(N)