#96. 查找数组中首个特定元素前缀

    ID: 96 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatArray-2gesp3一维数组循环结构

查找数组中首个特定元素前缀

Elements Before First 4

Background

Congcong is playing a number game. He has an array of integers, and now he wants to know all the numbers that appear before the first occurrence of the number 4 in the array.

Problem Description

Given a non-empty array of integers, return a new array containing the elements from the original array that come before the first 4 in the original array. The original array will contain at least one 4. Note that it is valid in Java to create an array of length 0.

Input Format

Input is given from Standard Input in the following format.

A single line of integers, representing the elements of the array, separated by spaces.

Output Format

Output is printed to Standard Output in the following format.

A single line of integers, representing the elements of the new array, separated by commas and spaces, enclosed in square brackets.

Sample

1 2 4 1
[1, 2]
3 1 4
[3, 1]
1 4 4
[1]

Sample Explanation

For Sample 1, the input array is 1 2 4 1. The first 4 appears at index 2. The elements before it are 1 and 2. Thus, the output is [1, 2].

Constraints

Time limit: 1 second Memory limit: 1024 KiB