# 剑指offer-java(2)

(1)栈的压入、弹出序列

import java.util.ArrayList;
import java.util.*;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length==0||popA.length==0)
return false;
int j=0;
for(int i=0;i<pushA.length;i++){
stack.push(pushA[i));
while(j<pushA.length&& stack.peek()==popA[j]){
stack.pop();
j++;
}
}
return stack.isEmpty();
}
}

(2)从上往下打印二叉树

import java.util.ArrayList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
this.val = val;

}

}
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
//层次遍历，借助一个队列来实现
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null)
return list;
queue.offer(root);
while(queue!=null){
TreeNode node=queue.poll();
if(node.left!=null)
queue.offer(root.left);
if(node.right!=null)
queue.offer(root.right);
}
return list;
}
}

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

public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
//二叉搜索树，后序遍历，即最后一个节点为根节点。且左子树小于根节点，右子树大于根节点的值
if(sequence.length==0)
return false;
return VerifySquenceOfBST(sequence,0,sequence.length-1);
}
public boolean VerifySquenceOfBST(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 VerifySquenceOfBST(sequence,start,i-1)&VerifySquenceOfBST(sequence,i,end-1);
}
}

4,二叉树中和为某一值的路径

import java.util.ArrayList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
this.val = val;

}

}
*/
public class Solution {
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
ArrayList<ArrayList<Integer>> pathList=new ArrayList<ArrayList<Integer>>();
if(root==null)
return ;
findpath(root,target,stack,pathList);
return pathList;
}
public void findpath(TreeNode root,int target,LinkedList<Integer> stack,ArrayList<ArrayList<Integer>> pathList){
if(root==null)
return;
if(root.left==null&&root.right==null){
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i: stack){
}
}
}else{
findpath(root.left,target-root.val,stack,pathList);
findpath(root.right,target-root.val,stack,pathList);
stack.remove(stack.size()-1);
}
}

5，复杂链表的复制

/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;

RandomListNode(int label) {
this.label = label;
}
}
*/
public class Solution{
return null;
}
}
}
return and;
}
}

6，二叉搜索树与双向链表

//二叉排序树-左子树比根节点值小，右子树比根节点值大
public class Solution{
public TreeNode Convert(TreeNode pRootOfTree){
if(pRootOfTree==null)
return null;
if(pRootOfTree.left==null&&pRootOfTree.right==null)
return pRootOfTree;
TreeNode left=Convert(pRootOfTree.left);
TreeNode p=left;
while(p!=null&&p.right!=null)
p=p.right;
if(left!=null){
p.right=pRootOfTree;
pRootOfTree.left=p;
}

TreeNode right=Convert(pRootOfTree.right);
if(right!=null){
pRootOfTree.right=right;
right.left=pRootOfTree;
}
return left!=null?left:pRootOfTree;
}
}

7，字符串的排列

import java.util.*;
public class Solution{
public ArrayList<String> Permutation(String str){
ArrayList<String> list=new ArrayList<String>();
if(str==null||str.length()==0)
return list;
char[] c=str.toCharArray();
TreeSet<String> result=new TreeSet<String>();
Permutation(c,0,result);
return list;
}
public void Permutation(char[] c,int begin,TreeSet<String> result){
if(c==null||c.length==0||begin<0||begin>c.length-1)
return;
if(begin==c.length-1)
else{
for(int i=begin;i<c.length;i++){
swap(c,begin,i);
Permutation(c,begin+1,result);
swap(c,begin,i);
}
}
}
public void swap(char[] c,int begin, int i){
char tmp=c[begin];
c[begin]=c[i];
c[i]=tmp;
}
}

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

public class Solution{
public int MoreThanHalfNum(int[] array){
if(array==null||array.length==0)
return 0;
int count=0;
int mid=array[(array.length-1)/2];
for(int i: array){
if(i==mid)
count++;
}
if(array.length<count*2)
return mid;
else
return 0;
}
}

9，最小的K个数

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

10,连续子数组的最大和

import java.util.*;
public class Solution{
public int findSumOfSubArray(int[] array){
List<Integer> list=new ArrayList<Integer>();
if(array.length==0||array==null)
return 0;
for(int i=0;i<array.length;i++){
int sum=0;
for(int j=i;j<array.length;j++){
sum+=array[j];
}
}
Collections.sort(list);
return list.get(list.size()-1);
}
}

11，整数中1出现的次数（从1到n整数中1出现的次数）

import java.util.*;
public class Solution{
public int NumberOfBetween1AndN(int n){
int i=countNum(n);
return i;
}
public int countNum(int n){
if(n==0)
return 0;
if(n<10)
return 1;
int len=new Integer(n).toString().length();
int first;
int count=0;
for(len--;len>=0;len--){
first=(int)(n/Math.pow(10,len));
if(len==0&&first!=0)
return ++count;
else if(len==0)
return count;
if(first>1)
count+=first*getCount(len)+getCount2(len);
else if(first==1)
count+=n%getCount2(len)+1+first*getCount(len);
n=(int)(n%getCount(len));
}
return count;
}
public int getCount(int len){
return len*(int)(Math.pow(10,len-1));
}
public int getCount2(int len){
return (int)Math.pow(10,len);
}
}

12,把数组排成最小的数

import java.util.*;
public class Solution{
public String PrintMinNumber(int[] numbers){
ArrayList<Integer> list=new ArrayList<Integer>();
String s="";
for(int i=0;i<numbers.length;i++){
}
Collections.sort(list,new Comparator<Integer>(){
public int compare(Integer str1,Integer str2){
String s1=str1+""+str2;
String s2=str2+""+str1;
return s1.compareTo(s2);
}
});
for(int j: list){
s+=j;
}
return s;
}
}

13,丑数

import java.util.*;
public class Solution{
public int GetUglyNumber(int index){
if(index==1)
return 1;
if(index==0)
return 0;
List<Integer> list=new ArrayList<Integer>();
int i2=0,i3=0,i5=0;
for(list.size()<index){
int n2=list.get(i2)*2;
int n3=list.get(i3)*3;
int n5=list.get(i5)*5;
int min=Math.min(n2,Math.min(n3,n5));
if(min==n2)
i2++;
if(min==n3)
i3++;
if(min==n5)
i5++;
}
return list.get(list.size()-1);
}
}

import java.util.*;
public class Solution{
public int firstNotRepeatingChar(String str){
if(str==null||str.length()==0)
return -1;
for(int i=0;i<str.length();i++){
if(str.length()-str.replaceAll(str.charAt(i)+"","").toString().length()==1)
return i;
}
return -1;
}
}

public class Solution{
public int InversePairs(int[] array){
int count=0;
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(array[i]>array[j])
count++;
}
}
return count;
}
}

import java.util.*;
public class Solution{
return null;
int count1=0;
while(p1!=null){
p1=p1.next;
count1++;
}
int count2=0;
while(p2!=null){
p2=p2.next;
count2++;
}
int flag=count1-count2;
if(flag>0){
while(flag>0){
flag--;
}
}
}
if(flag<=0){
while(flag<0){
flag++;
}
}
}
return null;
}
}

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

public class Solution{
public int getNumber(int[] array,int k){
if(array.length==0)
return 0;
int low=0;
int high=array.length-1;
int mid=(low+high)/2;
int count=0;
while(low<high){
mid=(low+high)/2;
if(array[mid]>k){
high=mid-1;
}else if(array[mid]<k){
low=mid+1;
}else{
count=1;
int temp=mid-1;
while(temp>=0&&array[temp]==array[mid]){
count++;
temp--;
}
temp=mid+1;
while(temp<array.length&&array[temp]==array[mid]){
count++;
temp++;
}
break;
}
}
return count;
}
}

public class Solution{
public int TreeDepth(TreeNode root){
if(root==null)
return 0;
int nLeft=TreeDepth(root.left);
int nRight=TreeDepth(root.right);
return nLeft>nRight?(nLeft+1):(nRight+1);
}
}

首先了解什么是平衡二叉树：

import java.util.*;
public class Solution{
public boolean isBalanced(TreeNode root){
if(root==null)
return true;
if(Math.abs(getHight(root.left)-getHeight(root.right))>1)
return false;
return isBalanced(root.left)&isBalanced(root.right);
}
public int getHeight(TreeNode root){
if(root==null)
return 0;
return Math.max(getHeight(root.left),getHeight(root.right));
}
}

import java.util.*;
//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
public class Solution{
public void findNumsAppearOnce(int[] array,int num1[],int num2[]){
if(array.length<2){
num1[0]=0;
num2[0]=0;
return;
}
if(array.length==2||array[0]!=array[1]){
num1[0]=array[0];
num2[0]=array[1];
}
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(array[i]^array[j]==0)
array[i]=array[j]=0;
}
}
List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<array.length;i++){
if(array[i]!=0)
}
num1[0]=list.get(0);
num2[0]=list.get(1);
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120