`
hcx2013
  • 浏览: 88762 次
社区版块
存档分类
最新评论
文章列表

Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X   After running your function, the board should be: X X X X X X X X X X X X X O X ...
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 total sum of all root-to-leaf numbers. For example, 1 / \ 2 3   The root-to-leaf path ...
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in O(n) complexity.   public class Solution { ...
对于一个矩阵,请设计一个算法,将元素按“之”字形打印。具体见样例。 给定一个整数矩阵mat,以及他的维数nxm,请返回一个数组,其中元素依次为打印的数字。 测试样例: [[1,2,3],[4,5,6],[7,8,9],[10,11,12]],4,3 返回:[1,2,3,6,5,4,7,8,9,12,11,10] import java.util.*; public class Printer { private static int index = 0; public int[] printMatrix(int[][] mat, int ...
【题目】 给定一个矩阵matrix,按照“之”字形的方式打印这个矩阵,例如: 1 2 3 4 5 6 7 8 9 10 11 12 “之”字形打印的结果为:1,2,5,9,6,3,4,7,10,11,8,12 【要求】 额外空间复杂度为O(1)。     package client; import java.util.*; public class Printer { private static int index = 0; public static void main(String[] args) { int[][] mat = {{85,40 ...
有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。 给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵,保证N小于等于300。 测试样例: [[1,2,3],[4,5,6],[7,8,9]],3 返回:[[7,4,1],[8,5,2],[9,6,3]] import java.util.*; public class Rotate { public int[][] rotateMatrix(int[][] mat, int n) { int tR = 0; int dR = mat.length-1; ...
对于一个矩阵,请设计一个算法从左上角(mat[0][0])开始,顺时针打印矩阵元素。 给定int矩阵mat,以及它的维数nxm,请返回一个数组,数组中的元素为矩阵元素的顺时针输出。 测试样例: [[1,2],[3,4]],2,2 返回:[1,2,3,4] import java.util.*; public class Printer { private static int index = 0; /* public static void main(String[] args) { int[][] max = {{4,46,89}, {28,66 ...

折纸问题

请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。 给定折的次数n,请返回从上到下的折痕的数组,若为下折痕则对应元素为"down",若为上折痕则为"up". 测试样例: 1 返回:["down"] import java.util.*; public class FoldPaper { publ ...

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that the string might be empty? This is a good que ...
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / \ 2 3   Return 6.   /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode ...
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions.   public class Solution { public int maxProfit(int[] prices) { int res = 0; if (prices==null || pr ...
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transaction ...
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, buy one and sell one share of the stock), design an algorithm to find the maximum profit.     public class Solution { public int maxPro ...

最长公共子串

对于两个字符串,请设计一个时间复杂度为O(m*n)的算法(这里的m和n为两串的长度),求出两串的最长公共子串的长度。这里的最长公共子串的定义为两个序列U1,U2,..Un和V1,V2,...Vn,其中Ui + 1 == Ui+1,Vi + 1 == Vi+1,同时Ui == Vi。 给定两个字符串A和B,同时给定两串的长度n和m。 测试样例: "1AB2345CD",9,"12345EF",7 返回:4 import java.util.*; public class LongestSubstring { publi ...
对于两个字符串,请设计一个高效算法,求他们的最长公共子序列的长度,这里的最长公共子序列定义为有两个序列U1,U2,U3...Un和V1,V2,V3...Vn,其中Ui&ltUi+1,Vi&ltVi+1。且A[Ui] == B[Vi]。 给定两个字符串A和B,同时给定两个串的长度n和m,请返回最长公共子序列的长度。保证两串长度均小于等于300。 测试样例: "1A2C3D4B56",10,"B1D23CA45B6A",12 返回:6 public class LCS { public int findLCS( ...
Global site tag (gtag.js) - Google Analytics