本周算法总结

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;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值