आसान 0 हल किए गए 0% स्वीकृति

Move Zeroes

Move every zero to the end while keeping the other order.

आप क्या सीखेंगे

  • Move elements with a write pointer
  • Keep the relative order of non-zero values
  • Fill the remaining positions with zeros

समस्या

अनुवाद दिखाएँ

Return the list with all zeros moved to the end. The non-zero values keep their relative order.

Implement move_zeroes(numbers).

Input

numbers: The list of integers.

Output

Return the rearranged list.

सीमाएँ

0 ≤ length ≤ 100,000

उदाहरण

उदाहरण 1
इनपुट:
numbers = [0, 1, 0, 3, 12]

अपेक्षित आउटपुट:
expected = [1, 3, 12, 0, 0]
इनपुट:
std::vector<long long> numbers = {0, 1, 0, 3, 12};

अपेक्षित आउटपुट:
std::vector<long long> expected = {1, 3, 12, 0, 0};
इनपुट:
long[] numbers = new long[] {0, 1, 0, 3, 12};

अपेक्षित आउटपुट:
long[] expected = new long[] {1, 3, 12, 0, 0};

व्याख्या

1, 3, 12 keep their order and the two zeros go last: [1, 3, 12, 0, 0].

उदाहरण 2
इनपुट:
numbers = [0, 0]

अपेक्षित आउटपुट:
expected = [0, 0]
इनपुट:
std::vector<long long> numbers = {0, 0};

अपेक्षित आउटपुट:
std::vector<long long> expected = {0, 0};
इनपुट:
long[] numbers = new long[] {0, 0};

अपेक्षित आउटपुट:
long[] expected = new long[] {0, 0};

व्याख्या

Only zeros: the list stays [0, 0].

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Non-zero values should keep their order while zeros drift to the end.

संकेत 2

A two-pointer or write-index approach lets you rebuild the list in one pass.

संकेत 3

Do not reorder the non-zero values when compacting them.

अपेक्षित जटिलता

O(n) time, O(1) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।