剑指Offer行榜【牛客网】练习(三)

1、二进制中1的个数

题目描述:
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示

代码:

public class Solution {
    public int NumberOf1(int n) {
        String str = Integer.toBinaryString(n);
        int count = 0;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)=='1'){
                count++;
            }
        }
        return count;
    }
}

2、数值的整数次方

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

代码:

//直接调用函数的代码
public class Solution {
    public double Power(double base, int exponent) {
        return Math.pow(base,exponent);
  }
}
//手撸的代码
public class Solution {
    public double Power(double base, int exponent) {
        double result = 1;
        if(exponent==0){
            
        }else if(exponent>0){
            int ex = exponent;
            while(ex>0){
                result*=base;
                ex--;
            }
        }else{
            int ex = -exponent;
            while(ex>0){
                result/=base;
                ex--;
            }
        }
        return result;
  }
}

3、调整数组顺序使奇数在偶数之前且顺序不变

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

代码:

import java.util.ArrayList;
public class Solution {
    public void reOrderArray(int [] array) {
        ArrayList<Integer> odd = new ArrayList<>();
        ArrayList<Integer> even = new ArrayList<>();
        for(int i=0;i<array.length;i++){
            if(array[i]%2==0){
                even.add(array[i]);
            }else{
                odd.add(array[i]);
            }
        }
        for(int i=0;i<odd.size();i++){
            array[i] = odd.get(i);
        }
        for(int i=0;i<even.size();i++){
            array[i+odd.size()] = even.get(i);
        }
    }
}

4、链表中的倒数第k个结点

题目描述:
输入一个链表,输出该链表中倒数第k个结点。

思路:
找到长度,得到正序length-k

注意点:
length-k<0时,返回null

代码:

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        ListNode h = head;
        int length = 0;
        while(h!=null){
            h = h.next;
            length++;
        }
        int order = length-k;
        if(order<0){
            return null;
        }
        ListNode r = head;
        while(order>0){
            r = r.next;
            order--;
        }
        return r;
    }
}

5、链表翻转

题目描述:
输入一个链表,反转链表后,输出新链表的表头。

思路:
1、递归
反转=当前结点作为剩下部分的最后结点的next
2、非递归
使用两个指针依次倒序

注意点:
递归方法——反转后,当前结点的next需要置为null
非递归方法——先保存next,再替换next

代码:

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
/**
使用递归的算法如下,但是复杂度太大。
*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }else{
            ListNode leftPart = ReverseList(head.next);
            ListNode l = leftPart;
            while(l.next!=null){
                l = l.next;
            }
            l.next = head;
            head.next = null;
            return leftPart;
        }
    }
}
//非递归
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode h1 = head;
        ListNode h2 = head;
        h1 = h1.next;
        ListNode h1next = h1.next;
        ListNode h2next = h2.next;
        h1.next = h2;
        h2.next = null;
        h1 = h1next;
        h2 = h2next;
        if(h1next==null){
            return h1;
        }
        while(h1.next!=null){
            h1next = h1.next;
            h2next = h1;
            h1.next = h2;
            h1 = h1next;
            h2 = h2next;
        }
        h1.next = h2;
        return h1;
    }
}
//非递归
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode node = null;
        ListNode h = head;
        while(h!=null){
            ListNode hnext = h.next;
            h.next = node;
            node = h;
            h = hnext;
        }
        return node;
    }
}

猜你喜欢

转载自blog.csdn.net/xiaowei_innocence/article/details/85346480