zoukankan      html  css  js  c++  java
  • 编程练习-只用0交换排序数组

    题目来源:华为模拟题

    题目描述

    华为模拟测试题,蛮简单,一个长度为n的整型数组,乱序存放0至n-1,要求只能交换0和其中的一个数,对数组进行排序(也没有说升序还是降序),交换的函数他已经写好了。给出如下结构:

    public class Solution {
    
    public void swapWithZero(int[] array, int len, int n) {
            Main.SwapWithZero(array, len, n);
        }
    
    public void sort(int[] array, int len) {
            // 完成这个函数
        }
    }
    

    要求完成这个sort函数,在sort函数中调用swapWithZero函数进行排序,注意这里的n是指要调换的数,并不是在数组中的下标!

    分析

    首先需要始终明确几点:

    1. 数组为[0, n-1],是连续的,只是乱序的。
    2. 从始至终只能交换0和其中某个数。
    3. 不需要考虑怎么交换
    4. 最终排好的数组,下标和该位置上的数相同,这点非常重要

    基本思想

    1. 首先将0放到第一位上,还有1->n-1的位置上需要排序
    2. 循环n-1次,每次将一个数放到相应的位置上。这里就要用到数组的特点,第1次循环(0已经在下标为0的位置上了)将1放到下标为1的位置上,第2次循环将2放到下标为2的位置上,……,第n-1次循环,将n-1放在n-1的位置上!
      下面来看一下我采用的示例{4,5,0,3,1,2}
      首先看一下手工推导过程

      这里只是将1交换到相应位置处的过程!
    3. 第一步将0和初始数组第一个数交换,将0交换到第一位。
    4. 第二步将0和1交换,将1交换到第一个位置上。
    5. 第三步将当前正在处理(循环处)的位置下标处(位置1)的数和0交换(有点绕)。
    6. 第四步将1和0交换,这样1就到了应该呆的位置上了。同时0又回到了第一个,方便下次操作!

    实现(java 1.8)

    package sxtest20190409;
    
    /**
     * @ClassName: Solution
     * @Author: fanjiajia
     * @Date: 2019/4/9 下午8:12
     * @Version: 1.0
     * @Description:
     */
    public class Solution {
        public static void main(String[] args) {
            int[] array = {4,5,0,3,2,1};
            Solution solution = new Solution();
            solution.sort(array, array.length);
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + " ");
            }
    
        }
        public void sort(int[] array, int len) {
            // 完成这个函数
            // 将0移动到第一位
            int firstNum = array[0];
            swapWithZero(array, len, firstNum);
    
            // 还有位置1->n-1位置的数需要移动
            for (int index = 1; index < len; index++) {
                //每次循环将index处放置该位置应该的数(index本身)
                swapWithZero(array,len, index); // 将index 放在首位
                int indexNum = array[index];
                swapWithZero(array, len, indexNum);
                swapWithZero(array, len, index);
            }
        }
    
        public void swapWithZero(int[] array, int len, int n) {
            Main.SwapWithZero(array, len, n);
        }
    
    }
    
    class Main {
        /**
         * 交换数组中0和n
         * @param array
         * @param len
         * @param n
         */
        public static void SwapWithZero(int[] array, int len, int n) {
            /**
             * 1 0的位置更改为n
             * 2 n的位置改为0
             */
            for (int i = 0; i < len; i++) {
                if (array[i] == 0) {
                    array[i] = n;
                }
                else if (array[i] == n) {
                    array[i] = 0;
                }
            }
        }
    }
    

    这里自定义实现了Main类,以及其中的SwapWithZero函数,这里交换相对简单,直接赋值即可。

    最后

    此致,敬礼

  • 相关阅读:
    CentOS7使用集群同步脚本对文件同步分发
    CentOS7安装jdk1.8
    CentOS7+CDH5.12.1集群搭建
    nginx通配符
    Nginx反向代理及配置
    一些好玩有用的书签
    linux操作小技巧锦集
    系统压测结果对比:tomcat/thinkphp/swoole/php-fpm/apache
    python修改linux日志(logtamper.py)
    【原创】给定随机数的取值范围(最小值、最大值),且要求多次取得的随机数最后的结果有一个固定的平均值
  • 原文地址:https://www.cnblogs.com/numen-fan/p/10679992.html
Copyright © 2011-2022 走看看