1.只出现一次的数字
public class ANumberThatOnlyAppearsOnce {
public static void main(String[] args) {
System.out.println(singleNumber(new int[]{2,2,1}));
}
public static int singleNumber(int[] nums) {
int x = 0;
// 1. 遍历 nums 执行异或运算
for (int num : nums)
x ^= num;
// 2. 返回出现一次的数字 x
return x;
}
}
2.只出现一次的数字II
public class ANumberThatOnlyAppearsOnceII {
public static void main(String[] args) {
System.out.println(singleNumber(new int[]{2, 2, 3, 2}));
}
public static int singleNumber(int[] nums) {
int a = 0, b = 0;
for (int x : nums) {
int tmpA = a;
a = (a ^ x) & (a | b);
b = (b ^ x) & ~tmpA;
}
return b;
}
}
3.零钱兑换
public class Change {
public static void main(String[] args) {
System.out.println(coinChange(new int[]{1, 2, 5}, 11));
}
public static int coinChange(int[] coins, int amount) {
// 给 0 占位
int[] dp = new int[amount + 1];
// 注意:因为要比较的是最小值,这个不可能的值就得赋值成为一个最大值
Arrays.fill(dp, amount + 1);
// 理解 dp[0] = 0 的合理性,单独一枚硬币如果能够凑出面值,符合最优子结构
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (i - coin >= 0 && dp[i - coin] != amount + 1) {
dp[i] = Math.min(dp[i], 1 + dp[i - coin]);
}
}
}
if (dp[amount] == amount + 1) {
dp[amount] = -1;
}
return dp[amount];
}
}
4.删除链表的倒数第N个节点
public class ListNodeRemoveNthFromEnd {
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode hair = new ListNode();
hair.next = head;
ListNode p = head, q;
int i;
for (i = 0; p != null && i < n; i++, p = p.next) ;
if(p==null&&i<n){
return head;
}
q=hair;
while(p!=null){
p=p.next;
q=q.next;
}
q.next=q.next.next;
return hair.next;
}
}
5.两两交换链表中的节点
public class PairwiseSwapNodesInALinkedList {
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public static ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode next = head.next;
head.next = swapPairs(next.next);
next.next = head;
return next;
}
}
6.移除链表元素
public class RemovesALinkedListElement {
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode removeElements(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
if(head.val==val){
return head.next;
}else{
return head;
}
}
}
7.反转链表
public class ReverseLinkedList {
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public static ListNode reverseList(ListNode head) {
ListNode cur = head, pre = null;
while(cur != null) {
// 暂存后继节点 cur.next
ListNode tmp = cur.next;
// 修改 next 引用指向
cur.next = pre;
// pre 暂存 cur
pre = cur;
// cur 访问下一节点
cur = tmp;
}
return pre;
}
}