
算法
李家少年
这个作者很懒,什么都没留下…
展开
-
leet_257. Binary Tree Paths-根到叶子的路径
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree:1 / \ 2 3 \ 5 All root-to-leaf paths are:[“1->2->5”, “1->3”]析:想办法层次遍历,每个结点都保存从根到它的路径,判断是否叶原创 2017-03-11 19:36:30 · 292 阅读 · 0 评论 -
链表应用:奇数位丢弃
对于一个由0..n的所有数按升序组成的序列,我们要进行一些筛选,每次我们取当前所有数字中从小到大的第奇数位个的数,并将其丢弃。重复这一过程直到最后剩下一个数。请求出最后剩下的数字。析:把所有数放在链表中,每次从里面删除就行了。但是注意,应该从后向前删除。因为如果从前开始删除,数据的index就会发生改变,从而删错。import java.util.LinkedList;import java.ut原创 2017-03-22 10:01:55 · 765 阅读 · 0 评论 -
分治案例_leetcode_46. Permutations-全排列
Given a collection of distinct 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], [3,2,1]原创 2017-03-13 10:55:29 · 278 阅读 · 0 评论 -
leetcode_3. Longest Substring Without Repeating Characters-没有重复字符的子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the le原创 2017-03-13 16:27:11 · 259 阅读 · 0 评论 -
根据前序、中序遍历创建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。// 自己的思路:/** * Definition for binary tree * public class TreeNode { *原创 2017-03-22 23:48:03 · 554 阅读 · 0 评论 -
用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node);原创 2017-03-23 10:34:29 · 321 阅读 · 0 评论 -
链表反转
题目描述 输入一个链表,反转链表后,输出链表的所有元素。/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode Revers原创 2017-03-23 14:59:15 · 193 阅读 · 0 评论 -
寻找丑数
题目描述 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。析:后面的丑数肯定是由前面的丑数求出来的// 用i2,i3,i5保存当前乘2 3 5结果最小的数的位置 public int GetUglyNumber_Solution2(int n) {原创 2017-03-30 16:32:05 · 331 阅读 · 0 评论 -
判断B是否是A的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) { if(root2==null) return false; if(root1==null &&原创 2017-03-23 22:19:02 · 442 阅读 · 0 评论 -
和的拆分
有一个和为num,现在想把它拆成多个正数之和,注意比如对于4拆成1+3和3+1实际是一种拆法。也就是每次拆成的List要是非降序的。 求最后拆成的结果:析:想到递归,但是注意,要求升序,对于4,如果拆的前两个是1+2,最后成了1+2+1,并不是升序了。 好的方法是每次从右向左开始拆,拆的数据逐步减小。 保证本次拆的数据<=上次拆的数据import java.util.ArrayList;im原创 2017-04-09 20:56:47 · 323 阅读 · 0 评论 -
二叉树中序遍历的下一个结点
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。析:每次先找当前结点的右子树中序遍历的第一个结点,如果非空就返回,如果为空,就让当前结点逐步向祖宗方向寻找,如果某个祖宗是前一祖宗的左结点,就返回该结点。否则返回null/*public class TreeLinkNode { int val; T原创 2017-03-24 16:02:01 · 467 阅读 · 0 评论 -
判断树是否结构对称
题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。析: 法1:网友方法boolean isSymmetrical(TreeNode pRoot) { if (pRoot == null) return true; return f(pRoot.left,pRoot.righ原创 2017-03-24 17:27:14 · 675 阅读 · 0 评论 -
优先队列PriorityQueue
PriorityQueue是个基于优先级堆的优先级队列。此队列按照在构造时所指定的顺序对元素排序,既可以根据元素的自然顺序来指定排序(参阅 Comparable),也可以根据Comparator 来指定,这取决于使用哪种构造方法。优先级队列不允许 null 元素。例1:按照升序依次取出元素import java.util.PriorityQueue;public class BiJiao {原创 2017-04-12 11:08:25 · 1166 阅读 · 0 评论 -
java创建二叉树及遍历
已知外部提供创建树所需要的数据。方法1:按照从上到下从左到右依次把数据放在对应结点来创建树。 即数组中index处的数据在树中仍然是放在index处,而它的左右子结点分别是 2*index+1 , 2*index+2-- BinaryTree.javapublic class BinaryTree { Node root; int size; // 结点总个数 O原创 2017-03-05 22:20:17 · 673 阅读 · 1 评论 -
leetcode_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 always原创 2017-03-07 14:40:41 · 247 阅读 · 0 评论 -
map排序
按照key排序:TreeMap提供了按key排序。import java.util.ArrayList;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map原创 2017-04-16 17:29:56 · 308 阅读 · 0 评论 -
判断是否是栈的出栈顺序
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)析:借助一个栈 public boolean IsPopOrder(int [] pushA,in原创 2017-03-28 10:17:31 · 653 阅读 · 0 评论 -
leetcode_513.Find Bottom Left Tree Value找树最后一行的最左数
Given a binary tree, find the leftmost value in the last row of the tree.Example 1: Input:2 / \ 1 3Output: 1 Example 2: Input: 1 / \ 2 3 / / \4 5 6 / 7Output: 7析:原创 2017-03-11 15:38:27 · 372 阅读 · 0 评论 -
leetcode_454. 4Sum II-4个数的和
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D have same length o原创 2017-03-11 00:25:42 · 238 阅读 · 0 评论 -
kruskal最小生成树(MST)算法
关键是不构成回路的判断 例子: 代码:#include<iostream>#include<queue>using namespace std;struct EdgeNode{int v1;int v2;int value;bool operator<(const EdgeNode &a) const //此处把边放到优先级队列,由于自己 //重原创 2016-06-14 02:21:47 · 829 阅读 · 0 评论 -
leetcode_500 Keyboard Row
题目描述: 输入多个字符串,条件:字符串是由键盘上的某一行字母组成的。将满足的字符串输出。public class Solution { public String[] findWords(String[] words) { String[] data = new String[]{"qwertyuiop","asdfghjkl","zxcvbnm"};原创 2017-03-02 15:19:31 · 216 阅读 · 0 评论 -
leetcode_496 Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums2.The原创 2017-03-03 21:23:54 · 206 阅读 · 0 评论 -
leetcode_448 Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you原创 2017-03-04 14:58:29 · 238 阅读 · 0 评论 -
leetcode_108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST 给有序数组,创建 平衡二叉搜索树网友解法:/** * Definition for a binary tree node. * public class TreeNode { * int va原创 2017-03-08 11:24:30 · 266 阅读 · 0 评论 -
leetcode_53. Maximum Subarray-子数组最大和
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the原创 2017-03-08 23:42:33 · 302 阅读 · 0 评论 -
leetcode_326. Power of Three-判断是否3的次方
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的次方?法1:网友神奇方法public boolean isPowerOfThree(int n) {原创 2017-03-08 20:00:47 · 594 阅读 · 0 评论 -
leetcode_202. Happy Number-找数规律
找规律题的机械方法!Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the sq原创 2017-03-08 16:37:25 · 397 阅读 · 0 评论 -
leetcode_404. Sum of Left Leaves-左叶子的和
查找二叉树所有左叶子的和。法1:public int sumOfLeftLeaves(TreeNode root) { if(root == null) return 0; int ans = 0; if(root.left != null) { if(root.left.left == null && root.left.right == null) ans原创 2017-03-07 09:52:05 · 262 阅读 · 0 评论 -
leetcode_383. Ransom Note-近似子串问题
典型的近似子串问题!Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magaz原创 2017-03-07 09:01:41 · 289 阅读 · 0 评论 -
leetcode_136 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 using extra me原创 2017-03-04 00:56:43 · 539 阅读 · 0 评论 -
leetcode_437. Path Sum III-二叉树
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it must go原创 2017-03-09 20:49:40 · 256 阅读 · 0 评论 -
leetcode_15. 3Sum-求数组中三个数和为0
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 duplic原创 2017-03-10 14:18:12 · 1902 阅读 · 0 评论 -
leetcode_167 Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers suc原创 2017-03-06 21:52:39 · 262 阅读 · 0 评论 -
B-树、B+树、B*树的区别
见: http://blog.youkuaiyun.com/dazhong159/article/details/7963846转载 2017-07-17 15:25:51 · 223 阅读 · 0 评论