算法---剑指offer

public static boolean duplicate(int[] nums){
// 3. 数组中重复的数字
        if (nums==null || nums.length<=0){
            return false;
        }
        for (int i=0;i<=nums.length-1;i++){
            while (nums[i]!=i){
                // 重要:一直让其变为nums[i] = i为止                
                if(nums[i]==nums[nums[i]]){
                    return true;
                }
                else {
                    int temp = nums[i];
                    nums[i] = nums[nums[i]];
                    nums[nums[i]] = temp;
                }
            }
        }
        return false;
    }
public static boolean Find(int target,int[][] matrix){
//  4. 二维数组中的查找
        if (matrix.length ==0 || matrix == null || matrix[0].length==0){
            return false;
        }
        int row_length = matrix[0].length-1;
        int col_length = matrix.length-1;
        int row = 0;
        int col = col_length;
        while(row<=row_length && col>=0){
            if (matrix[row][col]>target){
                col -= 1;
            }
            else if (matrix[row][col]
public static String replaceSpace(String s){
//    5. 替换空格
//    5.1:先看需要增加多少个空位置
    int a = s.length()-1;
    StringBuilder new_s = new StringBuilder(s);
    for(int i = 0; i<=a; i++){
        if(new_s.charAt(i)==' '){
            new_s.append("  ");
        }
    }
//    5.2:循环进行替换
    int y = new_s.length()-1;
    for(int i = a; i>=0; i--){
        if(new_s.charAt(i)==' '){
            new_s.setCharAt(y,'0');
            new_s.setCharAt(y-1,'2');
            new_s.setCharAt(y-2,'%');
            y-=3;
        }
        else {
            new_s.setCharAt(y,new_s.charAt(i));
            y-=1;
        }
    }
    return String.valueOf(new_s);
}

需要测试:

public static void print_lianbiao(Node node){
//  6. 从尾到头打印链表(递归模式)
        if(node==null){
            System.out.println("error");
        }
        if(node!=null){
            node = node.next;
            System.out.println(node.val);
        }
}
public static void print_liaobiao_1(Node node){
        //  6. 从尾到头打印链表(栈)
    if (node==null){
        System.out.println("error");

    }
    Stack stack = new Stack<>();
    while(node!=null){
        stack.add(node.val);
        node = node.next;
    }
    while (!stack.isEmpty()){
        System.out.println(stack.pop());
    }
}

你可能感兴趣的:(算法)