Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
此題可設idx來記錄不為0的位置,此題只要管idx是否等於i,若一樣就不用更新,若不一樣就更新即可。
//2022_04_26
void moveZeroes(int* nums, int numsSize){
int i = 0, idx = 0;
for(i = 0; i < numsSize; i++){
if(nums[i] != 0){
if(i != idx){
nums[idx] = nums[i];
nums[i] = 0;
}
idx++;
}
}
}
https://leetcode.com/problems/move-zeroes/
Leetcode
LeetCode 445. Add Two Numbers II Description You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example Input: l1 = [7,2,4,3], l2 = [5,6,4] Output: [7,8,0,7]
Jun 7, 2022LeetCode 2. Add Two Numbers Description You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807.
Jun 7, 2022LeetCode 7. Reverse Integer Description Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example Input: x = 123 Output: 321
Jun 7, 2022LeetCode 121. Best Time to Buy and Sell Stock Description You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example Input: prices = [7,1,5,3,6,4]
Jun 3, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up