0
点赞
收藏
分享

微信扫一扫

剑指offer

炽凤亮尧 2022-03-30 阅读 109
java

1.哈希表就是一个超时的大动作

2.直接排序

class Solution {
    public int findRepeatNumber(int[] nums) {
        Arrays.sort(nums);
        int res=0;
        for(int i=0;i<nums.length-1;i++){
            if(nums[i]==nums[i+1]){
                res=nums[i];
                break;
            }
        }
        return res;
    }
}

 3.索引与值对应法

class Solution {
    public int findRepeatNumber(int[] nums) {
        for(int i = 0; i < nums.length; i++) {  
            //利用循环交换,将子循环排序,如果这个循环链中有重复,就直接返回
            while(nums[i] != i) {
                if(nums[nums[i]] == nums[i]) return nums[i];
                else {
                    //交换
                    int temp = nums[nums[i]];
                    nums[nums[i]] = nums[i];
                    nums[i] = temp;
                }
            }
        }
        return -1;
    }
}

1.暴力法

2.右上角往左下移,如果当前大于目标,左移,小于下移。注意越界问题!!!

class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int rows = matrix.length;
        int columns = matrix[0].length;
        int row = 0;
        int column = columns - 1;
        while(column>=0&&row<rows){
            if(matrix[row][column]==target){
                return true;
            }else if(matrix[row][column]>target){
                //左移
                column--;
            }else{
                //下移
                row++;
            }
        }
        return false;
    }
}

1.数组

class Solution {
    public String replaceSpace(String s) {
        char[] c=s.toCharArray();
        char[] result=new char[3*c.length];
        int k=0;
        for(int i=0;i<c.length;i++){
            if(c[i]==' '){
                result[k++]='%';
                result[k++]='2';
                result[k++]='0';
            }else{
                result[k]=c[i];
                k++;
            }
        }
        String s1=String.copyValueOf(result);
        String s2=s1.substring(0,k);
        return s2;
    }
}

 

 以后转且截取数组可以用这个方法。

2.StringBuider

class Solution {
    public String replaceSpace(String s) {
        StringBuilder sb=new StringBuilder();
        char[] c=s.toCharArray();
        for(int i=0;i<c.length;i++){
           if(c[i]==' '){
               sb.append("%20");
           }else{
               sb.append(c[i]);
           }
        }
        String result=sb.toString();
        return result;
    }
}

1.栈

class Solution {
    public int[] reversePrint(ListNode head) {
        //写一个栈
        Stack<Integer> s=new Stack();
        ListNode temp=head;
        while(temp!=null){
            s.push(temp.val);
            temp=temp.next;
        }
        //一个一个弹到数组里
        int[] res=new int[s.size()];
        for(int i=0;i<res.length;i++){
            res[i]=s.pop();
        }
        return res;
    }
}

 2.反转链表

class Solution {
    public int[] reversePrint(ListNode head) {
        //写个反转链表
        ListNode reverseHead=new ListNode();
        ListNode temp=head;
        int count=0;
        while(temp!=null){
            ListNode t=temp.next;
            temp.next=reverseHead.next;
            reverseHead.next=temp;
            temp=t;
            count++;
        }
        reverseHead=reverseHead.next;
        int[] res=new int[count];
        for(int i=0;i<res.length;i++){
            res[i]=reverseHead.val;
            reverseHead=reverseHead.next;
        }
        return res;
    }
}
class CQueue {
    Stack<Integer> in;
    Stack<Integer> out;
    public CQueue() {
        in=new Stack<>();
        out=new Stack<>();
    }
    
    public void appendTail(int value) {
        in.push(value);
    }
    
    public int deleteHead() {
        if(out.isEmpty()){
            if(in.isEmpty()){
                return -1;
            }else{
                //把in的都放到out里,然后弹出一个
                while(!in.isEmpty()){
                    out.push(in.pop());
                }
                return out.pop();
            }
        }else{
            //优先删除out里的
            return out.pop();
        }
    }
}

10-1和10-2完全一样啊。。懒得做了。。

 

class Solution {
    static final int MOD = 1000000007;

    public int fib(int n) {
        if (n < 2) {
            return n;
        }
        int[][] q = {{1, 1}, {1, 0}};
        int[][] res = pow(q, n - 1);
        return res[0][0];
    }

    public int[][] pow(int[][] a, int n) {
        int[][] ret = {{1, 0}, {0, 1}};
        while (n > 0) {
            if ((n & 1) == 1) {
                ret = multiply(ret, a);
            }
            n >>= 1;
            a = multiply(a, a);
        }
        return ret;
    }

    public int[][] multiply(int[][] a, int[][] b) {
        int[][] c = new int[2][2];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                c[i][j] = (int) (((long) a[i][0] * b[0][j] + (long) a[i][1] * b[1][j]) % MOD);
            }
        }
        return c;
    }
}

1.填表

class Solution {
    public int numWays(int n) {
        //
        int[] result=new int[101];
        result[0]=1;
        result[1]=1;
        for(int i=2;i<=100;i++){
            result[i]=(result[i-1]+result[i-2])%(1000000007);
        }
        return result[n];
    }
}

 2.动态规划

class Solution {
    public int numWays(int n) {
        //动态规划
        int a=1;
        int b=1;
        int sum=0;
        if(n<=1){
            return 1;
        }
        for(int i=2;i<=n;i++){
            sum=(a+b)%1000000007;
            a=b;
            b=sum;
        }
        return sum;
    }
}

3.斐波那契记得四舍五入round和n+1;这里取模这种不能用斐波那契??为什么??

class Solution {
    public int minArray(int[] numbers) {
        //二分一下
        int left=0;
        int right=numbers.length-1;
        int mid=0;
        while(left<right){
            mid=(left+right)/2;
            //中间大于右边,那么值一定在中间+1到右边
            if(numbers[mid]>numbers[right]){
                left=mid+1;
                //如果等于右边,只能排除当前右边
            }else if(numbers[mid]==numbers[right]){
                right--;
                //中间小于右边的话,说明一定在左边到中间
            }else{
                right=mid;
            }
        }
        return numbers[left];        
    }
}
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        //int类型二进制数小于32位。
        int count=0;
        for(int i=0;i<32;i++){
            int k=(1<<i);
            if((n&k)!=0){
                count++;
            }
        }
        return count;
    }
}
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int count=0;
        //一比特数
        while(n!=0){
            n&=(n-1);
            count++;
        }
        return count;
    }
}

迭代一下,学习一下递归。 

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode reverseHead=new ListNode();
        ListNode temp=head;
        while(temp!=null){
            ListNode t=temp.next;
            temp.next=reverseHead.next;
            reverseHead.next=temp;
            temp=t;
        }
        return reverseHead.next;
    }
}
class Solution {
    public ListNode reverseList(ListNode head) {
        //递归一下
        if(head==null||head.next==null){
            return head;
        }
        ListNode temp=reverseList(head.next);
        head.next.next=head;
        head.next=null;
        return temp;
    }
}
举报

相关推荐

0 条评论