![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Java
文章平均质量分 73
Yadoer
这个作者很懒,什么都没留下…
展开
-
ArrayList.add(index, element)失败,都是size的误解
在使用ArrayList时遇到一个很愚蠢的问题,想在大小为10的ArrayList的第5个位置插入10,结果抛异常。代码示例如下ArrayList arr=new ArrayList(10);arr.add(5, 10);异常为Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0原创 2015-01-29 17:48:44 · 5381 阅读 · 1 评论 -
LeetCode-Number of Digit One(编程之美-1的数目)
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the followin原创 2015-08-29 18:05:33 · 2283 阅读 · 1 评论 -
LeetCode-Same Tree & Symmetric Tree
两道题放一块了,一个是判断两棵树是否相同:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have原创 2015-02-03 17:50:48 · 1073 阅读 · 0 评论 -
LeetCode-Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled,原创 2015-08-30 13:23:37 · 1058 阅读 · 0 评论 -
LeetCode-Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2015-08-15 16:11:26 · 1330 阅读 · 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原创 2015-08-14 16:53:10 · 727 阅读 · 0 评论 -
LeetCode-Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.没什么可说的,直接上代码:原创 2015-08-14 21:16:15 · 659 阅读 · 0 评论 -
LeetCode-Maximum/Minimum Depth of Binary Tree
iven a binary tree, find its maximum/minimum depth.The maximum/minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.两个很相似的题,但是解法完全不怎么原创 2015-08-14 15:39:02 · 751 阅读 · 0 评论 -
LeetCode - Populating Next Right Pointers in Each Node II 及其变形题
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2015-08-15 18:10:11 · 1011 阅读 · 0 评论 -
LeetCode-Path Sum II
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree原创 2015-01-30 21:11:27 · 589 阅读 · 0 评论 -
剑指offer-二叉搜索树与双向链表
输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向两步。空间复杂度为O(1).最笨的方法就是中序遍历放在一个数组或链表中,再次遍历串联起来。但是要求空间复杂度为O(1),就没办法这么做了,这时候可以参考这个解法。http://blog.csdn.net/my_jobs/article/details/47666909判断一个树是不是一颗二叉搜索树,可以模仿最后的那个方法。本题跟原创 2015-08-17 15:10:36 · 1292 阅读 · 0 评论 -
LeetCode-Single NumberI II III
I)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 usi原创 2015-08-17 20:30:34 · 925 阅读 · 0 评论 -
LeetCode-Lowest Common Ancestor of a Binary Tree 深度解析
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node原创 2015-08-16 18:48:04 · 1525 阅读 · 0 评论 -
位操作总结
有一类题是位操作,但是感觉在面试中,很少有这种题,毕竟不需要那么细的操作,感觉位操作在汇编和C语言关系比较大,比较偏底层。但是有时候用位操作往往得到意想不到的效果。http://graphics.stanford.edu/~seander/bithacks.html关于常见的为操作可以看这链接,将了好多方法,现将比较常见的整理一下。一、位的基本操作基本操作主要有六种,与、或、异或、原创 2015-08-18 15:41:22 · 1345 阅读 · 0 评论 -
Egg dropping puzzle
问题:有一个100层高的楼,如果一个鸡蛋从n层掉下来或者n层以上,鸡蛋就会碎掉。已知,现在有两个鸡蛋,找到满足条件的n,使在最坏情况下,丢鸡蛋的次数最少。原题链接可以转到wiki:点击打开链接很明显,不管egg1怎么丢,egg2肯定得线性得丢。我们假设egg1每次丢10层,即按10f, 20f, 30f, 40f, 50f, 60f, 70f, 80f, 90f, 100f。则,如原创 2015-08-18 19:40:53 · 1245 阅读 · 0 评论 -
LeetCode-Subsets(子集)
三种不同的方式,生产一个集合的所有子集,回溯法、迭代法。。。原创 2015-02-09 20:22:49 · 1053 阅读 · 0 评论 -
LeetCode-Generate Parentheses & Letter Combinations of a Phone Number
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()原创 2015-08-20 14:20:53 · 1041 阅读 · 0 评论 -
LeetCode-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 node contains only nodes with keys less than the node's key.Th原创 2015-08-15 16:10:26 · 1056 阅读 · 0 评论 -
LeetCode-Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2015-08-14 17:28:44 · 841 阅读 · 0 评论 -
LeetCode-Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empt原创 2015-08-13 20:08:16 · 1021 阅读 · 0 评论 -
String, StringBuffer, StringBuilder比较
字符串操作在编程中的用处是非常大的,而在编程时选择哪个字符串操作呢?我们在这里比较三个,主要区别如下:String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全)插入删除修改的速度比较(在一般情况下,不具有普世性)操作字符串速度: StringBuilder > StringBuffer > StringB原创 2015-03-12 20:53:04 · 976 阅读 · 0 评论 -
linux下安装jdk
第一种,用linux自带的yum工具,比如 yum search jdk。进行搜索jdk软件,之后直接安装,比如java-1.7.0-openjdk.x86_64第二种方式就是自己去官网下载安装。具体如下:1.官网下面所需版本,比如32位,或者64位。2.选择安装位置,比如/usr/jdk/。如果没有,自己可以新建3.将下载的文件解压,比如:tar -zxvf jdk-7u45-l原创 2015-04-16 09:41:29 · 1092 阅读 · 0 评论 -
LeetCode-Word Break II
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"原创 2015-03-13 20:56:46 · 1029 阅读 · 0 评论 -
LeetCode-N-Queens
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.原创 2015-08-22 17:29:00 · 1063 阅读 · 0 评论 -
LeetCode-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] ha原创 2015-08-25 15:59:19 · 1100 阅读 · 0 评论 -
LeetCode-Best Time to Buy and Sell Stock I II III IV
此处的三个题跟Maximum Subarray,可以先看此题I)Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, b原创 2015-08-25 17:46:27 · 1064 阅读 · 0 评论 -
LeetCode-Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2015-08-25 19:57:08 · 1030 阅读 · 0 评论 -
LeetCode-Product of Array Except Self
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原创 2015-07-31 10:18:18 · 861 阅读 · 0 评论 -
LeetCode-Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2015-08-13 15:29:44 · 858 阅读 · 0 评论 -
LeetCode-Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly si原创 2015-08-27 17:05:39 · 931 阅读 · 0 评论 -
LeetCode-Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0原创 2015-08-27 17:45:34 · 840 阅读 · 0 评论 -
LeetCode-Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet原创 2015-08-13 20:57:10 · 671 阅读 · 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], [原创 2015-08-27 14:34:47 · 866 阅读 · 0 评论 -
LeetCode-Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.此题是Majority Element的继续,关于此题可以移步至Majority Elem原创 2015-08-06 11:03:29 · 1217 阅读 · 0 评论 -
LeetCode-Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", ret原创 2015-08-28 17:48:18 · 1025 阅读 · 0 评论 -
LeetCode-Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non原创 2015-08-28 15:21:03 · 867 阅读 · 0 评论 -
LeetCode-Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2015-08-21 11:43:47 · 888 阅读 · 0 评论