剑指offer算法题JZ11-JZ15
题库链接(牛客网)
JZ11 二进制中1的个数
描述
输入一个整数,输出该数32位二进制表示中1的个数。其中负数用补码表示。
示例1
输入:10
返回值:2
测试代码(java)
public class JZ11 {
public static void main(String[] args) {
System.out.println(NumberOf1(1));
System.out.println(Integer.toBinaryString(1));
System.out.println(NumberOf1(-10));
System.out.println(Integer.toBinaryString(-10));
}
public static int NumberOf1(int n) {
int num = 0;
if (n < 0){
n = n ^ 0x80000000; // 异或10000000000000000000000000000000
num += 1;
}
while (n != 0){
if (n % 2 == 1){
num += 1;
}
n = n >> 1;
}
return num;
}
}
JZ12 数值的整数次方
描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
保证base和exponent不同时为0。不得使用库函数,同时不需要考虑大数问题,也不用考虑小数点后面0的位数。
示例1
输入:2.00000,3
返回值:8.00000
示例2
输入:2.10000,3
返回值:9.26100
示例3
输入:2.00000,-2
返回值:0.25000
说明:2的-2次方等于1/4=0.25
测试代码(java)
public class JZ12 {
public static void main(String[] args) {
System.out.println(Power(2.00000,3));
System.out.println(Power(2.10000,3));
System.out.println(Power(2.00000,-2));
}
public static double Power(double base, int exponent) {
if (exponent == 0){
return 1;
} else if (exponent > 0){
double t = base;
while (exponent > 1){
base *= t;
exponent -= 1;
}
} else {
double t = 1/base;
base = t;
while (exponent < -1){
base *= t;
exponent += 1;
}
}
return base;
}
}
JZ13 调整数组顺序使奇数位于偶数前面
描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
示例1
输入:[1,2,3,4]
返回值:[1,3,2,4]
示例2
输入:[2,4,6,5,7]
返回值:[5,7,2,4,6]
测试代码(java)
import java.util.Arrays;
public class JZ13 {
public static void main(String[] args) {
int[] a = {1,2,3,4};
System.out.println(Arrays.toString(reOrderArray(a)));
a = new int[]{2, 4, 6, 5, 7};
System.out.println(Arrays.toString(reOrderArray(a)));
}
public static int[] reOrderArray (int[] array) {
// write code here
int[] b = new int[array.length];
int j = 0;
for (int k : array) {
if (k % 2 == 1) {
b[j] = k;
j++;
}
}
for (int k : array) {
if (k % 2 == 0) {
b[j] = k;
j++;
}
}
return b;
}
}
JZ14 链表中倒数最后k个结点
描述
输入一个链表,输出一个链表,该输出链表包含原链表中从倒数第k个结点至尾节点的全部节点。
如果该链表长度小于k,请返回一个长度为 0 的链表。
示例1
输入:{1,2,3,4,5},1
返回值:{5}
测试代码(java)
import java.io.FileReader;
public class JZ14 {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(4);
ListNode l5 = new ListNode(5);
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
System.out.println(FindKthToTail(l1, 3).val);
}
public static ListNode FindKthToTail (ListNode pHead, int k) {
// write code here
if (pHead != null) {
int num = 1;
ListNode head1 = pHead;
while (head1.next != null) {
num++;
head1 = head1.next;
}
if (k > num || k <= 0){
return null;
}
while (num - k > 0 && pHead.next != null) {
pHead = pHead.next;
k++;
}
}
return pHead;
}
}
JZ15 反转链表
描述
输入一个链表,反转链表后,输出新链表的表头。
示例1
输入:{1,2,3}
返回值:{3,2,1}
思路提示
共需三个指针,一个指针初始指null
测试代码(java)
public class JZ15 {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
l1.next = l2;
l2.next = l3;
ListNode l = ReverseList(l1);
System.out.println(l.val + " " + l.next.val + " " + l.next.next.val);
}
// 反转链表(需要三个指针)
public static ListNode ReverseList(ListNode head) {
ListNode h1 = head;
ListNode h2;
ListNode pre = null;
while (h1 != null){
h2 = h1.next;
h1.next = pre;
pre = h1;
h1 = h2;
}
return pre;
}
}