剑指offer题目--代码的规范性

3.3 代码的完整性

功能测试、边界测试、负面测试(错误输入)

3种错误处理方法

1、函数返回值

2、发生错误时设置一个全局变量

3、异常

11 数值的整数次方

循环

public class Solution {
    public double Power(double base, int exponent) {
        if (exponent == 0) {
            return 1.0;
        }
        if (base - 0.0 == 0.00001 || base - 0.0 == -0.00001)  {
            if (exponent < 0) {
                throw new RuntimeException("除0异常"); 
            }else{
                return 0.0;
            }
        }
        int e = exponent > 0 ? exponent: -exponent;
        double result = 1;
        while (e != 0) {
            result = (e & 1) != 0 ? result * base : result;
            base *= base;
            e = e >> 1;
        }
        return exponent > 0 ? result : 1/result;
  }
}

递归

public class Solution {
    public double Power(double base, int exponent) {
        boolean flag = exponent < 0;
        if (flag) {
            exponent = -exponent;
        }
        double result = getPower(base, exponent);
        return flag ? 1 / result : result;
  }
   
    public static double getPower(double base, int exp) {
        if (exp == 0) {
            return 1;
        }
        if (exp == 1) {
            return base;
        }
        double ans = getPower(base, exp >> 1);
        ans *= ans;
        if ((exp & 1) == 1) {
            ans *= base;
        }
        return ans;
    }
}

12 打印1到最大的n位数

13 在0(1)时间删除链表结点

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

public class Solution {
    public void reOrderArray(int [] array) {
        int length = array.length;
        if(length == 0){
            return;
        }
        int pBegin=0;
        int pEnd = length-1;
        
        while(pBegin < pEnd ){
            while(pBegin < pEnd && (array[pBegin] & 0x1) != 0){
                pBegin ++;
            }
            
            while(pBegin < pEnd && (array[pEnd] & 0x1) == 0){
                pEnd--;
            }
            
            if(pBegin < pEnd){
                int tmp = array[pBegin];
                array[pBegin] = array[pEnd];
                array[pEnd] = tmp;
            }
        }
    }
}

拓展,牛客网多条件情况:并保证奇数和奇数,偶数和偶数之间的相对位置不变。

public class Solution {
    public void reOrderArray(int [] array) {
        for (int i = 0; i < array.length;i++){
            for (int j = array.length - 1; j>i;j--)
            {
                if (array[j] % 2 == 1 && array[j - 1]%2 == 0) //前偶后奇交换
                {
                    int tmp = array[j];
                     array[j] = array[j-1];
                    array[j-1] = tmp;
                }
            }
        }
    }
}

3.4 代码的鲁棒性

15 链表中倒数第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) {
        if(head == null || k==0){
            return null;
        }
        
        ListNode pAhead = head;
        ListNode pBehind = null;
        
        for(int i=0;i<k-1;++i){
            if(pAhead.next != null){
                pAhead = pAhead.next;
            }else{
                return null;
            }
            
        }
        pBehind = head;
        while(pAhead.next != null){
            pAhead = pAhead.next;
            pBehind = pBehind.next;
        }
        return pBehind;
    }
}

相关题目:

求链表中间结点

从链表头结点出发,一个指针走一步,一个指针走两步。

判断词汇表单向链表是否形成了环形结构。同上一个,如果走的快的指针追上了走得慢的指针,就是;如果走到链表末尾(next指向null),就不是。

应用--leetcode题目 141. 环形链表

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null ){
            return false;
        }
        
        ListNode ahead = head.next;
        ListNode behind = head;
        
        while(ahead != behind){
            if(ahead.next == null || ahead.next.next == null){
                return false;
            }
            ahead = ahead.next.next;
            behind = behind.next;
        }
        return true;
    }
}

16 反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    ArrayList<Integer> arrayList=new ArrayList<Integer>();
    public ListNode reverseList(ListNode head) {
        ListNode reversedHead = null;
        ListNode node = head;
        ListNode prenode = null;
        while(node != null){
            ListNode next = node.next;
            if(next == null){
                reversedHead = node;
            }
            node.next = prenode;
            prenode = node;
            node = next;
        }
        return reversedHead;
    }
}

17 合并两个排序的链表

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }

        ListNode mergeHead = null;

        if(l1.val >= l2.val){
            mergeHead = l2;
            mergeHead.next = mergeTwoLists(l1,l2.next);
        }else{
            mergeHead = l1;
            mergeHead.next = mergeTwoLists(l2,l1.next);
        }
        return mergeHead;
    }
}

18 树的子结构

/**
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) {
        boolean result = false;
        if(root1 != null && root2 != null){
            if(root1.val == root2.val){
                result = DoesTree1HasTree2(root1,root2);
            }
            if(!result){
                result = HasSubtree(root1.left,root2);
            }
            if(!result){
                result = HasSubtree(root1.right,root2);
            }
        }
         return result;
    }
    boolean DoesTree1HasTree2(TreeNode root1,TreeNode root2){
        if(root2 == null){
            return true;
        }
        if(root1 == null){
            return false;
        }
        if(root1.val!= root2.val){
            return false;
        }
        return DoesTree1HasTree2(root1.left,root2.left)&&
            DoesTree1HasTree2(root1.right,root2.right);
    }
}
posted @ 2020-02-06 20:46  小仙女学编程  阅读(173)  评论(0编辑  收藏  举报