- 浏览: 88806 次
最新评论
-
hcx2013:
mircle 写道Given nums = [1, 2, 1, ...
Single Number III -
mircle:
Given nums = [1, 2, 1, 3, 2, 5, ...
Single Number III -
hcx2013:
恩,可以,代码更简洁
空格替换 -
QuarterLifeForJava:
这样可以吗?
public static void main( ...
空格替换 -
hcx2013:
这个2是指多出2位,按你的理解可以这么改:int len = ...
空格替换
文章列表
最长递增子序列
- 博客分类:
- algorithms
对于一个数字序列,请设计一个复杂度为O(nlogn)的算法,返回该序列的最长上升子序列的长度,这里的子序列定义为这样一个序列U1,U2...,其中Ui < Ui+1,且A[Ui] < A[Ui+1]。
给定一个数字序列A及序列的长度n,请返回最长上升子序列的长度。
测试样例:
[2,1,4,3,1,5,6],7
返回:4
import java.util.*;
public class AscentSequence {
public int findLongest(int[] A, int n) {
return getdp( ...
A、B和C。如果C包含且仅包含来自A和B的所有字符,而且在C中属于A的字符之间保持原来在A中的顺序,属于B的字符之间保持原来在B中的顺序,那么称C是A和B的混编。实现一个函数,判断C是否是A和B的混编。
给定三个字符串A,B和C,及他们的长度。请返回一个bool值,代表C是否是A和B的混编。保证三个串的长度均小于等于100。
测试样例:
"ABC",3,"12C",3,"A12BCC",6
返回:true
public class Mixture {
public boolean chkMixture( ...
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],
[6,5,7],
[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11 ...
Pascal's Triangle II
- 博客分类:
- leetcode
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,Return [1,3,3,1].
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<Integer>(rowIndex+1);
for (int i = 0; i <= rowInd ...
Pascal's Triangle
- 博客分类:
- leetcode
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Intege ...
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant extra space.
For example,Given the following binary tree,
1
/ \
...
import java.util.Arrays;
public class HeapSort {
public static void main(String[] args) {
int[] arr = new int[]{12, 4, 76, 14, 100, 1, 3};
/*for (int i = 0; i != arr.length; i++) {
heapInsert(arr, arr[i], i);
}*/
for (int i = (arr.length-1)/2; i >= 0; i--) {
heapify(arr, ...
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next point ...
完全二叉树结点数
- 博客分类:
- algorithms
给定一棵完全二叉树的头节点head,返回这棵树的节点个数。如果完全二叉树的节点数为N,请实现时间复杂度低于O(N)的解法。
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class Solution {
public int nodeNum(TreeNo ...
子数组最大乘积
- 博客分类:
- algorithms
给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积。例如arr=[-2.5,4,0,3,0.5,8,-1],子数组[3,0.5,8]累乘可以获得最大的乘积12,所以返回12。
public class Solution {
public double maxProduct(double[] arr) {
if (arr==null || arr.length==0) {
return 0;
}
double max = arr[0];
double m ...
局部最小值位置
- 博客分类:
- algorithms
定义局部最小的概念。arr长度为1时,arr[0]是局部最小。arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]是局部最小;如果arr[N-1]<arr[N-2],那么arr[N-1]是局部最小;如果0<i<N-1,既有arr[i]<arr[i-1]又有arr[i]<arr[i+1],那么arr[i]是局部最小。 给定无序数组arr,已知arr中任意两个相邻的数都不相等,写一个函数,只需返回arr中任意一个局部最小出现的位置即可。
public class Solution {
public int ...
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 none) of the characters without disturbing the relative positions of the remaining characters. (ie, " ...
Given a binary tree, flatten it to a linked list in-place.
For example,Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
...
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
...
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 and sum = 22,
5
/ \
4 8
/ / \
11 13 4
...