给定一矩阵,从中心位置开始逆时针排序输出

面试题目:给定一个矩阵,实现从中心位置逆时针输出(当时让我自己定方向,反正就是从中间开始走圈圈,这个题我就定逆时针来做了)

算法思想:

1、用二维数组存储矩阵

2、找到中心点,用indexi=Math.floor(row/2),indexj=Math.floor(col/2),找到中心点的索引

3、设一个count变量,用于计算每个方向需要走多少步,赋初值count=1;然后每走两个方向count++;

4、每个方向边界判断

  这儿有几个坑:
    1)、因为我们的每一个循环是走四个方向,所以如果当在中途就超过了我们的循环条件,result.length>len,
    那么循环还会继续执行,所以我设置了每一个方向前判断一下,达到了就退出循环
    2)、当我们单纯的控制indexi和indexj不超出自己的边界,是不可行的,因为当row和col相距太大时,就不容易取到剩下的值
    所以,就算indexi和indexj超出了范围,我们可以不push到result,但是索引还是作相应的增加,当然这时候你在nums中也找不到,会报错。

5、把结果存入result数组,一个一个的push进去的,也就是题目要求的顺序了

 

在代码中,我是自己在浏览器中手动设置矩阵:

<div id="container">
        <label for="conten">向二维数组添加内容</label>
        <input type="text" name="content" id="" v-model="num">
        <button @click="addNum" v-show="flag">添加</button>
        <button @click="addNext" v-show="!flag">添加下一行</button>
        <span style="color:red" v-if="err">{{worning}}</span>
        <br />
        <!-- {{num}} -->
        <br />
        <!-- {{nums}} -->
        <!-- 遍历nums数组,展现矩阵 -->
        <hr />
        <div>输入的矩阵为:
            <table>
                <tr v-for='(item, index) in nums'>
                    <template v-for='items in item'>
                        <template v-for='(itemss, indexs) in items'>
                            <td>{{itemss}}</td>
                        </template>
                    </template>
                </tr>
            </table>
        </div>
        <br />
        <div>您目前共添加了{{row}}行,{{col}}列</div>
        <br />
        <button @click="sorftNums">从中间按逆时针顺序输出</button>
        {{result}}
    </div>

 

 

var vm = new Vue({
            el: '#container',
            data: {
                nums: [], //存放矩阵
                num: [], //存放每次加入的行数据
                row: 0,
                col: 0,
                flag: true, //判断按钮显示
                worning: "添加列数与第一次不同,请重新输入",
                err: false,
                result: [], //存放输出结果
            },
            methods: {
                // 第一次加数据
                addNum() {
                    this.nums.push(this.num);
                    this.col = this.num.length;
                    this.num = [];
                    this.row++;
                    this.flag = !this.flag;
                },
                addNext() {
                    if (this.num.length === this.col) {
                        this.nums.push(this.num);
                        this.num = [];
                        this.row++;
                    } else {
                        this.err = true;
                        this.num = [];
                    }
                },
                // 排序
                sorftNums() {
                    // 找到中心位置数据
                    let first;
                    let indexi = Math.floor(this.row / 2);
                    let indexj = Math.floor(this.col / 2);
                    first = this.nums[indexi][indexj];
                    // console.log(first);
                    // console.log(Math.floor(this.row/2),Math.floor(this.col/2));
                    // 判断当前步数是否走完
                    let n;
                    // 每走两个方向,单向步数加一 
                    let count = 1;
                    let len = this.row * this.col;
                    this.result.push(parseInt(first));

                    while (this.result.length < len) {
                        //向左走
                        n = count;
                        while (n > 0) {
                            indexj = indexj - 1;
                            if (indexj >= 0 && indexi >= 0) {
                                this.result.push(parseInt(this.nums[indexi][indexj]));
                            }
                            n--;
                        }

                        //避免加入重复数据
                        if (this.result.length >= len) break;

                        //向下走
                        n = count;
                        while (n > 0) {
                            indexi = indexi + 1;
                            if (indexi < this.row && indexj >= 0) {
                                this.result.push(parseInt(this.nums[indexi][indexj]));
                            }
                            n--;
                        }


                        if (this.result.length >= len) break;

                        // 每走两个方向,走的步数加一
                        count += 1;

                        //向右走
                        n = count;
                        while (n > 0) {
                            indexj = indexj + 1;
                            if (indexj < this.col && indexi < this.row) {
                                this.result.push(parseInt(this.nums[indexi][indexj]));
                            }
                            n--;
                        }

                        if (this.result.length >= len) break;

                        //向上走
                        n = count;
                        while (n > 0) {
                            indexi = indexi - 1;
                            if (indexi >= 0 && indexj < this.col) {
                                this.result.push(parseInt(this.nums[indexi][indexj]));
                            }
                            n--;
                        }

                        // 每走两个方向,走的步数加一
                        count += 1;
                    }

                }

            }
        });

这儿给出了body内的代码,大家若想看看源码,可以到我的github去clone,因为最近在学习Vue.js,所以我还是用的vue相关语法,数据展示双向绑定也很方便。 欢迎大家给予更优的解法。一起讨论讨论。

github地址:git clone https://github.com/FlowerOfSummer/Counterclockwise-matrix.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DiuDiu_yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值