说明

  • LeetCode提供的样本输入,显示上是数组Array,而后台的实际测试用例则是树TreeNode,链表ListNode等。
  • 如果你是在页面手撸代码直接提交的,那没什么影响。
  • 如果你是在本地IDE编写的代码,你就需要把样本输入拷贝下来,转换成相应的数据类型,再编写核心算法进行测试。
  • 我们只需要提交核心算法,不需要提交测试数据初始化代码。
  • 本文记录公用的初始化代码,不占其他随笔篇幅。

ListNode

function ListNode(val) {
this.val = val;
this.next = null;
}

arrayToListNode

/**
* @param {number[]} arr
* @return {ListNode}
*/
function arrayToListNode(arr) {
if (arr.length === 0) return null;
let root = new ListNode(arr.shift());
root.next = arrayToListNode(arr);
return root;
}

arrayToListNodeArray

/**
* @param {number[][]} arr
* @return {ListNode[]}
*/
function arrayToListNodeArray(arr) {
let result = [];
for (let a of arr) {
let dummy = new ListNode();
let head = dummy;
for (let i of a) {
dummy.next = new ListNode(i);
dummy = dummy.next;
}
result.push(head.next);
}
return result;
}

LeetCode Input Initial Code的更多相关文章

  1. jQuery选择器中,通配符[id^='code']input[id$='code'][id*='code']

     1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code']&qu ...

  2. [LeetCode] Unique Morse Code Words 独特的摩斯码单词

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  3. [LeetCode] 89. Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  4. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  5. 【leetcode】Gray Code (middle)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  6. adb shell input keyevent code详解

    adb shell input keyevent 7 # for key '0' adb shell input keyevent 8 # for key '1' adb shell input ke ...

  7. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  8. leetcode[88] Gray Code

    题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...

  9. LeetCode题目:Gray Code

    原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...

随机推荐

  1. 146-PHP 使用<<<和HTML混编(二)

    <?php $html=<<<HTM1 <title>PHP输出HTML代码</title> <body> <a href=#> ...

  2. centos7搭建kafka集群

    一.安装jdk 1.下载jdk压缩包并移动到/usr/local目录 mv jdk-8u162-linux-x64.tar.gz /usr/local 2.解压 tar -zxvf jdk-8u162 ...

  3. 【剑指Offer】面试题05.替换空格

    题目 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 示例 1: 输入:s = "We are happy." 输出:"We%20are ...

  4. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

  5. ImportError: dynamic module does not define init function (initcaffe)

    https://github.com/BVLC/caffe/issues/2770 $ python2 -c "import caffe" Traceback (most rece ...

  6. 四个因素决定Essay写作段落长度

    段落是一篇Essay的基石,写好Essay应从写好段落开始.那么Essay写作中一个段落多长为好?英语和修辞学教授理查德·诺德奎斯特著文介绍了一些专家的观点.从以下的译文可以看到,段落长度虽然没有固定 ...

  7. 2020PHP面试-PHP篇

    一.列举一些PHP的设计模式 单例模式:保证在整个应用程序的生命周期中,任何一个时刻,单例类的实例都只存在一个,同时这个类还必须提供一个访问该类的全局访问点. 工厂模式:定义一个创建对象的接口,但是让 ...

  8. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war

    创建springboot项目,且不采用<parent>引入springboot时,pom.xml如下: <?xml version="1.0" encoding= ...

  9. Redis: Reducing Memory Usage

    High Level Tips for Redis Most of Stream-Framework's users start out with Redis and eventually move ...

  10. Mybatis框架的简单配置

    Mybatis 的配置 1.创建项目(当然,这是废话) 2.导包 下载mybatis-3.2.0版:https://repo1.maven.org/maven2/org/mybatis/mybatis ...