1.二进制中1的个数
请实现一个函数,输入一个整数(以二进制串形式),输出该数二进制表示中 1 的个数。例如,把 9 表示成二进制是 1001,有 2 位是 1。因此,如果输入 9,则该函数输出 2。
解:
方法一:逐位判断
根据 与运算 定义,设二进制数字 nn ,则有:
若 n & 1 = 0 ,则 n 二进制 最右一位 为 0 ;
若 n & 1 = 1 ,则 n 二进制 最右一位 为 1 。
根据以上特点,考虑以下 循环判断 :
判断 n 最右一位是否为1 ,根据结果计数。
将 n 右移一位(本题要求把数字 n 看作无符号数,因此使用无符号右移操作)。
算法流程:
1.初始化数量统计变量res = 0;
2.逐位进行比较,当n = 0时跳出循环
res += n&1;
n >>>= 1;
3.返回res
public class Solution {
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
res += n & 1;
n >>>= 1;
}
return res;
}
}
方法二:巧用 n & (n - 1)
(n−1) 解析: 二进制数字 n 最右边的 1变成 0 ,此 1 右边的 0 都变成 1 。
n & (n - 1) 解析: 二进制数字 n 最右边的 1 变成 0 ,其余不变。
每次消去一个1,计数加一,知道数字为0为止
public class Solution {
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
res++;
n &= n - 1;
}
return res;
}
}
2.数值的整数次方
解:
class Solution {
public double myPow(double x, int n) {
if(x == 0) return 0;
long b = n;
double res = 1.0;
if(b < 0) {
x = 1 / x;
b = -b;
}
while(b > 0) {
if((b & 1) == 1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
}
3.打印从1到最大的n位数
输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。
4.删除链表的节点
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
解:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head == null){
return null;
}
if(head.val == val){
head = head.next;
}
ListNode cur = head;
while(cur.next != null && cur.next.val != val){
cur = cur.next;
}
if(cur.next!=null){
cur.next = cur.next.next;
}
return head;
}
}