
java
wutingyehe
Just for fun
展开
-
LintCode SortedArrayToBST 把排序数组转换为高度最小的二叉搜索树
LintCode SortedArraytoBST 将有序数组转化为高度最小的二叉搜索树原创 2015-06-18 16:26:05 · 2383 阅读 · 1 评论 -
LintCode Reserve Integer 翻转整数
LintCode Reserve Integer 翻转整数原创 2015-06-18 15:13:45 · 1069 阅读 · 0 评论 -
LintCode Add Binary 二进制求和
LintCode Add Binary 二进制求和原创 2015-06-18 17:27:47 · 2825 阅读 · 0 评论 -
LintCode PlusOne 加一
LintCode PlusOne 加一原创 2015-06-18 15:41:21 · 901 阅读 · 0 评论 -
LintCode 两个链表的和
你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in原创 2015-06-25 16:12:37 · 777 阅读 · 0 评论 -
LintCode 合并两个排序链表
将两个有序(升序)链表合并为一个新的有序(升序)链表。 样例 给出 1->3->8->11->15->null,2->null, 返回 1->2->3->8->11->15->null。Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should b原创 2015-06-25 16:47:45 · 1121 阅读 · 0 评论 -
LintCode Gas Station加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第i个加油站前往第i+1个加油站需要消耗汽油cost[i]。你有一辆油箱容量无限大的汽车,现在要从某一个加油站出发绕环路一周,一开始油箱为空。求可环绕环路一周时出发的加油站的编号,若不存在环绕一周的方案,则返回-1。There are N gas stations along a circular route, where原创 2015-06-24 23:00:09 · 1257 阅读 · 0 评论 -
LintCode Delete Element 删除元素
给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度。 元素的顺序可以改变,并且对新的数组不会有影响。 Given an array and a value, remove all occurrences of that value in place and return the new length. The order of elements can be changed, a原创 2015-06-25 15:38:12 · 1878 阅读 · 0 评论 -
LintCode 链表插入排序
用插入排序对链表排序 样例 Given 1->3->2->0->null, return 0->1->2->3->nullSort a linked list using insertion sort. Example Given 1->3->2->0->null, return 0->1->2->3->null./** * Definition for ListNode. * publ原创 2015-06-25 15:19:08 · 1279 阅读 · 0 评论 -
LintCode Two Strings Are Anagrams 两个字符串是变位词
写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的。 样例 给出 s=”abcd”,t=”dcab”,返回 true。Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s=”abcd”, t=”dcab”, return true.Ch原创 2015-06-25 17:27:37 · 1191 阅读 · 1 评论 -
LintCode Valid Sudoku 判断数独是否合法
请判定一个数独是否有效。该数独可能只填充了部分数字,其中缺少的数字用 . 表示。 样例 下列就是一个合法数独的样例。 Determine whether a Sudoku is valid.The Sudoku board could be partially filled, where empty cells are filled with the character ..原创 2015-06-24 22:21:45 · 1834 阅读 · 0 评论 -
LintCode SingleLeton 单例
单例 是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。你的任务是设计一个 getInstance 方法,对于给定的类,每次调用 getInstance 时,都可得到同一个实例。Singleton is a most widely原创 2015-06-24 22:28:18 · 4242 阅读 · 0 评论 -
LintCode Matrix Zigzag Traversal 矩阵的之字型遍历
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-order. 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历。Example Given a matrix: [ [1, 2, 3, 4], [5原创 2015-06-24 23:59:23 · 5065 阅读 · 0 评论 -
LintCode Flip Bits 将整数A转换为B
如果要将整数A转换为B,需要改变多少个bit位? 样例 如把31转换为14,需要改变2个bit位。 (31)10=(11111)2 (14)10=(01110)2 挑战 你能想出几种方法?Determine the number of bits required to flip if you want to convert integer n to integer m. Example原创 2015-06-25 14:04:12 · 2595 阅读 · 0 评论 -
LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
给定一个链表,删除链表中倒数第n个节点,返回链表的头节点。 Given a linked list, remove the nth node from the end of list and return its head.样例 给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1->2->3->5->null. Example Give原创 2015-06-25 14:30:26 · 851 阅读 · 0 评论 -
LintCode Nth to Last Node in List 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n。 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点1.Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Example Given a List 3->2->原创 2015-06-25 16:24:24 · 861 阅读 · 0 评论 -
LintCode Unique Characters 判断字符串是否没有重复字符
实现一个算法确定字符串中的字符是否均唯一出现样例 给出”abc”,返回 true 给出”aab”,返回 false挑战 如果不使用额外的存储空间,你的算法该如何改变?Implement an algorithm to determine if a string has all unique characters.Example Given “abc”, return true. Given原创 2015-06-26 10:11:24 · 3029 阅读 · 0 评论 -
LintCode Subarray Sum 子数组之和
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置。样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3].Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of t原创 2015-06-26 13:24:06 · 1942 阅读 · 0 评论 -
LintCode Sqrt(x) x的平方根
实现 int sqrt(int x) 函数,计算并返回 x 的平方根。样例 sqrt(3) = 1 sqrt(4) = 2 sqrt(5) = 2 sqrt(10) = 3挑战 O(log(x))Compute and return the square root of x.Example sqrt(3) = 1 sqrt(4) = 2 sqrt(5) = 2 sqrt(10) =原创 2015-06-26 13:11:14 · 4557 阅读 · 0 评论 -
LintCode 二叉树的最小深度
给定一个二叉树,找出其最小深度。 二叉树的最小深度为根节点到最近叶子节点的距离。样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最小深度为 2Given a binary tree, find its minimum depth. The minimum depth is the number of n原创 2015-06-26 10:33:21 · 1125 阅读 · 0 评论 -
LintCode HashFunction 哈希函数
在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数。一个好的哈希函数可以尽可能少地产生冲突。一种广泛使用的哈希函数算法是使用数值33,假设任何字符串都是基于33的一个大整数,比如:hashcode("abcd") = (ascii(a) * 333 + ascii(b) * 332 + ascii(c) *33 + ascii(d)) % HASH_SI原创 2015-06-26 16:41:52 · 3644 阅读 · 1 评论 -
LintCode O(1) Check Power of 2 O(1)检测2的幂次
用 O(1) 时间检测整数 n 是否是 2 的幂次。样例 n=4,返回 true; n=5,返回 false.注意 O(1) 时间复杂度Using O(1) time to check whether an integer n is a power of 2.Example For n=4, return true; For n=5, return false;Challenge O(1原创 2015-06-26 10:58:59 · 1916 阅读 · 0 评论 -
LintCode Majority Number 主元素
给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。 Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it.样例 给出数组[1,1,1,1,2,2,2],返回 1挑战 要求时原创 2015-07-10 17:24:27 · 1463 阅读 · 0 评论 -
LintCode Binary Search 二分查找
给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) ti原创 2015-07-10 17:51:37 · 1312 阅读 · 0 评论 -
LintCode UniquePathsII 不同的路径II
中文描述 跟进“不同的路径”: 现在考虑网格中有障碍物,那样将会有多少条不同的路径? 网格中的障碍和空位置分别用1和0来表示。样例 如下所示在3x3的网格中有一个障碍物: [ [0,0,0], [0,1,0], [0,0,0] ] 一共有2条不同的路径从左上角到右下角。注意 m和n均不超过100public class Solution { /**原创 2015-07-10 14:58:48 · 647 阅读 · 0 评论 -
LintCode UniquePaths 不同的路径
中文描述: 有一个机器人的位于一个M×N个网格左上角(下图中标记为’Start’)。 机器人每一时刻只能向下或者向右移动一步。机器人试图达到网格的右下角(下图中标记为’Finish’)。 问有多少条不同的路径? start 1.2 1.3 1.4 1.5 1.6 1.7 2.1 3.1 3.2 3.3 3.4原创 2015-07-10 14:17:09 · 2206 阅读 · 0 评论 -
LintCode Remove Duplicates from Sorted List 删除链表中的重复元素
给定一个排序链表,删除所有重复的元素每个元素只留下一个。 Given a sorted linked list, delete all duplicates such that each element appear only once.样例 给出1->1->2->null,返回 1->2->null 给出1->1->2->3->3->null,返回 1->2->3->null/** * D原创 2015-07-10 15:53:48 · 820 阅读 · 0 评论 -
LintCode Climbing Stairs 爬楼梯
中文描述: 假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法返回 3English Version: You are climbing a stair case. It takes n steps to reach to the top. Each time you原创 2015-07-11 22:05:58 · 1704 阅读 · 0 评论 -
LintCode Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
中文描述:跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理?样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2,3]。English Version:Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, G原创 2015-07-11 21:57:38 · 615 阅读 · 0 评论 -
LinCode Maximum Depth of Binary Tree 二叉树的最大深度
中文版: 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的距离。样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最大深度为3.English Version: Given a binary tree, find its maximum depth. The maximum depth is the n原创 2015-07-11 21:40:59 · 506 阅读 · 0 评论 -
LinCode Remove Duplicates from Sorted Array 删除排序数组中的重复数字
中文描述:给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。样例 给出数组A =[1,1,2],你的函数应该返回长度2,此时A=[1,2]。English Version:Given a sorted array, remove the duplicates in place such t原创 2015-07-11 21:49:07 · 619 阅读 · 0 评论 -
LintCode Partition List 链表划分
中文版: 给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。 你应该保留两部分内链表节点原有的相对顺序。样例 给定链表 1->4->3->2->5->2->null,并且 x=3 返回 1->2->2->4->3->5->nullEnglish Version Given a linked list and a value x, partition it suc原创 2015-07-11 21:14:10 · 1868 阅读 · 1 评论 -
LintCode Triangle 数字三角形
中文描述: 给定一个数字三角形,找到从顶部到底部的最小路径和。每一步可以移动到下面一行的相邻数字上。样例 比如,给出下列数字三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 从顶到底部的最小路径和为11 ( 2 + 3 + 5 + 1 = 11)。注意 如果你只用额外空间复杂度O(n)的条件下完成可以获得加分,其中n是数字原创 2015-07-11 22:41:07 · 2109 阅读 · 0 评论 -
LinCode Minimum Path Sum 最小路径和
中文版: 给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径。注意 你在同一时间只能向下或者向右移动一步.English Version Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes th原创 2015-07-11 21:36:54 · 829 阅读 · 0 评论 -
LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
中文描述: 给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾)。请在在O(1)时间复杂度删除该链表节点。并在删除该节点后,返回表头。样例 给定 1->2->3->4,和节点 3,返回 1->2->4。English Version: Implement an algorithm to delete a node in the middle of a singly linked lis转载 2015-07-14 09:33:30 · 1391 阅读 · 0 评论 -
LintCode Binary Tree Preorder Traversal二叉树的前序遍历(非递归)
给出一棵二叉树,返回其节点值的前序遍历。 Given a binary tree, return the preorder traversal of its nodes’ values.样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 1)将二叉树节点入栈(入栈之前输出该节点值),直到左孩子为null为止原创 2015-07-14 13:49:47 · 674 阅读 · 0 评论 -
LintCode Binary Tree Inorder Traversal 二叉树的中序遍历(非递归)
给出一棵二叉树,返回其中序遍历。 Given a binary tree, return the inorder traversal of its nodes’ values.样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]./** * Definition of TreeNode: * public clas原创 2015-07-14 14:03:08 · 744 阅读 · 0 评论 -
LintCode Binary Tree Postorder Traversal 二叉树的后序遍历
给出一棵二叉树,返回其节点值的后序遍历。 Given a binary tree, return the postorder traversal of its nodes’ values.样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1]解法: 使用两个栈,第二个栈记录第一个栈对应节点被访问的次数。只有当被访问原创 2015-07-14 14:45:06 · 614 阅读 · 0 评论 -
LintCode Merge Sorted Array 合并排序数组
合并两个排序的整数数组A和B变成一个新的数组。 Merge two given sorted integer array A and B into a new sorted integer array.样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]挑战 你能否优化你的算法,如果其中一个数组很大而另一个数组很小? How can you op原创 2015-07-14 16:00:41 · 1110 阅读 · 0 评论 -
【LintCode】Coins in a Line 硬币排成行
有 n 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。 请判定 第一个玩家 是输还是赢?样例 n = 1, 返回 true. n = 2, 返回 true. n = 3, 返回 false. n = 4, 返回 true. n = 5, 返回 true.挑战 O(1) 时间复杂度且O(1) 存储。相似例子(引自http:原创 2015-07-15 09:52:11 · 1429 阅读 · 0 评论