【剑指offer】之【 代码的完整性】

数值的整数次方

题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

保证base和exponent不同时为0

解法一:

  • 调用Math.pow()函数 估计面试管不让这么写吧—
public class Solution {
    public double Power(double base, int exponent) {
        if(exponent == 0){
            return 1.00;
        }
        if(base == 0){
            return 0.00;
        }
        return Math.pow(base, exponent);
  }
}

解法二:

  • 使用迭代方式 自己实现一个Math.pow()函数
  • 最重要的就是判断边界条件 我们不能用 num1 == num2 来判断小数的相等情况,因为计算机表达小数是不准确的。
public class Solution {
    //给其他函数或者调用者一个提示的作用
    boolean invalid_Input = false;
    public double Power(double base, int exponent) {
        //校验底数的边界
        if(equals(base, 0.0)){
            invalid_Input = true;
            return 0.0;
        }
        //把幂看作是正数算
        int absExponent = Math.abs(exponent);
        double result = powerWithUnsignedExponent(base, absExponent);
        //如果幂是负数的话我们需要将结果倒置
        if(exponent < 0){
            result = 1.0 / result;
        }
        return result;
    }
    
    public double powerWithUnsignedExponent(double base, int absExponent){
        double res = 1.0;
        for(int i = 0;i < absExponent;++i){
            res *= base;
        }
        return res;
    }
    
    //判断小数是否相等
    //由于计算机表达小数不准确,用差的绝对值的范围来看是否相同不可以用  num1 == num2判断 
    public boolean equals(double a, double b){
        if((a - b > -0.0000001 && (a - b < 0.0000001))){
            return true;
        }
        return false;
    }
}

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

题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

解法一:

  • 额外开辟两个空间,以空间换时间 空间复杂度O(N) 时间O(N)
public void reOrderArray(int [] array) {
        if(array == null || array.length == 0){
            return;
        }
        ArrayList<Integer> list1 = new ArrayList();
        ArrayList<Integer> list2 = new ArrayList();
        
        for(int i = 0; i < array.length;i++){
            if((array[i] & 1) == 1){
                list1.add(array[i]);
            }else{
                list2.add(array[i]);
            } 
        }
        for(int i = 0;i < list1.size();i++){
            array[i] = list1.get(i);
        }
        int index = list1.size();
        for(int i = 0;i < list2.size();i++){
            array[index] = list2.get(i);
            index++;
        }
    }

解法二:

  • 利用选择排序 再遍历的过程中把需要的选择出来
  • 时间O(N^2) 空间O(1)
  • 运用
public class Solution {
    public void reOrderArray(int [] array) {
        if(array == null || array.length == 0){
            return;
        }
       for(int i = 1;i < array.length;i++){
            for(int j = i - 1;j >= 0;j--){
                int a = array[j] & 1;
                int b = array[j+1] & 1;
                if(a==0 && b==1){
                    swap(array, j, j + 1);
                }
            }
        }
    }
    public void swap(int[] arr, int a, int b){
        int tem = arr[a];
        arr[a] = arr[b];
        arr[b] = tem;
    }
}
发布了118 篇原创文章 · 获赞 5 · 访问量 8730

猜你喜欢

转载自blog.csdn.net/weixin_43672855/article/details/105306288