2460. Apply Operations to an Array Easy

@problem@discussion
#Array#Simulation



1/**
2 * [2460] Apply Operations to an Array
3 *
4 * You are given a 0-indexed array nums of size n consisting of non-negative integers.
5 * You need to apply n - 1 operations to this array where, in the i^th operation (0-indexed), you will apply the following on the i^th element of nums:
6 * 
7 * 	If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.
8 * 
9 * After performing all the operations, shift all the 0's to the end of the array.
10 * 
11 * 	For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].
12 * 
13 * Return the resulting array.
14 * Note that the operations are applied sequentially, not all at once.
15 *  
16 * <strong class="example">Example 1:
17 * 
18 * Input: nums = [1,2,2,1,1,0]
19 * Output: [1,4,2,0,0,0]
20 * Explanation: We do the following operations:
21 * - i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
22 * - i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,<u>4</u>,<u>0</u>,1,1,0].
23 * - i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
24 * - i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,<u>2</u>,<u>0</u>,0].
25 * - i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,<u>0</u>,<u>0</u>].
26 * After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
27 * 
28 * <strong class="example">Example 2:
29 * 
30 * Input: nums = [0,1]
31 * Output: [1,0]
32 * Explanation: No operation can be applied, we just shift the 0 to the end.
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	2 <= nums.length <= 2000
38 * 	0 <= nums[i] <= 1000
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/apply-operations-to-an-array/
44// discuss: https://leetcode.com/problems/apply-operations-to-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn apply_operations(nums: Vec<i32>) -> Vec<i32> {
50        vec![]
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_2460() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.