# 1、打印转圈数组

``````public class RotatePrint {

public static void printOneCircle(int[][] arr,int start,int rows,int cols){
//上面一行
for(int i=start;i<cols-start;i++)
System.out.println(arr[start][i]);
//右边一列,至少有两行
if(rows > (start * 2 + 1)){
for(int i=start+1;i<rows-start;i++)
System.out.println(arr[i][cols-start-1]);
}
//下方一行，至少两行两列
if(rows > (start * 2 + 1) && cols > (start * 2 + 1)){
for(int i=cols-start-2;i>=start;i--)
System.out.println(arr[rows-start-1][I]);
}
//左边一列，至少三行两列
if(rows > (start * 2 + 2) && cols > (start * 2 + 1)){
for(int i = rows-start-2;i>=start+1;i--)
System.out.println(arr[i][start]);
}
}

public static void printRotateMatrix(int[][] arr){
int rows = arr.length;
int cols = arr[0].length;
for(int i = 0;i<(rows + 1) / 2;i++){
printOneCircle(arr,i,rows,cols);
}
}

public static void main(String[] args){
int[][] arr = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};

printRotateMatrix(arr);
}
}
``````

# 2、将正方形矩阵顺时针转动90度

``````public class RotateArray90 {

public static void rotateArrayBy90(int[][] arr){
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr[i].length;j++){
int tmp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = tmp;
}
}
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length / 2;j++){
int tmp = arr[i][j];
arr[i][j] = arr[i][arr[0].length-j-1];
arr[i][arr[0].length-j-1] = tmp;
}
}
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
System.out.print(arr[i][j] + " ");
}
System.out.println(" ");
}
}
public static void main(String[] args){
int[][] arr = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
rotateArrayBy90(arr);
}
}
``````

# 3、找到无序数组中最小的K个数

``````public class HeapSort {

public static void perceDown(int[] arr,int i,int k){
int tmp = arr[i];
int child = i * 2 + 1;
while(child < k){
if(child + 1 < k && arr[child+1] > arr[child])
child = child + 1;
if(arr[child] > tmp){
arr[i] = arr[child];
i = child;
}
else
break;
child = i * 2 + 1;
}
arr[i] = tmp;
}

public static void heapSort(int[] arr,int k){
for(int i=(k-1)/2;i>=0;i--){
perceDown(arr,i,k-1);
}
for(int i=k;i<arr.length;i++){
if(arr[i] < arr[0]){
int tmp = arr[i];
arr[i] = arr[0];
arr[0] = tmp;
perceDown(arr,0,k);
}
}
for(int i=0;i<k;i++){
System.out.println(arr[i]);
}
}

public static void main(String[] args){
int[] arr = {49,38,65,97,76,13,27,49,78,34,12,64,18};

heapSort(arr,3);

}
}
``````

# 4、需要排序的最短子数组长度

``````public class MinSortArrayLength {

public static int getMinSortArrayLength(int[] arr){
if(arr==null || arr.length==0)
return 0;
int min = arr[arr.length-1];
int noMinIndex = -1;
for(int i = arr.length-2;i>=0;i--){
if(arr[i] > min){
noMinIndex = I;
}
else{
min = Math.min(min,arr[I]);
}
}
if(noMinIndex == -1)
return 0;
int max = arr[0];
int noMaxIndex = -1;
for(int i=1;i<arr.length;i++){
if(arr[i] < max)
noMaxIndex = I;
else
max = Math.max(max,arr[I]);
}
return noMaxIndex - noMinIndex + 1;
}

public static void main(String[] args){
int[] arr = {1,5,3,4,2,6,7};
System.out.println(getMinSortArrayLength(arr));
}
}
``````

# 5、在数组中找到出现次数大于N／K的数

## 5.1 在数组中找到出现次数大于一半的数

``````public class FindAppearMoreThanHalfTimes {

public static void getAppearMoreThanHalfTimes(int[] arr){

int cand = arr[0];
int times= 1;
for(int i=1;i<arr.length;i++){
if (times == 0){
cand = arr[i];
times = 1;
}
else if(arr[i] == cand){
times++;
}
else{
times--;
}
}
times = 0;
for(int i=0;i<arr.length;i++){
if(arr[i] == cand)
times ++;
}
if(times > arr.length / 2){
System.out.println(cand);
}
else{
System.out.println("no such number");
}

}
``````

## 5.2 在数组中找到出现次数大于N／K的数

``````public class FindAppearMoreThanNDivKTimes {

public static void allCandsMinusOne(HashMap<Integer,Integer> cands){
for(Map.Entry<Integer,Integer> set: cands.entrySet()){
Integer key = set.getKey();
Integer value = set.getValue();
if(value == 1){
}
cands.put(key,value-1);
}
for(Integer removeKey:removeList){
cands.remove(removeKey);
}
}

public static HashMap<Integer,Integer> getReals(int[] arr,HashMap<Integer,Integer> cands){
HashMap<Integer,Integer> reals = new HashMap<Integer,Integer>();
for(int i=0;i<arr.length;i++){
int curNum = arr[i];
if (cands.containsKey(curNum)) {
if(reals.containsKey(curNum))
reals.put(curNum,reals.get(curNum) + 1);
else
reals.put(curNum,1);
}
}
return reals;
}

public static void printKMajor(int[] arr,int k){
if(k<2)
return;
HashMap<Integer,Integer> cands = new HashMap<Integer,Integer>();
for(int i =0;i<arr.length;i++){
if(cands.containsKey(arr[I]))
cands.put(arr[i],cands.get(arr[i])+1);
else{
if(cands.size() == k-1){
allCandsMinusOne(cands);
}
else{
cands.put(arr[i],1);
}
}
}
HashMap<Integer,Integer> reals = getReals(arr,cands);
boolean hasPrint = false;
for(Map.Entry<Integer,Integer> set:cands.entrySet()){
Integer key = set.getKey();
if(reals.get(key) > arr.length/k){
hasPrint = true;
System.out.print(key + " ");
}
}
System.out.println(hasPrint ? "":"no such number!");
}

public static void main(String[] args){
int[] arr = {1,3,3,6,43,5,6,3,23643,3,52423,3,446,2,32,562,4,2,2,2,4,4,3,5,2,2,2,4,4,4};
printKMajor(arr,5);
}
}
``````

# 6、在行列都排好序的矩阵中找数

``````public class FindKin2DMatrix {
public static boolean findK(int[][] arr,int k){
int rows = arr.length;
int cols = arr[0].length;
int m = 0;
int n = cols - 1;
while(m < rows && n >= 0){
if(arr[m][n] == k)
return true;
else if(arr[m][n] < k){
m++;
}
else
n--;
}
return false;
}

public static void main(String[] args){
int[][] arr = {{0,1,2,5},{2,3,4,7},{4,4,4,8},{5,7,7,9}};
System.out.println(findK(arr,7));
}
}
``````

# 7、不重复打印排序数组中相加和为给定值的所有二元组和三元组

## 7.1 二元组

``````public class FindAllTwoTuples {

public static void findAllTwoTuples(int[] arr,int k){
int left = 0;
int right = arr.length-1;
while(left < right){
if(arr[left] + arr[right] == k ){
if(left == 0 || arr[left] != arr[left-1]){
System.out.println(arr[left] + "," + arr[right]);
}
left++;
right--;
}
else if(arr[left] + arr[right] < k)
left ++;
else
right--;
}
}
public static void main(String[] args){
int[] arr = {-8,-4,-3,0,1,2,4,5,8,9};
findAllTwoTuples(arr,10);
}
}
``````

## 7.2 三元组

``````public class FindAllThreeTuples {

public static void findAllThreeTuples(int[] arr,int k){
if(arr == null || arr.length < 3)
return;
for(int i = 0;i<arr.length - 2;i++){
if(i==0 || arr[i] != arr[i-1]){
int tmp = k - arr[i];
int left = i + 1;
int right = arr.length - 1;
while(left < right){
if(arr[left] + arr[right] < tmp)
left++;
else if(arr[left] + arr[right] > tmp)
right--;
else{
if(left==i+1 || arr[left] != arr[left-1]){
System.out.println(arr[i] + "," + arr[left]+ "," + arr[right]);
}
left++;
right--;
}
}
}
}
}

public static void main(String[] args){
int[] arr = {-8,-4,-3,0,1,2,4,5,8,9};
findAllThreeTuples(arr,10);
}
}
``````

# 8、未排序数组中累加和为给定值的最长子数组长度

## 8.1 数组全为正数

``````public class MaxLengthSubArrayWithSumK {

public static int findMaxLengthSubArrayWithSumK(int[] arr,int k){
if(arr==null || arr.length==0)
return 0;
int left = 0;
int right = 0;
int maxlen = 0;
int sum = arr[0];
while(right < arr.length){
if(sum == k){
maxlen = Math.max(maxlen,right-left+1);
sum -= arr[left++];
}
else if(sum < k){
right++;
if(right == arr.length)
break;
sum += arr[right];
}
else{
sum -= arr[left];
left++;
}

}
return maxlen;
}

public static void main(String[] args){
int[] arr = {1,2,1,1,1};
System.out.println(findMaxLengthSubArrayWithSumK(arr,3));
}
}
``````

## 8.2 数组可正可负

``````public class MaxLengthSubArrayWithSumKV2 {
public static int findMaxLengthSubArrayWithSumK(int[] arr,int k){
if(arr==null || arr.length==0)
return 0;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(0,-1);
int maxlen = 0;
int sum = 0;
for(int i=0;i<arr.length;i++){
sum += arr[i];
if(map.containsKey(sum - k)){
maxlen = Math.max(i-map.get(sum-k),maxlen);
}
if(!map.containsKey(sum)){
map.put(sum,i);
}
}
return maxlen;
}

public static void main(String[] args){
int[] arr = {-1,1,2,0,1,1,1};
System.out.println(findMaxLengthSubArrayWithSumK(arr,3));
}
}
``````

# 9、子数组的最大累积和问题

``````public class MaxCumSumSubArray {
public static int findMaxCumSumSubArray(int[] arr){
int maxCur = arr[0];
int maxSoFar = arr[0];
for(int i=1;i<arr.length;i++){
maxCur = Math.max(arr[i],maxCur + arr[i]);
maxSoFar = Math.max(maxCur,maxSoFar);
}
return maxSoFar;
}

public static void main(String[] args){
int[] arr = {1,-2,3,5,-2,6,-1};
System.out.println(findMaxCumSumSubArray(arr));
}
}
``````

# 10、子矩阵的最大累积和问题

``````public class MaxCumSumSubMatrix {

public static int findMaxCumSumSubMatrix(int[][] arr){
if(arr == null || arr.length == 0 || arr[0].length == 0)
return 0;
int max = Integer.MIN_VALUE;
int cur = 0;
int[] s = null;
for(int i=0;i<arr.length;i++){
s = new int[arr[0].length];
for(int j = i;j<arr.length;j++){
s[0] += arr[j][0];
cur = s[0]; // 每一行开始的时候，都变成0
max = Math.max(cur,max);
for(int k = 1;k<s.length;k++){
s[k] += arr[j][k];
cur = Math.max(s[k] ,s[k] + cur);
max = Math.max(max,cur);
}

}
}
return max;
}

public static void main(String[] args){
int[][] arr = {{-90,48,78},{64,-40,64},{-81,-7,66}};
System.out.println(findMaxCumSumSubMatrix(arr));

}
}
``````

# 11、数组中子数组的累乘积

``````public class MaxCumMulitSubArray {

public static double findMaxCumMultiSubArray(double[] arr){
if(arr==null || arr.length==0)
return 0;
double max = arr[0];
double min = arr[0];
double res = arr[0];
double maxEnd = 0;
double minEnd = 0;

for(int i = 1;i<arr.length;i++){
maxEnd = max * arr[i];
minEnd = min * arr[i];
max = Math.max(Math.max(maxEnd,minEnd),arr[i]);
min = Math.min(Math.min(maxEnd,minEnd),arr[i]);
res = Math.max(max,res);
}
return res;
}

public static void main(String[] args){
double[] arr = {-2.5,4,0,3,0.5,8,-1};
System.out.println(findMaxCumMultiSubArray(arr));
}
}
``````

# 12、边界都是1的最大正方形大小

``````public class MaxSquareWithAllOne {

public static int findMaxSquareWithAllOne(int[][] arr){
if(arr==null || arr.length==0 || arr[0].length ==0)
return 0;
int[][] right = new int[arr.length][arr[0].length];
int[][] down = new int[arr.length][arr[0].length];
for(int i =arr.length-1;i>=0;i--){
for(int j=arr[0].length-1;j>=0;j--){
if(i==arr.length-1 && j==arr[0].length-1){
right[i][j] = arr[i][j] == 1 ? 1:0;
down[i][j] = arr[i][j] == 1? 1:0;
}
else if(i==arr.length-1){
down[i][j] = arr[i][j] == 1?1:0;
right[i][j] = arr[i][j] == 1?right[i][j+1] + 1:0;
}
else if(j==arr.length-1){
right[i][j] = arr[i][j] == 1 ? 1:0;
down[i][j] = arr[i][j] == 1?down[i+1][j] + 1:0;
}
else{
down[i][j] = arr[i][j] == 1?down[i+1][j] + 1:0;
right[i][j] = arr[i][j] == 1?right[i][j+1] + 1:0;
}
}
}
int maxSize = 0;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
int n = Math.min(right[i][j],down[i][j]);
for(int k=1;k<=n;k++){
if((j + k - 1) <arr[0].length && down[i][j+k-1] >= k && (i+k-1) < arr.length && right[i+k-1][j] >= k)
maxSize = Math.max(maxSize,k);
}
}
}
return maxSize;

}

public static void main(String[] args){
int[][] arr = {{0,1,1,1,1},{0,1,0,0,1},{0,1,0,0,1},{0,1,1,1,1},{0,1,0,1,1}};
System.out.println(findMaxSquareWithAllOne(arr));
}
}
``````

# 13、不包含本位置的累乘数组

``````public class ProductWIthoutSelf {

public static void getProductWithOutSelf(int[] arr){
if(arr==null || arr.length==0)
return;
int[] left = new int[arr.length];
int[] right = new int[arr.length];
int[] res = new int[arr.length];
left[0] = 1;
for(int i=1;i<arr.length;i++){
left[i] = left[i-1] * arr[i-1];
}
right[arr.length-1] = 1;
for(int i=arr.length-2;i>=0;i--){
right[i] = right[i+1] * arr[i+1];
}
for(int i=0;i<arr.length;i++){
res[i] = left[i] * right[i];
System.out.println(res[i]);
}

}
public static void main(String[] args){
int[] arr = {2,3,1,4};
getProductWithOutSelf(arr);
}
}
``````