摘要:
48. Rotate Image 先按对角线对称图形,再水平对折。 class Solution { public void rotate(int[][] matrix) { //1.transpose for(int i = 0; i < matrix.length; i++){ for(int 阅读全文
摘要:
62. Unique Paths 方法一: 二位数组 而这道题是每次可以向下走或者向右走,求到达最右下角的所有不同走法的个数。那么跟爬梯子问题一样,需要用动态规划 Dynamic Programming 来解,可以维护一个二维数组 dp,其中 dp[i][j] 表示到当前位置不同的走法的个数,然后可 阅读全文
摘要:
389. Find the Difference ^ (按位异或): 参加运算的两个数,如果两个相应位为“异”(值不同),则该位结果为1,否则为0。 抵消掉相同的位,剩下的就是多余的位。 class Solution { public char findTheDifference(String s, 阅读全文
摘要:
78. Subsets While iterating through all numbers, for each new number, we can either pick it or not pick it1, if pick, just add current number to every 阅读全文
摘要:
82. Remove Duplicates from Sorted List II 跳过重复节点,返回head。 class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null || head.ne 阅读全文
摘要:
328. Odd Even Linked List “穿针引线” 1. odd指针穿奇数位 2.even指针穿偶数位 3.用evenHead记录第一个偶数位节点,最后用odd连接evenHead 注意判断空节点以及一个节点的情况。 class Solution { public ListNode o 阅读全文
只有注册用户登录后才能阅读该文。 阅读全文
摘要:
278. First Bad Version 二分法,如果isBadVersion返回true则坏版本在左边,right = mid,否则 left = mid + 1。 注意溢出问题 left+(right - left) /2 最后return left right均可 /* The isBad 阅读全文
摘要:
344. Reverse String 解法一(暴力法): 直接从两头往中间走,同时交换两边的字符即可 首位对调位置。 class Solution { public void reverseString(char[] s) { int tail = s.length-1; for(int i = 阅读全文
摘要:
387. First Unique Character in a String 用一个freq表建立每个字符和其出现次数的映射,然后按顺序遍历字符串,找到第一个出现次数为1的字符,返回其位置即可,参见代码如下: 注意 383. Ransom Note 同理,用freq表记录magazine的每一个字 阅读全文