• leetcode原地删除系列27


    /**
    <p>给定一个数组 <code>nums</code>,编写一个函数将所有 <code>0</code> 移动到数组的末尾,同时保持非零元素的相对顺序。</p>
    
    <p><strong>请注意</strong>&nbsp;,必须在不复制数组的情况下原地对数组进行操作。</p>
    
    <p>&nbsp;</p>
    
    <p><strong>示例 1:</strong></p>
    
    <pre>
    <strong>输入:</strong> nums = <code>[0,1,0,3,12]</code>
    <strong>输出:</strong> <code>[1,3,12,0,0]</code>
    </pre>
    
    <p><strong>示例 2:</strong></p>
    
    <pre>
    <strong>输入:</strong> nums = <code>[0]</code>
    <strong>输出:</strong> <code>[0]</code></pre>
    
    <p>&nbsp;</p>
    
    <p><strong>提示</strong>:</p>
    <meta charset="UTF-8" />
    
    <ul>
    	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
    	<li><code>-2<sup>31</sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li>
    </ul>
    
    <p>&nbsp;</p>
    
    <p><b>进阶:</b>你能尽量减少完成的操作次数吗?</p>
    <div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div><br><div><li> 1525</li><li> 0</li></div>
    */
    
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public void moveZeroes(int[] nums) {
            if(nums.length==0){
                return ;
            }
            int fast=0,slow=0;
            for (int i = 0; i < nums.length; i++) {
                if(nums[fast]!=0){
                    nums[slow]=nums[fast];
                    slow++;
                }
                fast++;
            }
            for (int i = slow; i <nums.length ; i++) {
                nums[i]=0;
            }
            return;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
    
    
  • 相关阅读:
    RabbitMQ
    连接池,为什么要使用连接池?
    mac 安装arcanist
    感触
    UDP socket
    Servlet过滤器
    PL、SQL
    springmvc 文件上传实现(不是服务器的)
    注解spring
    excel工具类
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/16106321.html
Copyright © 2020-2023  润新知