二维数组中的查找

题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

public class Solution {
public boolean Find(int target, int [][] array) {
int row = array.length;
int col = array[0].length;
int j = 0;
while(row > 0 && j < col){
int b = array[row-1][j];
if(target > b){
j++;
}else if(target < b){
row--;
}else{
return true;
}
}
return false; }
}

替换空格

题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
public String replaceSpace(StringBuffer str) {
return str.toString().replaceAll(" ","%20");
}
}

从尾到头打印链表

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<>();
if(listNode == null){
return list;
}
Stack<Integer> stack = new Stack<>(); while(listNode != null){
//栈用push(),队列用add()
stack.push(listNode.val);
listNode = listNode.next;
}
while(!stack.empty()){
list.add(stack.pop());
}
return list;
}
}

重建二叉树

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length == 0 || in.length == 0){
return null;
}
TreeNode node = new TreeNode(pre[0]); for(int i = 0; i < pre.length; i++){
if(pre[0] == in[i]){
node.left = this.reConstructBinaryTree(Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(in,0,i)); // node.rigth = this.reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length));
node.right = this.reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length)); }
}
return node;
}
}

用两个栈实现队列

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

import java.util.Stack;

public class Solution {

    //中间栈,存储数据
Stack<Integer> stack1 = new Stack<Integer>();
//入栈和出栈操作
Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node){
while(!stack2.empty()){
stack1.push(stack2.pop());
}
stack2.push(node);
} public int pop(){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
return stack2.pop();
} }

旋转数组的最小数字

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

 import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if (array.length == 0){
return 0;
}
int left = 0;
int right = array.length - 1;
int middle = 0;
while (array[left]>=array[right]) {
//数组长度等于2时
if(right-left==1){
middle = right;
break;
}
middle = left + (right - left) / 2;
if (array[middle] >= array[left]) {
left = middle;
}
if (array[middle] <= array[right]) {
right = middle;
}
}
return array[middle];
}
}

斐波那契数列

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。

n<=39

public class Solution {
public int Fibonacci(int n) {
if(n <= 0){
return 0;
}else if(n == 1){
return 1;
}else if(n == 2){
return 1;
}else{
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
}

跳台阶

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

public class Solution {
public int JumpFloor(int target) {
if(target <= 0){
return 0;
}else if(target == 1){
return 1;
}else if(target == 2){
return 2;
}else{
return JumpFloor(target-1)+JumpFloor(target-2);
}
}
}

变态跳台阶

一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

public class Solution {
public int JumpFloorII(int target) {
if(target <= 2){
return target;
}else{
return 2*JumpFloorII(target-1);
}
}
}

矩形覆盖

我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

 public class Solution {
public int RectCover(int target) {
if(target <= 3){
return target;
}else{
return RectCover(target -1) + RectCover(target -2);
}
}
}

二进制中1的个数

输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

public class Solution {
public int NumberOf1(int n) {
String str = Integer.toBinaryString(n); int count = 0;
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == '1'){
count++;
}
}
return count;
}
}

链表中倒数第k个结点

输入一个链表,输出该链表中倒数第k个结点。

 /*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
import java.util.*;
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null){
return head;
}
Stack<ListNode> stack = new Stack<>();
while(head != null){
stack.push(head);
head = head.next;
}
int count = 0;
ListNode node = null;
while(!stack.empty()){
count++;
if(count == k){
//找到第k个节点,出栈
node = stack.pop();
}else{
//没找到也出栈
stack.pop();
}
}
//当k大于链表长度时,node为null,不用额外判断
return node;
}
}

反转链表

输入一个链表,反转链表后,输出新链表的表头。

 /*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode node1 = null;
ListNode node2 = null;
while(head != null){
node1 = head.next;
head.next = node2;
node2 = head;
head = node1;
}
return node2; }
}

合并两个有序的链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

 /*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
if(list1.val < list2.val){
list1.next = Merge(list1.next,list2);
return list1;
}else{
list2.next = Merge(list1,list2.next);
return list2;
}
}
}

二叉树的镜像

操作给定的二叉树,将其变换为源二叉树的镜像。

 /**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
public void Mirror(TreeNode root) {
TreeNode temp = null; if(root != null){
temp = root.left;
root.left = root.right;
root.right = temp; if(root.left != null){
this.Mirror(root.left);
}
if(root.right != null){
this.Mirror(root.right);
}
} }
}

包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

 import java.util.Stack;

 public class Solution {
Stack<Integer> dataStack = new Stack<>();
Stack<Integer> minStack = new Stack<>(); public void push(int node) {
dataStack.push(node);
if(minStack.empty() || node < minStack.peek()){
minStack.push(node);
}else{
minStack.push(minStack.peek());
}
} public void pop() {
dataStack.pop();
minStack.pop();
} public int top() {
return dataStack.peek();
} public int min() {
return minStack.peek();
}
}

栈的压入、弹出顺序

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

 import java.util.*;

 public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length == 0 || popA.length == 0){
return false;
} Stack<Integer> stack = new Stack<>(); int index = 0;
for(int i = 0; i < pushA.length; i++){
stack.push(pushA[i]);
//for循环到第五遍时,while会一直循环判断栈顶元素是否等于出栈数组
while(!stack.empty() && stack.peek() == popA[index]){
//出栈
stack.pop();
//出栈标记向后移一位
index++;
}
}
//如果为空,则返回true
return stack.empty();
}
}

从上往下打印二叉树

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

 import java.util.*;;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
//层次打印二叉树,使用队列,统计字符个数也使用队列
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root); while(queue.size() != 0){
TreeNode node = queue.poll();
list.add(node.val);
if(node.left != null){
queue.add(node.left);
}
if(node.right != null){
queue.add(node.right);
}
}
return list;
}
}

二叉搜索树的后序遍历序列

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。

 public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length == 0) return false;
return IsTreeBST(sequence, 0, sequence.length-1);
}
public boolean IsTreeBST(int [] sequence,int start,int end ){
if(end <= start) return true;
int i = start;
for (; i < end; i++) {
if(sequence[i] > sequence[end]) break;
}
for (int j = i; j < end; j++) {
if(sequence[j] < sequence[end]) return false;
}
return IsTreeBST(sequence, start, i-1) && IsTreeBST(sequence, i, end-1);
}
}

数组中出现次数超过一半的数字

数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

 import java.util.*;
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
if(array.length == 1){
return array[0];
}
int count = 1,max = 0;
int [] a = new int [1];
for(int i = 0; i < array.length; i++){
for(int j = i+1; j < array.length; j++){
if(array[i] == array[j]){
count++;
}
}
if(count > max){
max = count;
a[0] = array[i];
}
count = 1;
} if(max > array.length/2){
return a[0];
}else{
return 0;
}
}
}

最小的K个数

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

 import java.util.*;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<Integer> list = new ArrayList<>();
if(k > input.length){
return list;
}
Arrays.sort(input);
for(int i = 0; i < k; i++){
list.add(input[i]);
}
return list;
}
}

连续子数组的最大和

HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)

 public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
if(array.length == 0){
return 0;
}
int sum = 0;
//不能等于0,存在负数
int max = array[0];
for(int i=0; i < array.length; i++){
if(sum >= 0){
sum = sum+array[i];
}else{
//抛弃,重新计算
sum = array[i];
}
if(sum > max){
max = sum;
}
}
return max;
}
}

把数组排成最小的数

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

 import java.util.ArrayList;

 public class Solution {
public String PrintMinNumber(int [] numbers) {
String str = "";
for(int i = 0; i < numbers.length; i++){
for(int j = i+1; j < numbers.length; j++){
int a = Integer.parseInt(numbers[i]+""+numbers[j]);
int b = Integer.parseInt(numbers[j]+""+numbers[i]);
if(a > b){
int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
}
}
for(int i = 0; i < numbers.length; i++){
str = str + numbers[i];
}
return str;
}
}

第一个只出现一次的字符

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).

 import java.util.*;
public class Solution {
public int FirstNotRepeatingChar(String str) {
LinkedHashMap<Character,Integer> map = new LinkedHashMap<>(); for(int i = 0; i < str.length(); i++){
if(!map.containsKey(str.charAt(i))){
map.put(str.charAt(i),1);
}else{
int t = map.get(str.charAt(i));
map.put(str.charAt(i),++t);
}
} //返回字符串的位置
for(int i = 0; i < str.length(); i++){
if(map.get(str.charAt(i)) == 1){
return i;
}
}
return -1; }
}

数字在排序数组中出现的次数

统计一个数字在排序数组中出现的次数。

 public class Solution {
public int GetNumberOfK(int [] array , int k) {
int sum = 0;
for(int i = 0; i < array.length; i++){
if(array[i] == k){
sum++;
}
}
return sum;
}
}

二叉树的深度

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

 /**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
import java.util.Queue;
import java.util.LinkedList;
public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null){
return 0;
}
int count = 0, depth = 0, nextCount = 1; Queue<TreeNode> queue = new LinkedList<TreeNode>(); //将根节点添加到队列中
queue.add(root); //第一次循环时队列的长度为1
while(queue.size()!=0){
count++;
//先进先出,取出队列的第一个元素
TreeNode top = queue.poll();
//如果根节点的左子树不为空,则将左子树的根节点添加到队列中
if(top.left!=null){
queue.add(top.left);
}
//如果根节点的右子树不为空,则将右子树的根节点添加到队列中
if(top.right!=null){
queue.add(top.right);
}
//当同一层的节点全部添加到队列中时,count与nextCount相等,deph+1
if(count==nextCount){
nextCount = queue.size();
depth++;
count=0;
}
}
return depth; }
}

平衡二叉树

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

 public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) {
return true;
}
return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 &&
IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
} private int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}

数组中只出现一次的数字

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

 //num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
import java.util.*;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
LinkedHashMap<Integer,Integer> map = new LinkedHashMap<>();
for (int i = 0; i < array.length; i++) {
if(map.containsKey(array[i])){
int t = map.get(array[i]);
map.put(array[i],++t);
}else {
map.put(array[i],1);
}
}
int [] a = new int[2];
int i = 0;
for(int b : map.keySet()){
if(map.get(b) == 1){
a[i] = b;
i++;
}
}
num1[0] = a[0];
num2[0] = a[1];
}
}

左旋转字符串

汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!

 import java.util.*;
public class Solution {
public String LeftRotateString(String str,int n) {
if(str.length() == 0 || n == 0){
return str;
}
n = n % str.length();
String s1 = str.substring(0,n);
String s2 = str.substring(n,str.length());
return s2+s1;
}
}

翻转单词顺序列

牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?

 import java.util.*;
public class Solution {
//单词翻转使用字符串数组实现
public String ReverseSentence(String str) {
//str.trim(),去掉字符串首尾的空格
if(str.trim().equals("")){
return str;
}
String [] s = str.split(" ");
StringBuffer bf = new StringBuffer();
for(int i = s.length; i > 0; i--){
bf.append(s[i-1]);
if(i > 1){
bf.append(" ");
}
}
return bf.toString();
}
}

孩子们的游戏(圆圈中最后剩下的数)

每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)
 
如果没有小朋友,请返回-1
 public class Solution {
public int LastRemaining_Solution(int n, int m) {
if(n == 0){
return -1;
}
if(n == 1){
return 0;
}
return (LastRemaining_Solution(n-1,m)+m)%n;
}
}

求1+2+3+...+n

求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

 public class Solution {
public int Sum_Solution(int n) {
int sum = n;
boolean a = (n>0) && (sum = sum + Sum_Solution(n-1))>0;
return sum;
}
}

把字符串转换成整数

将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0

 public class Solution {
public int StrToInt(String str) {
if(str == null || str.equals("")||str.equals("-2147483649")){
return 0;
}
int count = 0,flag = 0;
for(int i = 0; i < str.length();i++){
char c = str.charAt(i);
if(i == 0){
if((c == '+' || c == '-')&& str.length() <= 1){
return 0;
}
if(c == '+' || c == '-'){
count = 0;
flag = 1;
}else{
if( c > 47 && c < 58 ){
count++;
}
}
}else{
if( c > 47 && c < 58 ){
count++;
}
} }
if(flag == 1 && (count+1) == str.length()){
long a = Long.parseLong(str);
if(a > Integer.MAX_VALUE){
return 0;
}else{
return (int)a;
} }else if(flag == 0 && count == str.length()) {
long a = Long.parseLong(str);
if(a > Integer.MAX_VALUE){
return 0;
}else{
return (int)a;
}
}else{
return 0;
}
}
}

表示数值的字符串

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

 public class Solution {
public boolean isNumeric(char[] str) {
try{
double a = Double.parseDouble(new String(str));
}catch(Exception e){
return false;
}
return true;
}
}

删除链表中重复的结点

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

 /*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if(pHead == null || pHead.next == null){
return pHead;
} //如果当前节点是重复节点
if(pHead.val == pHead.next.val){
ListNode node = pHead.next;
while(node != null && node.val == pHead.val){ //跳过一样的节点
node = node.next;
}
return deleteDuplication(node);
}else{
pHead.next = deleteDuplication(pHead.next);
return pHead;
}
}
}

二叉搜索树的第k个结点

给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8)    中,按结点数值大小顺序第三小结点的值为4。

 /*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
//思路:二叉搜索树按照中序遍历的顺序打印出来正好就是排序好的顺序。
// 所以,按照中序遍历顺序找到第k个结点就是结果。
public class Solution {
int count = 0;
TreeNode node = null; TreeNode KthNode(TreeNode pRoot, int k)
{
if(pRoot == null){
return pRoot;
}
KthNode(pRoot.left,k);
count++;
if(count == k){
node = pRoot;
}
KthNode(pRoot.right,k);
return node;
} }

滑动窗口的最大值

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

 import java.util.*;
public class Solution {
public ArrayList<Integer> maxInWindows(int [] num, int size)
{
int k = 0;
ArrayList<Integer> res = new ArrayList<>();
if (num == null||size <= 0 || num.length < size) {
return res;
}
for(int i = 0; i < num.length-size+1; i++,k++){
ArrayList<Integer> list = new ArrayList<>();
for(int j = 0; j < size; j++){
list.add(num[i]);
i++;
}
i = k;
Collections.sort(list);
res.add(list.get(list.size()-1));
}
return res;
}
}

最新文章

  1. ABP入门系列(2)——通过模板创建MAP版本项目
  2. Keychain group access
  3. POJ 2185 Milking Grid KMP(矩阵循环节)
  4. 使用PetaPoco ORM 框架分页查询
  5. hdu 1576 A/B (扩展欧几里德简单运用)
  6. 重学C语言 -- printf,scanf
  7. range() 函数创建并返回一个包含指定范围的元素的数组
  8. 安装Visual Studio 2013 中文社区版
  9. 《how to design programs》第10章表的进一步处理
  10. 一道java面试题-方法静态分派
  11. 极化码之tal-vardy算法(2)
  12. WordPress彩色背景标签云实现
  13. k8s Kubernetes v1.10
  14. CSS Sprites ——雪碧图的使用方法
  15. H5 58-网页的布局方式
  16. ceph 重启,停止,开始
  17. pictureBox绑定Base64字符串
  18. JSOUP爬虫示例
  19. cJSON 使用详解
  20. IIS发布以后,handle文件找不到,404错误

热门文章

  1. 「LuoguP3979」遥远的国度
  2. app开屏广告
  3. 102、Java中String类之相等判断(忽略大小写)
  4. arm linux 移植 gdb/gdbserver
  5. 运行自己的 DaemonSet【转】
  6. POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希
  7. ReadAsm2
  8. stm32串口收发导致的死机
  9. 二进制枚举之被RE完虐的我的一天
  10. 【SQL必知必会笔记(3)】SELECT语句的WHERE子句数据过滤操作