- 博客(29)
- 收藏
- 关注
原创 【Leetcode】:96. Unique Binary Search Trees 问题 in Go语言
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's.题目要求出func numTrees(n int) int { i
2016-04-29 15:41:23 385
原创 【Leetcode】:230. Kth Smallest Element in a BST 问题 in JAVA
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if t
2016-04-28 22:55:47 320
原创 【Leetcode】:328. Odd Even Linked List 问题 in JAVA
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in
2016-04-28 21:44:12 622
原创 【Leetcode】:344. Reverse String 问题 in JAVA
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".这道题很简单,但是我写的第一个版本居然超时了,没有通过,下面这个版本可以通过。public class Solution {
2016-04-27 16:06:55 1895
原创 【Leetcode】:287. Find the Duplicate Number 问题 in JAVA
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,
2016-04-27 15:41:28 370
原创 【Leetcode】:51. N-Queens 问题 in JAVA
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.
2016-04-25 19:16:59 2670 1
原创 【Leetcode】:13. Roman to Integer 问题 in JAVA
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.题目要求把罗马数字转换为阿拉伯数字,这个题很简单,只不过代码量比较大先查查维基百科罗马数字的知识,各个字母分别代表什么意思Symbol
2016-04-25 10:04:16 447
原创 【Leetcode】:94. Binary Tree Inorder Traversal 问题 in JAVA
题目难度中等,就是要求不用递归的情况下进行中序遍历:中序遍历就是先访问左孩子的值,再访问父节点的值,最后访问右孩子的值。这样的话,一棵树的最最左边的子节点的值一定是第一个被访问的所以实现起来的思路就是,沿着树一直向左走,直到走不动为止,这个时候再看看当前节点是否有右节点,如果有的话对该又节点进行前面的操作。/** * Definition for a binary tree
2016-04-24 12:21:42 543
原创 【Leetcode】:318. Maximum Product of Word Lengths 问题 in JAVA
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case le
2016-04-22 20:17:58 855
原创 【Leetcode】:144. Binary Tree Preorder Traversal 问题 in Go语言
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive soluti
2016-04-22 17:55:07 509
原创 【Leetcode】:268. Missing Number 问题 in JAVA
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm sho
2016-04-22 10:19:35 304
原创 【Leetcode】:319. Bulb Switcher 问题 in JAVA
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning of
2016-04-21 10:14:19 391
原创 【Leetcode】:343. Integer Break 问题 in JAVA
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, ret
2016-04-20 21:11:30 1257
原创 【Leetcode】:122. Best Time to Buy and Sell Stock II 问题 in JAVA
Say you have an array for which the ith 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 on
2016-04-19 15:02:49 593
原创 【Leetcode】:238. Product of Array Except Self 问题 in JAVA
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n
2016-04-19 12:24:33 430
原创 【Leetcode】:226. Invert Binary Tree 问题 in JAVA
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howe
2016-04-18 21:48:04 260
原创 【Leetcode】:104. Maximum Depth of Binary Tree 问题 in JAVA
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./** * Definition for a binary tree no
2016-04-18 21:23:47 484
原创 二叉搜索树实现 in Go语言
用Go实现了下二叉搜索树,还是花了不少时间,在实现中使用的是单向链表,这才算是体会到了双向链表在实现中的优势package datastructureimport ( "container/list" "fmt")type BSTree struct { root *Node}type Node struct { left *Node right *Node
2016-04-18 20:37:28 483
原创 【Leetcode】:169. Majority Element 问题 in Go语言
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element
2016-04-17 21:30:40 472
原创 【Leetcode】:171. Excel Sheet Column Number 问题 in Go语言
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ...
2016-04-17 20:28:57 410
原创 算法导论习题:10.3-4 in JAVA
算法导论课后习题10.3-4:我们往往希望双向链表的所有元素在存储器中保持紧凑,例如,在多数组表示中占用前m个下标位置。import java.util.Scanner;public class ans10_3_4 { public static void main(String[] args){ Scanner in=new Scanner(System.in); Sy
2016-04-17 16:41:41 798
原创 算法导论习题:10.2-7 in Go语言
10.2-7题:给出一个时间复杂度为O(n)的非递归过程,实现对一个含n个元素的单链表的逆转。程序的主要思想就是,转变指标的指向,例如原本是1->2->3->4,现在变成1package mainimport ( "fmt")type Node struct { next *Node data int}type List struct {
2016-04-16 18:16:22 766
原创 【Leetcode】:242. Valid Anagram问题 in Go语言
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may ass
2016-04-16 12:23:51 605
原创 【Leetcode】:Move Zeroes问题 in JAVA
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you
2016-04-14 21:19:17 427
原创 【Leetcode】:Single Number II问题 in Go语言
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi
2016-04-13 18:09:51 349
原创 【Leetcode】:Single Number III问题 in Go语言
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given
2016-04-12 21:25:30 498
原创 【Leetcode】:Counting Bits问题 in Go语言
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5
2016-04-11 11:25:29 688
原创 选择算法-GoLang实现
选择算法中利用了快速排序的方法,达到了线性的时间复杂度RandomizedSelect函数返回切片a的下标p到r中,第i小的数,下面是用GO语言的实现主要思路和随机快排类似,不过随机快排需要对分割出的两个子序列进行处理,而选择算法只需要对一个进行处理实现了线性的复杂度package orderimport ( "math/rand" )//RandomizedSel
2016-04-10 21:38:31 871
原创 【Leetcode】:Single Number问题 in java
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using e
2016-04-09 21:56:47 450
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人