剑指Offer学习笔记(2)

一、代码的完整性

1. 数值的整数次方
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
publicclassSolution {
        publicdoublePower(doublebase, intexponent) {
        if(exponent==0){
            return1;
        }else  if(exponent>0){
            doubleresult=1;
            for(inti=1;i<=exponent;i++){
                result*=base;
            }
            returnresult;
        }else{
            doubleresult=1.0
            exponent=-exponent;
            for(inti=1;i<=exponent;i++){
                result*=(double)base;
            }
             
            return((double)1.0)/((double)result);
        }
        //return exponent;
 
    }
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
publicclassSolution {
  publicvoidreOrderArray(int[] array) {
 
        intindexEven = -1;
 
        for(inti = 0; i < array.length; i++) {
            if(array[i] % 2== 1) {
                if(indexEven < i && indexEven != -1) {
                    inttemp = array[i];
                    for(intj = i; j > indexEven; j--) {
                        array[j] = array[j-1];
                    }
                    array[indexEven] = temp;
                    indexEven += 1;
                }
 
            else{
                if(indexEven == -1) {
                    indexEven = i;
                }
            }
 
        }
 
    }
}


 3. 反转链表

输入一个链表,反转链表后,输出链表的所有元素。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
publicclassSolution {
    publicListNode ReverseList(ListNode head) {
        ListNode cur=head;
        ListNode next=null;
        ListNode pre=null;
        if(head==null||head.next==null){
            returnhead;
        }
        while(cur!=null){
            next=cur.next;
            cur.next=pre;
             
            pre=cur;
            cur=next;
        }
        returnpre;
    }
}
4. 链表中倒数第k个结点
代码思路如下:两个指针,先让第一个指针和第二个指针都指向头结点,然后再让第一个指正走(k-1)步,到达第k个节点。然后两个指针同时往后移动,当第一个结点到达末尾的时候,第二个结点所在位置就是倒数第k个节点了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
publicclassSolution {
    publicListNode FindKthToTail(ListNode head, intk) {
 
        ListNode first = head;
        ListNode second = head;
 
        if(head == null||k<=0) {
            returnnull;
        }
        for(inti = 1; i < k; i++) {
            if(second.next != null) {
                second = second.next;
            else{
                returnnull;
            }
        }
        while(second.next!=null){
            second=second.next;
            first=first.next;
        }
         
         
 
        returnfirst;
    }
 
}
 
5. 打印1到最大的n位数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
publicclassSolution {
    /**
     * 递归调用的思路
     * @param n
     */
    publicstaticvoidPrint1ToMaxOfNDigits(intn) {
        if(n<=0){
            return;
        }
         
        char[] num = newchar[n];
        //num[n]='';
         
        for(inti=0;i<10;i++){
            num[0]=(char)(i+'0');//字符转化成数字
            Print1ToMaxOfNDigitsRecursive(num,n,0);
        }
         
 
    }
 
    privatestaticvoidPrint1ToMaxOfNDigitsRecursive(char[] num, intlength, intindex) {
        if(index==length-1){//如果组合完毕
//          System.out.print(num);
            printNum(num);
            return;
        }
         
        for(inti=0;i<10;i++){
            num[index+1]=(char) ((char) i+'0');
            Print1ToMaxOfNDigitsRecursive(num, length, index+1);
        }
    }
 
    privatestaticvoidprintNum(char[] num) {
        booleanisBegin0 = true;
        intlen=num.length;
        for(inti=0;i<len;i++){
            if(isBegin0&&num[i]!='0'){
                isBegin0=false;
            }
             
            if(!isBegin0){
                System.out.print(num[i]);
            }
             
        }
        System.out.print('  ');
         
    }

二、代码的完整性

1. 链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
publicclassSolution {
    publicListNode FindKthToTail(ListNode head, intk) {
 
        ListNode first = head;
        ListNode second = head;
 
        if(head == null||k<=0) {
            returnnull;
        }
        for(inti = 1; i < k; i++) {
            if(second.next != null) {
                second = second.next;
            else{
                returnnull;
            }
        }
        while(second.next!=null){
            second=second.next;
            first=first.next;
        }
         
         
 
        returnfirst;
    }
 
}
相关题目:找中间结点可以两个指针,一个一次走一个 一个一次走两个,快的首先到结尾。
判断是否是环形链表,快的追上慢的。
2. 反转链表
输入一个链表,反转链表后,输出链表的所有元素。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
publicclassSolution {
        publicListNode ReverseList(ListNode head) {
        ListNode temp=null;
        ListNode orgin=null;
        if(head==null){
            returnhead;
        }
        temp=orgin=head;
        head=head.next;
        orgin.next=null;
        while(head!=null){ 
            temp=head;
            head=head.next;
            temp.next=orgin;
            orgin=temp;
        }
 
        returntemp;
    }
}
3. 反转链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
publicclassSolution {
    publicListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null){
            returnlist2;
        }
        if(list2==null){
            returnlist1;
        }
        ListNode mergeList = null;
        if(list1.val<list2.val){
            mergeList=list1;
            mergeList.next=Merge(list1.next,list2);
        }else{
            mergeList=list2;
            mergeList.next=Merge(list1,list2.next);
        }
        returnmergeList;
    }
}

4. 树的子结构
输入两颗二叉树A,B,判断B是不是A的子结构。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
 
    public TreeNode(int val) {
        this.val = val;
 
    }
 
}
*/
publicclassSolution {
    publicbooleanHasSubtree(TreeNode root1, TreeNode root2) {
        booleanflag=false;
        if(root1!=null&&root2!=null){
            if(root1.val==root2.val){
                flag=doesTree1HaveTree2(root1,root2);
            }
            if(!flag){
                flag=HasSubtree(root1.left,root2);
            }
            if(!flag){
                flag=HasSubtree(root1.right,root2);
            }
        }
        returnflag;
    }
 
    privatebooleandoesTree1HaveTree2(TreeNode root1, TreeNode root2) {
        if(root2==null){
            returntrue;
        }
         
        if(root1==null){
            returnfalse;
        }
         
        if(root1.val!=root2.val){
            returnfalse;
        }
        if(root1.val==root2.val){
            returndoesTree1HaveTree2(root1.left, root2.left)&&doesTree1HaveTree2(root1.right, root2.right);
        }
        returnfalse;
    }
}



踏实 踏踏实实~
原文地址:https://www.cnblogs.com/mrzhang123/p/5365800.html