GithubHelp home page GithubHelp logo

from_arylo's People

Contributors

arylo avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

from_arylo's Issues

算法题

image

import test from 'ava';

/* eslint-disable no-plusplus,no-continue */

let no = 0;

function fn(g, deep = 1) {
  const arr = [ ];
  const m = g.length || 0;
  const n = (g[0] || []).length || 0;
  const top = deep - 1;
  const bottom = m - deep;
  const left = deep - 1;
  const right = n - deep;
  // console.log(m, n, top, bottom, left, right);
  for (let i = 0; i < n; i++) {
    if (i < left || right < i) {
      continue;
    }
    // console.log('t', g[top][i]);
    arr.push(g[top][i]);
  }
  if (bottom - top > 1) {
    for (let j = top + 1; j < bottom; j++) {
      // console.log('r', g[j][right]);
      arr.push(g[j][right]);
    }
  }
  if (bottom - top > 0) {
    for (let i = 0; i < n; i++) {
      if (i < left || right < i) {
        continue;
      }
      // console.log('b', i, g[bottom][n - i - 1]);
      arr.push(g[bottom][n - i - 1]);
    }
  }
  if (bottom - top > 1 && right - left > 0) {
    for (let j = top + 1; j < bottom; j++) {
      // console.log('l', g[bottom - j][left]);
      arr.push(g[bottom - j][left]);
    }
  }
  if (bottom - top > 1 && right - left > 1) {
    arr.push(...fn(g, deep + 1));
  }
  return arr;
}

test(`#${++no}`, (t) => {
  const g = [];
  t.deepEqual(fn(g), []);
});

test(`#${++no}`, (t) => {
  const g = [
    []
  ];
  t.deepEqual(fn(g), []);
});

test(`#${++no}`, (t) => {
  const g = [
    [1, 2],
  ];
  t.deepEqual(fn(g), [1, 2]);
});

test(`#${++no}`, (t) => {
  const g = [
    [1, 2],
    [3, 4],
  ];
  t.deepEqual(fn(g), [1, 2, 4, 3]);
});

test(`#${++no}`, (t) => {
  const g = [
    [1, 2, 2],
    [3, 4, 4],
  ];
  t.deepEqual(fn(g), [1, 2, 2, 4, 4, 3]);
});

test(`#${++no}`, (t) => {
  const g = [
    [1, 2],
    [3, 4],
    [5, 6],
  ];
  t.deepEqual(fn(g), [1, 2, 4, 6, 5, 3]);
});

test(`#${++no}`, (t) => {
  const g = [
    [1, 2, 3],
    [3, 4, 5],
    [5, 6, 7],
  ];
  t.deepEqual(fn(g), [1, 2, 3, 5, 7, 6, 5, 3, 4]);
});

test(`#${++no}`, (t) => {
  const g = [
    [1, 2, 3],
    [3, 4, 5],
    [5, 6, 7],
    [7, 8, 9],
  ];
  t.deepEqual(fn(g), [1, 2, 3, 5, 7, 9, 8, 7, 5, 3, 4, 6]);
});

test(`#${++no}`, (t) => {
  const g = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16],
  ];
  t.deepEqual(fn(g), [1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]);
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.