Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).
Solution 1:
It rerains ordering.
// move all zeros to end of array, keep the non-zero elements order public static void moveZeroToEnd(int[] A) { int n = A.length, cnt = 0; for(int i=0; i<n; i++) { if(A[i] != 0) { A[cnt++] = A[i]; } } while(cnt < n) { A[cnt++] = 0; } }
Solution 2:
It does not keep the non-zero elements order.
// move all zeros to end of array, does not keep the non-zero elements order public static void moveZeroRight(int[] A) { for(int i=0, j=A.length-1; i<j; i++) { if(A[i] == 0) { while(j>i && A[j] == 0) j--; swap(A, i, j); } } } private static void swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; }
相关推荐
java java_leetcode-73-set-matrix-zeroes
javascript js_leetcode题解之172-factorial-trailing-zeroes.js
javascript js_leetcode题解之73-set-matrix-zeroes.js
c语言入门 C语言_leetcode题解之73-set-matrix-zeroes.c
python python_leetcode题解之073_Set_Matrix_Zeroes
c c语言_leetcode题解之0073_set_matrix_zeroes.zip
java java_leetcode题解之Move Zeroes.java
3. MoveZeroes - 将数组中所有的0移动到数组的末尾,同时保持其他元素的相对顺序不变。这是考察基本数组操作和数据处理能力的问题。 4. MergeIntervals - 合并重叠的区间,这是一个常见的面试题目,考察对区间...
Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) repository. I'll keep updating for full summary and...
1.MoveZeroes |-- codedrinker |-- MoveZeroes.java 访问官网,注册账号 访问每个译文下面的Readme.md里面是链接地址和译文 完成编码以后,到LeetCode官网提交结果,接受以后再提交Pull Request合并到master
1.MoveZeroes |-- codedrinker |-- MoveZeroes.java 访问 官网,注册账号 编码完成以后,在LeetCode官网运行结果通过以后再提交代码到我们的仓库。 如果不理解 Git 怎么使用可以观看这个视频 加群 QQ 免费 607313352...
MOVE 'END-OF-FILE' TO SCREEN-LINE GOBACK. PERFORM UNTIL FILE-STATUS = 13 READ EMPLOYEE-FILE AT END MOVE 'EOF' TO SCREEN-LINE SET FILE-STATUS TO 13 NOT AT END DISPLAY EMP-RECORD END-READ. ...
如`to-lower-case`要求将字符串转换为小写,`length-of-last-word`要求找出字符串最后一个单词的长度,`valid-anagram`检查两个字符串是否为变位词,`group-anagrams`则是将所有变位词分组,`find-all-anagrams-in-a...
* fixed: WAV files beginning with lots of zeroes were sometimes not accepted v3.14 * WAV reading was broken for all but very small files (introduced in v3.13) v3.13 * fields and frames are counted ...
它受到堆栈溢出工作的启发( https://stackoverflow.com/questions/21354701/matlab-fprintf-to-keep-重要数字-and-rightmost- zeroes )。 这是它在行动中的一个例子>> sigfigstr(.000012431634,3) ans = '0....
This tutorial is an excellent way for Perl developers to move to the next level of development and make the most of some powerful, free tools. --Stephen W. Plain Product Description MySQL and Perl ...
【字符串知识】 ...示例题目包括《盛最多水的容器》(https://leetcode-cn.com/problems/container-with-most-water/)、《移动零》(https://leetcode-cn.com/problems/move-zeroes/)以及《爬楼梯》...
leetcode提交记录消失解决leetcode问题 ...https://leetcode.com/problems/roman-to-integer/description/ first-submission-successful : yes 2018-05-16 : - id : 172 type : math difficulty : easy url : ...