Easy

Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem.

Example

For inputArray = [1, 2, 1], elemToReplace = 1, and substitutionElem = 3, the output should be
arrayReplace(inputArray, elemToReplace, substitutionElem) = [3, 2, 3].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    0 ≤ inputArray.length ≤ 10^4,
    0 ≤ inputArray[i] ≤ 10^9.

  • [input] integer elemToReplace

    Guaranteed constraints:
    0 ≤ elemToReplace ≤ 10^9.

  • [input] integer substitutionElem

    Guaranteed constraints:
    0 ≤ substitutionElem ≤ 10^9.

  • [output] array.integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
arr_integer arrayReplace(arr_integer inputArray,int elemToReplace,int substitutionElem)
{
	for(int i=0;i<inputArray.size;i++)
		inputArray.arr[i]=inputArray.arr[i]==elemToReplace?substitutionElem:inputArray.arr[i];

	return inputArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> variableName  (0) 2020.04.05
<Codesignal> evenDigitsOnly  (0) 2020.04.05
<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05

+ Recent posts