LeetCode
文章平均质量分 72
吉他手J
再细心一点,在努力一点
展开
-
[LeetCode]Maximum Subarray
Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum.原创 2014-09-25 05:38:09 · 622 阅读 · 0 评论 -
[LeetCode] Candy
public int candy(int[] ratings) { int n=ratings.length; int[] c= new int[n]; Arrays.fill(c,1); for(int i=0;i<n-1;i++){ if(ratings[i]>ratings[i+1] && c[i]<=c原创 2014-09-25 23:15:52 · 536 阅读 · 0 评论 -
[LeetCode]Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the to原创 2014-09-25 14:22:24 · 663 阅读 · 0 评论 -
[LeetCode] World Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"原创 2014-09-24 23:42:17 · 845 阅读 · 0 评论 -
[LeetCode] Valid Palindrome
public boolean isPalindrome(String s) { if(s==null) return false; s=s.toLowerCase(); StringBuilder sb= new StringBuilder(); for(int i=0;i<s.length();i++){ c原创 2014-09-26 09:54:03 · 692 阅读 · 0 评论 -
[LeetCode] Palindrome Partitioning I
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","原创 2014-09-26 12:05:06 · 619 阅读 · 0 评论 -
[LeetCode] Palindrome Partitioning II
public int minCut(String s) { int len=s.length(); int[] dp= new int[len]; for(int i=0;i<len;i++){ dp[i]=i; } boolean[][] isPalin=new boolean[len][le原创 2014-09-26 12:08:16 · 520 阅读 · 0 评论 -
[LeetCode] Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2014-09-23 03:59:04 · 601 阅读 · 0 评论 -
[LeetCode] Word Break II
public List wordBreak(String s, Set dict) { int n=s.length(); boolean[] dp=new boolean[n+1]; dp[0]=true; List> words = new ArrayList>(); for (int i = 0; i <= n; i++)原创 2014-09-25 14:29:11 · 583 阅读 · 0 评论 -
[LeetCode] Pow(x,n)
这道题我的写法和别人可能不一样,总体思路是一样的第一步就是判断n的情况,如代码所示我是将指数n转化成二进制,比如说: 3^4545的二进制是101101从左到右扫一遍,跳过第一位。如果位是0,那么就将结果平方,也就是乘自己。如果位是1,那么除了要将结果平方,还要再乘一次x这个的原理是根据指数的性质,如果n=10(二进制),相当于2,如果n左移一位,就是再*2,也原创 2014-10-06 10:53:27 · 892 阅读 · 0 评论 -
[LeetCode]200. Number of Islands岛的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assu原创 2017-03-22 23:17:12 · 858 阅读 · 0 评论 -
[LeetCode] Longest Palindrome Substring 详细分析
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.面DP题的考官都是神经病。。(吐原创 2014-10-01 11:39:56 · 1760 阅读 · 0 评论 -
[LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.这个题说数可能是任意的大的非负数,肯定不是让你直接乘起来返回(我干了。。)而是原创 2014-09-30 05:47:08 · 2035 阅读 · 0 评论 -
[LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2014-09-30 03:04:51 · 1131 阅读 · 0 评论 -
[LeetCode] Flatten Binary Tree to Linked List
大象放入冰箱几步的思路处理这问题就好了1.把左子树弄平, 2. 插入到根节点和根节点的右子树之间,3. 重复1,2思路不难,代码上有点混乱,细节实现是:把root的左子树弄成flatten的 flatten(root.left),把右子树保存起来 temp=root.right,让root.right=root.left, 然后断掉左边,root.left=null.然后一直走到右边的原创 2014-10-07 05:57:06 · 1061 阅读 · 0 评论 -
[LeetCode]Balanced Binary Tree 新理解
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2014-08-09 02:48:55 · 555 阅读 · 0 评论 -
[LeetCode]Maximum Product Subarray 新题152 最大乘积子序列
public int maxProduct(int[] A) { int n=A.length; if (n==0) return 0; int max = 1, min = 1; int res = Integer.MIN_VALUE; for (int i=0;i<n;i+原创 2014-09-25 05:52:16 · 901 阅读 · 2 评论 -
[LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2014-09-29 00:08:52 · 850 阅读 · 0 评论 -
[LeetCode]Sort List 链表排序 + Merge Two Sorted List 合并两个有序链表
Sort a linked list in O(n log n) time using constant space complexity.先不管zen原创 2014-09-22 01:01:04 · 841 阅读 · 0 评论 -
[LeetCode]String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2014-01-28 21:52:44 · 629 阅读 · 0 评论 -
[LeetCode]Next Permutation
mplement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2014-03-05 12:00:45 · 588 阅读 · 0 评论 -
[leetcode]Convert Sorted Array to Balanced Binary Search Tree (BST)
这是面试题中比较基础的,但是又对于数据结构有帮助的题。分析:构建二叉树,那么第一个应该想到的是,根节点就是数组中间的节点,根节点的左孩子是mid-1,右孩子是mid+1那么如何对应到整个数组上呢,第二个想到,应该用递归,递归是比较符合树的逻辑的。那么一种思路是: 根的左节点把左半部分 [0,mid-1] ,调用自己,递归下去右边是[mid+1,end] ,这样左边一层一层的就到原创 2014-02-25 07:51:56 · 640 阅读 · 0 评论 -
[leetcode]Convert Sorted List to Binary Search Tree
o(nlogn)public class Solution { public TreeNode sortedListToBST(ListNode head) { // Start typing your Java solution below // DO NOT write main() function if(head == n原创 2014-02-25 04:44:11 · 664 阅读 · 0 评论 -
[LeetCode]Roman to Integer
题目是把罗马数字转成整形返回,其实对罗马数字只知道I 到X, 特意去维基百科查了下。发现了如下几条规则:羅馬數字共有7個,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的規則可以表示任意正整數。需要注意的是罗马数字中没有“0”,與進位制無關。一般認為羅馬數字只用來記數,而不作演算。重複數次:一個羅馬數字重複幾次,就表示這個原创 2014-01-26 14:40:35 · 699 阅读 · 0 评论 -
[LeetCode]Single Number
题目: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 us原创 2014-01-26 12:24:15 · 560 阅读 · 0 评论 -
[LeetCode]Merge Sorted Array
题目:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initia转载 2014-01-25 11:58:29 · 516 阅读 · 0 评论 -
面试题:[Container the most water]容器中最大水容量
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2014-01-21 05:21:42 · 902 阅读 · 0 评论 -
[LeetCode]Convert Sorted Array to Binary Search Tree 将有序数组转换成BST
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.public class Solution { public TreeNode sortedArrayToBST(int[] num) { if(num.length原创 2014-08-09 03:04:59 · 653 阅读 · 0 评论 -
[LeetCode]Reverse Words in a String 新题151
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".这个题不难,结果我做的时候出现了TIME LIMIT EXCEED之前是用栈做的,把每个单词push进去,pop出来,就是倒序了么,感觉原创 2014-03-08 13:50:43 · 1152 阅读 · 0 评论 -
[LeetCode]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], [原创 2014-03-10 13:02:38 · 658 阅读 · 0 评论 -
[leetcode]Reverse Nodes in k-Group 反转以k个节点为一组的链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2014-08-04 07:22:12 · 878 阅读 · 0 评论 -
[LeetCode]Clone Graph 克隆图
lone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for eac原创 2014-08-04 23:56:49 · 1789 阅读 · 1 评论 -
[LeetCode]Merge K sorted List合并K个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.原创 2014-08-08 22:28:09 · 892 阅读 · 0 评论 -
[LeetCode]Valid Number有效数字
public boolean isNumber(String s) { if (s == null) return false; int start = 0, end = s.length() - 1; while (start <= end && s.charAt(start) == ' ') { start++; } while (end >= sta原创 2014-07-25 09:23:09 · 1293 阅读 · 0 评论 -
[LeetCode]Permutation I排列
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].高中数学就学过了排列(Pe原创 2014-03-27 04:53:09 · 637 阅读 · 0 评论 -
[leetcode]Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2014-03-23 02:19:44 · 647 阅读 · 0 评论 -
[leetcode]Combination组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2014-03-12 09:45:23 · 538 阅读 · 0 评论 -
[leetcode]Minimum Path Sum 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2014-03-12 10:11:26 · 526 阅读 · 0 评论 -
[LeetCode]Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters.Example 1:Input:"tree"Output:"eert"Explanation:'e' appears twice while 'r' and 't' both appear once.So 'e原创 2017-03-23 03:00:17 · 356 阅读 · 0 评论