LeetCode
文章平均质量分 81
LeaderTech_NJ
做中国最专业的防雷云服务商
展开
-
CC150 4.4 Check Balanced; Leetcode 110. Balanced Binary Tree
Leetcode link: https://leetcode.com/problems/balanced-binary-tree/Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined asa bin原创 2016-10-09 09:38:55 · 532 阅读 · 0 评论 -
Leetcode 15 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain原创 2016-10-12 05:45:18 · 297 阅读 · 0 评论 -
Leetcode 21. Merge Two Sorted Lists
原题链接:https://leetcode.com/problems/merge-two-sorted-lists/思路:0 处理边界条件1 两个输入链表可能为空,采用dummy节点,使得dummy.next指向新链表的第一个节点。2 初始化时,使用两个指针指向两个链表的表头,两个指针同时不为空时,取较小的节点放入新链表的尾部。3 然后把链表原创 2016-10-13 10:16:05 · 255 阅读 · 0 评论 -
Leetcode Longest Substring Without Repeating Characters
原题:https://leetcode.com/problems/longest-substring-without-repeating-characters/Given a string, find the length of the longest substring without repeating characters.Examples:Given "abca原创 2016-10-13 23:59:26 · 259 阅读 · 0 评论 -
Leetcode 155 Min Stack
链接:https://leetcode.com/problems/min-stack/题目要求:Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stac原创 2016-10-14 10:39:38 · 339 阅读 · 0 评论 -
Leetcode 102 Binary Tree Level Order Traversal
题目:https://leetcode.com/problems/binary-tree-level-order-traversal/九章算法答案:http://www.jiuzhang.com/solutions/binary-tree-level-order-traversal/思路:Queue使用时要尽量避免Collection的add()和remove()方原创 2016-10-15 07:48:28 · 259 阅读 · 0 评论 -
CC150 2.8&&Linked List Cycle dection 判断循环LinkedList
https://leetcode.com/problems/linked-list-cycle/《进军硅谷》类似题目:环的长度题目:给出一个单向链表头指针,如果有环,判断环的长度,否则返回0;思路:1 判断链表是否有环。 使用快慢指针,如果快指针已经到尾部,而且他们没有相遇,则无环;如果他们相遇,则有环2 上图为例:慢指针:9->8->7->1;原创 2016-09-30 13:39:22 · 391 阅读 · 0 评论 -
204. Count Primes
题目:https://leetcode.com/problems/count-primes/题目要求:输出比一个非负数字N小的prime的个数;质数,百度百科:http://baike.baidu.com/view/10626.htm素数: 是只有两个正因子(1和自己)的自然数;最小的素数是2;合数: 比1大但不是素数的数称之为合数;0和1不是素数也不是合数;IsPrime原创 2016-10-17 06:18:48 · 226 阅读 · 0 评论 -
Leetcode 160. Intersection of Two Linked Lists/CC150 2.7
https://leetcode.com/problems/intersection-of-two-linked-lists/Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two lin原创 2016-10-04 03:34:14 · 332 阅读 · 0 评论 -
CC150 2.6/Leetcode Palindrome LinkedList
判断 Linked List是否属于回文思路:迭代法+快慢指针快指针是慢指针速度的两倍;1 while循环,fast到tail,slow只到一半;2 slow内容均迭代放入stack3 如果linkedlist元素个数是奇数(fast!=null), slow需要前移1 位4 然后对比stack 的顶元素与slow指针指向的内容; 不等-> 返回false; 相等原创 2016-10-03 22:45:25 · 368 阅读 · 0 评论 -
242. Valid Anagram
原题链接:https://leetcode.com/problems/valid-anagram/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原创 2016-10-19 09:56:00 · 276 阅读 · 0 评论 -
206. Reverse Linked List
原题:https://leetcode.com/problems/reverse-linked-list/内容:Reverse a singly linked list.A linked list can be reversed either iteratively or recursively. Could you implement both?总思路图:原创 2016-10-19 04:46:46 · 322 阅读 · 0 评论 -
算法-两根指针类问题
1 有序数组去重给出有序数组,就地移除重复元素,保证无重复,并返回新数组长度;举例:已知:A=[1,1,2,3,3] 输出长度3, A=[1,2,3]思路图: public static int RemoveDuplicate(int[] arr) { if (arr == null || arr.Length ==原创 2017-01-16 07:06:29 · 672 阅读 · 0 评论 -
169. Majority Element
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-02-11 22:52:51 · 358 阅读 · 0 评论 -
326. Power of Three
Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?问题:判断是否是3的幂?思路:递归java代码:public class Solution原创 2016-02-12 07:06:32 · 298 阅读 · 0 评论 -
299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint t原创 2016-02-13 13:25:36 · 345 阅读 · 0 评论 -
CC150 4.6 Successor
Successor: write an algorithm to find the "next" node(i.e. in-order successor) of a given node in a binary search tree. you may assume that each node has a link to its parent已知BST中的一个node, 写算法找到下一个节原创 2016-10-10 01:38:46 · 277 阅读 · 0 评论 -
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/题目大意:已知一个BST,找到BST中的节点p+q的最小公共祖先;思路:递归1 boundary conditions: tree, p, q任意为null, 返回null2 如果p.va, q.val均小于root.val ->原创 2016-10-10 03:24:30 · 249 阅读 · 0 评论 -
Leetcode 236. Lowest Common Ancestor of a Binary Tree
原题:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/题目大意:已知一个BT,找到BST中的节点p+q的最小公共祖先;例子: _______3______ / \ ___5__ ___1__ /原创 2016-10-10 04:02:54 · 311 阅读 · 0 评论 -
Leetcode 98. Validate Binary Search Tree
https://leetcode.com/problems/validate-binary-search-tree/Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a原创 2016-10-09 11:37:02 · 292 阅读 · 0 评论 -
Leetcode2 Add Two Numbers
原题链接:https://leetcode.com/problems/add-two-numbers/给出两个整数,每个整数由一个链表表示。其中链表每个节点保存数的数位,这些数位反序存在链表中,即高位在最后,低位在最前面。返回一个链表代表这两个数之和。举例,如图:思路:由于反序存储在链表中,可以从表头开始顺序相加;两个链表长度不同,如何相加链表而没有剩余原创 2016-10-10 23:47:29 · 238 阅读 · 0 评论 -
LeetCode - 5 Longest Palindromic 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.Subscribe to see w原创 2016-09-21 23:03:48 · 334 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree LeetCode
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.思路:递归java语言/** * De原创 2016-02-09 13:49:01 · 229 阅读 · 0 评论 -
Leetcode 8. 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原创 2016-09-23 12:16:13 · 346 阅读 · 0 评论 -
Leetcode 20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid bu原创 2016-10-12 06:28:12 · 216 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st原创 2016-10-23 00:33:08 · 320 阅读 · 0 评论 -
Leetcode 合并有序数组 Merge 2 arrays in 1 array
《进军硅谷》原题:合并有序数组给定两个有序数组A和B, 合并B到A, 结果保持有序。假设A有足够空间容纳B。初始时,A、B元素各有m和n个;分析:数组插入新元素没有链表那么方便,因为插入一个新元素会导致插入后的所有元素的移动,为了避免移动,通常从后向前插入,即反向插入;思路:1 找到原有数组A和B的最后一个元素;2 逐个把较大的值放在A数组的m+n-1位置向原创 2016-09-26 12:11:40 · 365 阅读 · 0 评论 -
leetcode 1 Two Sum 两数之和
原题链接:https://leetcode.com/problems/two-sum/已知条件:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input原创 2016-09-25 06:17:47 · 423 阅读 · 0 评论 -
算法--全排列、全子集、DFS\BFS问题
排列组合子集合 subset(medium)输入一个含有不同数字的序列,输出所有可能的集合(含空集)。要求1 集合里元素有序排列;2 输出结果不含有重复集合;举例:输入{1,2,3], 输出{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}原题:https://leetcode.com/problems/subsets/思路图:Amor原创 2017-01-18 11:18:53 · 2666 阅读 · 1 评论