zoukankan      html  css  js  c++  java
  • 【原创】将一个数组中的0元素全部排到数组的右边,其他元素相对顺序保持不变。

    思想:首先初始化一个和给定数组(arr)一样大小的数组(newArr),然后将arr中不为0的元素依次放在newArr中。这时我们可以考虑,如果是说将数组中的*放在数组的左边或者右边我们只需要将数组初始化全为*,然后将arr中不为*的元素一次放在newArr的左边或者右边。
    namespace 测试
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] arr = {2,3,0,1,0,4,4,0,8,3,5};
                int[] newArr=new int[arr.Length];
                int count = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] != 0)
                    {
                        newArr[count] = arr[i];
                        count = count + 1;
                    }
              
                }
                foreach (int m in newArr)
                {
                    Console.Write(m+" ");
                }
                Console.ReadLine();
            }
        }
        
    }
  • 相关阅读:
    SpringBoot笔记
    SpringBoot面试篇
    多线程篇
    Tomcat篇
    Redis篇
    Nginx篇
    JVM篇
    MySQL篇
    python ETL工具 pyetl
    python通用数据库操作工具 pydbclib
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/chenyong.html
Copyright © 2011-2022 走看看