- 浏览: 183447 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
给定一个数组,将所有的0放到数组的后面,元素之间的相对位置保持不变。我们用双指针来解决。从数组第一个元素开始,如果不为0,就用index指针记录这个值,index++, 直到遍历完数组,这样index的值就是数组中不为0的元素的个数,然后从index开始,将后面的元素变为0。代码如下:
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
给定一个数组,将所有的0放到数组的后面,元素之间的相对位置保持不变。我们用双指针来解决。从数组第一个元素开始,如果不为0,就用index指针记录这个值,index++, 直到遍历完数组,这样index的值就是数组中不为0的元素的个数,然后从index开始,将后面的元素变为0。代码如下:
public class Solution { public void moveZeroes(int[] nums) { if(nums == null || nums.length == 0) return; int index = 0; for(int i = 0; i < nums.length; i++) { if(nums[i] != 0) { nums[index++] = nums[i]; } } for(int i = index; i < nums.length; i++) { nums[i] = 0; } } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
283_moveZeros.py MoveZeros.java 两数之和 twoSum.py 三数之和 15_threeSum.py 多数元素 169_majorityElement.py 删除排序数组中的重复项 26_removeDuplicate.py 移除元素 27_removeElement.py string 28. 实现 ...
第 338 章力码 王一飞的LeetCode题解 算法: | 问题 | 解决方案 | -----|---------------- ...(./solutions/MoveZeros.cc) | 第349话| [IntersectionOfTwoArrays.cc] (./solutions/IntersectionOfTwoArray
Leetcode经典01背包 ...Zeros | | | 27 Remove Element | | | 26 Remove Duplicates from Sorted Array | | | 80 Remove Duplicates from Sorted Array II | | | 75 Sort Colors 计数排序 | | | 88 Merge So
Zeros 283 [无] 728 747 3-4 即使简单的问题,也有很多优化的思路 283 27 26 80 3-5 三路快排partition思路的应用 Sort Color 75 88 215 3-6 对撞指针 Two Sum II - Input Array is Sorted 167 125 344 345 11 3-7 ...
Zeros 3-4 即使简单的问题,也有很多优化的思路 3-5 三路快排partition思路的应用 Sort Color 3-6 对撞指针 Two Sum II - Input Array is Sorted 3-7 滑动窗口 Minimum Size Subarray Sum 3-8 在滑动窗口中做记
I_move=uint8(zeros(s(1),s(2),3)); % 创建空白图像 I_move(1:s(1)-100,1:s(2)-100,:)=I(100+1:s(1),100+1:s(2),:); % 复制平移后的像素值 subplot(2,2,1); % 创建子图 imshow(I_move); % 显示平移后的图像 title('...
set(move_right, 'Callback', 'g2048right'); set(move_left, 'Callback', 'g2048left'); set(move_up, 'Callback', 'g2048up'); set(move_down, 'Callback', 'g2048down'); % 设置游戏的初始状态 flag = 0; ...
y = zeros(size(X)); % 计算移动平均 for i = 1:N:length(X) - N + 1 y(i:i+N-1) = mean(X(i:i+N-1)); end end ``` 在上述代码中,我们首先检查输入数据X是否为向量,以及N是否为正整数。然后,我们创建一个...
while iter < maxiter & move ~= 0 move = 0; for i = 1:n dist = sum((repmat(x(i,:), k, 1) - nc).^2, 2); radj = nr ./ (nr + 1) .* dist'; [m, ind] = min(radj); if ind ~= r cid(i) = ind; ic = find...
Python中的高阶函数是那些能够接受函数作为参数或者返回函数的函数,它们是函数式...这段代码首先通过 `move_zeros_to_end` 函数为每个元素分配一个优先级,然后使用 `sorted()` 对数组进行排序,使得0项被排到后面。
function move_zeros(nums) { let nonZeroCount = 0; for (let i = 0, lastNonZero = 0; i ; i++) { if (nums[i] !== 0) { [nums[i], nums[lastNonZero]] = [nums[lastNonZero], nums[i]]; lastNonZero++; ...
# Reassign new joint angles (all zeros) which we will later command to the limb angles['right_s0'] = 0.0 angles['right_s1'] = 0.0 angles['right_e0'] = 0.0 angles['right_e1'] = 0.0 angles['right_w0'] =...
C is a NxN cost (perhaps distance) matrix, where C(I,J) contains the value of the cost to move from point I to point J NOTE: only valid with A as the first input E is a Px2 matrix containing a ...
ImageNew = zeros(round(Xnew), round(Ynew)) + 255; % 计算原图像各像素的新坐标 for indexX = 0:(X-1) for indexY = 0:(Y-1) ImageNew(round(indexX * cos(Angle) - indexY * sin(Angle)) + round(abs(min([Left...
board = zeros(15); ``` 接着,我们需要编写下棋功能。这包括选择颜色(黑棋或白棋),输入坐标,并检查该位置是否合法(在棋盘范围内且为空)。下棋后,更新棋盘状态。 ```matlab function [isValid, board] = ...
RAIKIRI控制程序 机载传感器为Akizuki规格的LIS3DH(3轴加速度传感器模块)和Akizuki规格的AE-L3GD20(3轴陀螺仪传感器模块)。 输出规格 加速度通过i2c通信(Aruduino Uno A4...通过ENABLE_MOVEAVG为true ENABLE_MOVE
img = np.zeros((480, 640, 3), dtype=np.uint8) for i in range(0, 360, 15): x, y = heart_curve(t + i / 180 * pi, 100) cv2.circle(img, (320 + x, 240 + y), 5, (0, 0, 255), -1) out.write(img) out....
- **CLZ (Count Leading Zeros)** `CLZ RD, RS` 功能:计算源寄存器中最高有效位前连续0的数量。 示例:`CLZ $t0, $s0` 特点:用于位操作。 - **LA (Load Address)** `LA RD, LABEL` 功能:加载符号...