剑指offer11-20

二进制中1的个数

public class Solution {
    public int NumberOf1(int n) {
        int sum=0;
        while(n!=0)
        {
            sum++;
            n=n&(n-1);
        }
        return sum;
        //假设二进制为1100n-1为1011两个进行相与是1000这样就去除了一个最右边的一个1当没有1的时候此程序执行了多少次就是多少   }
   }
}

数值的整数次方

public class Solution {
    public double Power(double base, int exponent) {
        //当n为偶数,a^n =(a*a)的n/2次方 当n为奇数,a^n = (a*a)的n/2次方*a所以可以用递归求解
        //时间复杂度:log(n)
        if (exponent==0) return 1;
        if (exponent==1) return base;
        boolean isnegative=false;
        if(exponent<0)
        {
            exponent=-exponent;
            isnegative=true;
        }
        double number=Power(base*base,exponent/2);
        if(exponent%2==1)
            number=number*base;
        return isnegative?(1/number):number;

            
            
            
        
  }
}

调整数组顺序使得奇数位于偶数前面

public class Solution {
    public void reOrderArray(int [] array) {
        int oddcnt=0;
        for(int val:array)
        {
            if(val%2==1)
                oddcnt++;//记录数组中的奇数个数
        }
        int copy[]=array.clone();
        int i=0;
        for(int number:copy)
        {
            if(number%2==1)
                array[i++]=number;
            else
                array[oddcnt++]=number;
        }
    }
}

链表倒数第k个节点

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        //创建两个指针,让一个指针跑到第k个节点上,第二个指针指到头结点上,两个指针同时跑,当第一个
        //指针跑到尾部是第二个也就跑到了倒数第k个节点
        if(head==null||k==0)
        {
            return null;
        }
        ListNode p1,p2;
        p1=p2=head;
        while(p1!=null&&k-->0)
        {
            p1=p1.next;
        }
        if(k>0)
            return null;//判断如果倒数的k个节点超过了链表的长度返回null
        while(p1!=null)
        {
            p1=p1.next;
            p2=p2.next;
        }
        return p2;
    }
}

反转链表

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode newlist=new ListNode(0);
        while(head!=null)
        {
            ListNode next=head.next;
            head.next=newlist.next;
            newlist.next=head;
            head=next;
        }
        return newlist.next;
    }
}

合并两个排序的链表

/*
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;
        ListNode head=new ListNode(1);
        ListNode cur=head;
        while(list1!=null&&list2!=null)
        {
            if(list1.val<=list2.val)
            {
                cur.next=list1;
                list1=list1.next;
            }
            else
            {
                cur.next=list2;
                list2=list2.next;
            }
            cur=cur.next;
        }
        if(list1!=null) 
            cur.next=list1;
        if(list2!=null) 
            cur.next=list2;
        return head.next;
        
        
    }
}

树的子结构

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

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

    }

}
*/
public class Solution {
  
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root1==null||root2==null)
            return false;
        //找出根节点相同的位置
        boolean bol=false;
        if(root1!=null&&root2!=null)
        {
        if(root1.val==root2.val)
            bol=isSubTree(root1,root2);//找到了根节点相同,调用方法判断
        if(!bol)
            bol=HasSubtree(root1.left,root2);//从左子树找根节点相同的节点
        if(!bol)
            bol=HasSubtree(root1.right,root2);//从右子树找根节点相同的节点
        }
        return bol;
    }
    //匹配两个二叉树的每一个节点
    public static boolean isSubTree(TreeNode root1,TreeNode root2)
    {
        if(root2==null)
            return true;//B节点全部匹配
        if(root1==null)
            return false;//B节点还没有匹配A节点就完了
        if(root1.val!=root2.val)
            return false;
        return isSubTree(root1.left,root2.left)&&isSubTree(root1.right,root2.right);//判断每一个节点是否相等
    }
}

二叉树的镜像

/**
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) {
        if(root==null)
            return ;
        swap(root);
        Mirror(root.left);
        Mirror(root.right);
        
    }
    private void swap(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}

顺时针打印矩阵

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> ret=new ArrayList<Integer>();
        int r1=0,r2=matrix.length-1;//行数
        int c1=0,c2=matrix[0].length-1;//列数
        while(r1<=r2&&c1<=c2)
        {
            for(int i=c1;i<=c2;i++)
                ret.add(matrix[r1][i]);
            for(int i=r1+1;i<=r2;i++)
                ret.add(matrix[i][c2]);
            if(r1!=r2)
            {
                for(int i=c2-1;i>=c1;i--)
                    ret.add(matrix[r2][i]);
            }
            if(c1!=c2)//防止重复放入元素
            {
                for(int i=r2-1;i>r1;i--)
                    ret.add(matrix[i][c1]);
            }
            r1++;
            r2--;
            c1++;
            c2--;
                
        }
        return ret;
        
       
    }
}




/*int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;
    while (r1 <= r2 && c1 <= c2) {
        for (int i = c1; i <= c2; i++)
            ret.add(matrix[r1][i]);
        for (int i = r1 + 1; i <= r2; i++)
            ret.add(matrix[i][c2]);
        if (r1 != r2)
            for (int i = c2 - 1; i >= c1; i--)
                ret.add(matrix[r2][i]);
        if (c1 != c2)
            for (int i = r2 - 1; i > r1; i--)
                ret.add(matrix[i][c1]);
        r1++; r2--; c1++; c2--;
    }
    return ret;
    */

包含main函数的栈

import java.util.Stack;

public class Solution {
    private Stack<Integer> stack=new Stack<Integer>();
    private Stack<Integer> minstack=new Stack<Integer>();
    public void push(int node) {
        stack.push(node);
        minstack.push(minstack.isEmpty()?node:Math.min(node,minstack.peek()));
    }
    
    public void pop() {
        stack.pop();
        minstack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int min() {
        return minstack.peek();
        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值