Giter VIP home page Giter VIP logo

helper's Introduction

leetcode-helper(by golang)

中文版

this library helps you with:

  • convert problem's input to golang data structure which used by leetcode
  • display some data structure by leetcode problem's input(string)

now only linked list, binary tree, graph, int array, int matrix are supported

story

i prefer using IDE(goland) to solve leetcode problems, you can debug your code easily,

leetcode will provide some test cases which consists of input and output,

so we can use these test cases to test our code.

also, when our submission failed, leetcode will be kind to provide the corresponding input and output of the test case.

but the leetcode problem input is just a string,

for example: [9,6,-3,null,null,-6,2,null,null,2,null,-6,-6,-6], this input represents a tree,

it's hard for us to know what's the tree looks like,

so i write this library, with it's help you can convert the input to golang data structure to test your code

and display what does it look like.

Tree

leetcode use a json array as input to represent a tree,

it's hard for us to read the tree.

with the helper, you can turn the input to a readable one,

there are two way to display tree:

print in console

for example:

func TestPrintTree(t *testing.T) {
	input := `[9,6,-3,null,null,-6,2,null,null,2,null,-6,-6,-6]`
	tree := ParseTreeFromInput(input)
	PrintTree(``, tree)
}

the function GetTreeFromInput turn the input to a tree,

and the func PrintTree will print the tree in text

the following is the printed tree:

         |-- 2
                 |-- -6
             |-- 2
                 |-- -6
                     |-- -6
     |-- -3
         |-- -6
 |-- 9
     |-- 6

the root is at the left most place, the left child locate blow the parent,

the right child locate above the parent.

so the root is 9, it's left child is 6, it's right child is -3

for the node -3, it's right child is 2

rendered in pictures

for example:

func TestRenderTree(t *testing.T) {
	input := `[9,6,-3,null,null,-6,2,null,null,2,null,-6,-6,-6]`
	tree := ParseTreeFromInput(input)
	RenderTree(tree)
}

RenderTree will pop up window:

LinkedList

the example code:

func TestLinkedList(t *testing.T) {
	input := `1,2,3,4,5,6`
	head := ParseLinkedListFromStr(input)
	// get int linked list instead of string
	//head := GetIntLinkedListFromStr(input)
	PrintLinkedListNode(head)
}

the output is

1->2->3->4->5->6->

Graph

for now, i met two types of input string to represent graph:

input represent graph

for example:

example code:

func TestGraph(t *testing.T) {
	input := `[[4,3,1],[3,2,4],[3],[4],[]]`
	graph := ParseGraphInput(input)
	RenderGraph(graph)
}

RenderGraph will pop up a window displaying the graph(only tested on mac):

input represent edges

for example:

example code:

func TestGraphEdges(t *testing.T) {
	input := `[[0,1],[0,2],[2,5],[3,4],[4,2]]`
	edges := ParseEdgesInput(input)
	RenderGraphByEdges(edges)
}

rendered graph:

render graph implemented by map[int][]int

if you use map[int][]int as a map, you can render it with func : RenderGraphByEdgesMap

int matrix

example:

func TestIntMatrix(t *testing.T) {
	input := `[[0,0,1,1],[1,0,1,0],[1,1,0,0]]`
	matrix := ParseIntMatrix(input)
	PrintIntMatrix(matrix)
}

the output is :

[0 0 1 1]
[1 0 1 0]
[1 1 0 0]

int array

example:

func TestIntArray(t *testing.T){
    input := `[0,0,1,1]`
    ParseIntSlice(input)
}

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.