超记载
dugu108 · 共 29 篇文章
js闭包是什么?
尽管本身不太会js,不过通过这个文章理解了闭包 js闭包是什么? js闭包是什么?我初次接触“闭包”时,看了很多资料,总是无法理解,因为一些文章写得太“学术化”,虽然措辞非常严谨,但是对初学这来说,太难理解了。 自从看到这篇文章,我的眼前“豁然开朗” 一、什么是闭包? “官方”的解释是:所谓“闭包”,指的是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分。 相信很少有人能直接看懂这句话,因为他描述的太学术。我想用如何在Javascript中创建一个闭包来告诉你什么是闭包,因为跳过闭包的创建过程直接理解闭包的定义是非常困难的。看下面这段代码: …
2013-05-07 16:07 · 转载 · 评论(0)
堆Heap
public class Heap { public static void siftUp(int[] array, int i) { if (array == null || array.length <= 1 || i < 0 || i > array.length) { return; } while (true) { if (array[(i - 1) / 2] < array[i]) { swap(array, i, (i - 1) / 2); } else { break; } i = (i - 1) / 2; } } public static void siftDown(in…
2013-02-23 16:39 · 随手记 · 评论(0)
栈内元素排序
分别是利用一个辅助栈和两个辅助栈 public class StackSort { public static void sortWithOneCache(Stack<Integer> stack) { if (stack == null || stack.isEmpty()) { return; } Stack<Integer> cache = new Stack<Integer>(); while (!stack.isEmpty()) { int tmp = stack.pop(); while (!cache.isEmpty() && tmp < cache.peek()) { sta…
2013-02-16 20:55 · 每日一题 · 评论(0)
二叉查找树任意两个节点的最近共同父节点
找出二叉查找树Binary search tree上任意两个节点的最近共同父节点 public class Solution { public static int findFather(BinarySearchTreeNode root, BinarySearchTreeNode n1, BinarySearchTreeNode n2) { if (n1.value < root.value && n2.value > root.value) { return root.value; } if (n1.value < root.value && n2.value < root.value)…
2013-01-29 16:19 · 每日一题 · 评论(0)
Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. /** * Defini…
2013-01-29 15:34 · Leetcode · 评论(0)
Distinct Subsequences
Given a string S and a string T , count the number of distinct subsequences of T in S . A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "AC…
2013-01-29 15:03 · Leetcode · 评论(0)
Populating Next Right Pointers in Each Node II
Follow up for problem " Populating Next Right Pointers in Each Node ". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 7 After calling your functi…
2013-01-29 13:52 · Leetcode · 评论(0)
Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL . Initially, all next pointers are set to NULL . Note: You may …
2013-01-29 11:33 · Leetcode · 评论(0)
Pascal's Triangle II
Given an index k , return the k th row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1] . Note: Could you optimize your algorithm to use only O ( k ) extra space? import java.util.ArrayList; public class Solution { public ArrayList<Integer> getRow(int rowIndex) { ArrayList<Integ…
2013-01-28 23:16 · Leetcode · 评论(0)
Pascal's Triangle
Given numRows , generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] public class Solution { public ArrayList<ArrayList<Integer>> generate(int numRows) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList…
2013-01-28 17:19 · Leetcode · 评论(0)
找出数组中第k小的元素
给定一无序整型数组a,在不排序的情况下,查找第k小的元素 类似快排。 如果k非常小比如小于3,觉得应该用几个临时变量然后遍历一遍解决。 public class MinKth { public static int calculate(int[] array, int k) { return calculate(array, k, 0, array.length - 1); } public static int calculate(int[] array, int k, int low, int high) { int target = array[low]; int i = low; i…
2013-01-28 16:52 · 每日一题 · 评论(0)
Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if yo…
2013-01-28 15:31 · Leetcode · 评论(0)
Best Time to Buy and Sell Stock II
Say you have an array for which the i th element is the price of a given stock on day i . Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transacti…
2013-01-26 14:40 · Leetcode · 评论(0)
Best Time to Buy and Sell Stock
Say you have an array for which the i th element is the price of a given stock on day i . If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. public class Solution { public int maxProfit(int[] …
2013-01-26 14:31 · Leetcode · 评论(0)
Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6 . /** * Definition for binary tree public class TreeNode { int val; TreeNode left; * TreeNode right; TreeNode(int x) { val = x; } } */ pub…
2013-01-26 11:33 · Leetcode · 评论(0)