给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。
示例 1:
输入: [5,7]
输出: 4
示例 2:
输入: [0,1]
输出: 0
贡献者
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int j=0;
while (m!=n&&m>0){
j++;
m=m>>1;
n=n>>1;
}
return m<<j;
}
}
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
public ListNode removeElements(ListNode head, int val) {
while (head.val == val && head != null) {
head = head.next;
}
ListNode result = null;
ListNode temp = null;
if (head != null) {
result = new ListNode(head.val);
temp = result;
head = head.next;
} else {
return null;
}
while (head != null) {
if (head.val != val) {
temp.next = new ListNode(head.val);
temp = temp.next;
}
head = head.next;
}
return result;
}