algorithms
共 13 篇文章
之字形打印矩阵
对于一个矩阵,请设计一个算法,将元素按“之”字形打印。具体见样例。 给定一个整数矩阵 mat ,以及他的维数 n x m ,请返回一个数组,其中元素依次为打印的数字。 测试样例: [[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 n, int m) { int[] res…
2015-09-10 14:05 · algorithms
“之”字形打印矩阵
【题目】 给定一个矩阵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,61,54,9},{59,9,74,25,91…
2015-09-10 13:33 · algorithms
顺时针旋转矩阵
有一个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; int tC = 0; int dC = mat[0].lengt…
2015-09-10 12:47 · algorithms
顺时针打印矩阵
对于一个矩阵,请设计一个算法从左上角(mat[0][0])开始,顺时针打印矩阵元素。 给定int矩阵 mat ,以及它的维数 n x m ,请返回一个数组,数组中的元素为矩阵元素的顺时针输出。 测试样例: [[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,99}, {26,21,71}…
2015-09-09 18:13 · algorithms
折纸问题
请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。 给定折的次数 n ,请返回从上到下的折痕的数组,若为下折痕则对应元素为"down",若为上折痕则为"up". 测试样例: 1 返回:["down"] import java.util.*; public class FoldPaper { public String[] foldPaper(int n) { ArrayList<Strin…
2015-09-09 17:01 · algorithms
最长公共子串
对于两个字符串,请设计一个时间复杂度为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 { public int findLongest(String A, in…
2015-09-06 19:12 · algorithms
最长公共子序列
对于两个字符串,请设计一个高效算法,求他们的最长公共子序列的长度,这里的最长公共子序列定义为有两个序列U1,U2,U3...Un和V1,V2,V3...Vn,其中Ui<Ui+1,Vi<Vi+1。且A[Ui] == B[Vi]。 给定两个字符串 A 和 B ,同时给定两个串的长度 n 和 m ,请返回最长公共子序列的长度。保证两串长度均小于等于300。 测试样例: "1A2C3D4B56",10,"B1D23CA45B6A",12 返回:6 public class LCS { public int findLCS(String A, int n, String B, int m) {…
2015-09-06 17:02 · 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); } public int getdp(int[] …
2015-09-06 16:26 · 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(TreeNode head) { if (head == null) { return 0; } return sol…
2015-09-03 10:12 · 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 min = arr[0]; double res = arr[0]; double tmp1 = 0; do…
2015-09-03 09:44 · 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 getLessIndex(int[] arr) { if (ar…
2015-09-03 09:26 · algorithms
graph_dfs
package edu.xidian.graph; class MyStack { private final int SIZE = 20; private int[] st; private int top; public MyStack() { st = new int[SIZE]; top = -1; } public void push(int j) { st[++top] = j; } public int pop() { return st[top--]; } public int peek() { return st[top]; } public boolean isEmpty…
2015-07-09 22:22 · algorithms
从一列数中筛除尽可能少的数使得从左往右看,这些数是从小到大再从大到小的
package cn.xidian.edu.LIS; import java.util.Arrays; import java.util.Collections; import java.util.List; public class DoubleEndLIS { public static void main(String[] args) { int array[] = {1,4,5,3,2,1,4,5,6}; System.out.println(solve(array)); } private static int solve(int[] array) { int max = 0; f…
2015-06-10 21:09 · algorithms