跳转至

LeetCode 笔记

1. 两数之和

题目

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6
输出:[1,2]

示例 3:

输入:nums = [3,3], target = 6
输出:[0,1]

提示:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • 只会存在一个有效答案

进阶:你可以想出一个时间复杂度小于 \(O(n^{2})\) 的算法吗?

题解

正确题解1(2021年12月9日)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        Map<Integer, Integer> hashMap = new HashMap<>(len - 1);
        hashMap.put(nums[0] , 0);
        for(int i = 1; i < len; i++){
            int another = target - nums[i];
            if(hashMap.containsKey(another)){
                return new int[]{i , hashMap.get(another)};
            }
            hashMap.put(nums[i] , i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}

状态:通过,执行用时: 1 ms,内存消耗: 38.5 MB


2. 两数相加

题目

题解



3. 无重复字符的最长子串

题目

题解



4. 寻找两个正序数组的中位数

题目

题解



5. 最长回文子串

题目

题解



6. Z 字形变换

题目

题解



7. 整数反转

题目

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 \([−2^{31},  2^{31} − 1]\) ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)

示例 1:

输入:x = 123
输出:321

示例 2:

输入:x = -123
输出:-321

示例 3:

输入:x = 120
输出:21

示例 4:

输入:x = 0
输出:0

提示:

-2^31 <= x <= 2^31 - 1

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    public int reverse(int x) {
        int result = 0;
        while(x!=0){
            int num = x % 10;
            x /= 10;

            if(result < Integer.MIN_VALUE / 10 || result > Integer.MAX_VALUE / 10){
                return 0;
            }
            result = result * 10 + num;
        }
        return result;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 35.3 MB


8. 字符串转换整数 (atoi)

题目

题解


状态:通过,执行用时: ,内存消耗:


9. 回文数

题目

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。

示例 1:

输入:x = 121
输出:true

示例 2:

输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

示例 3:

输入:x = 10
输出:false
解释:从右向左读, 为 01 。因此它不是一个回文数。

示例 4:

输入:x = -101
输出:false

提示:

  • -2^31 <= x <= 2^31 - 1  

进阶:你能不将整数转为字符串来解决这个问题吗?

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0 || x % 10 == 0 && x != 0){
            return false;
        }

        int revertedNum = 0;
        while(x > revertedNum){
            revertedNum = revertedNum * 10 + x % 10;
            x /= 10;
        }

        return x == revertedNum || x == revertedNum / 10;
    }
}

状态:通过,执行用时: 4 ms,内存消耗: 37.6 MB


14. 最长公共前缀

题目

题解


状态:通过,执行用时: ,内存消耗:


19. 删除链表的倒数第 N 个结点

题目

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

示例 1:

20211107005005.jpg

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

示例 2:

输入:head = [1], n = 1
输出:[]

示例 3:

输入:head = [1,2], n = 1
输出:[1]

提示:

  • 链表中结点的数目为 sz
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz  

进阶:你能尝试使用一趟扫描实现吗?

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {

        // 获取链表的长度
        int len = 0;
        ListNode curr = head;
        while(curr != null){
            len++;
            curr = curr.next;
        }

        ListNode sample1 = new ListNode(0, head);
        ListNode sample2 = sample1;
        for (int i = 0; i < len - n; i++){
            sample2 = sample2.next;
        }
        sample2.next = sample2.next.next;
        ListNode sample3 = sample1.next;
        return sample3;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 36.1 MB


20. 有效的括号

题目

题解


状态:通过,执行用时: ,内存消耗:


21. 合并两个有序链表

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/merge-two-sorted-lists

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例 1:

20211107005004.jpg

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:[]

示例 3:

输入:l1 = [], l2 = [0]
输出:[0]

提示:

  • 两个链表的节点数目范围是 [0, 50]
  • -100 <= Node.val <= 100
  • l1l2 均按 非递减顺序 排列

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null){
            return list2;
        }else if(list2 == null){
            return list1;
        }else if(list1.val < list2.val){
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        }else{
            list2.next = mergeTwoLists(list1,list2.next);
            return list2;
        }
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 37.1 MB


67. 最长回文子串

题目

题解


状态:通过,执行用时: ,内存消耗:


206. 反转链表

题目

!!! quote "来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/reverse-linked-list "

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

20211107005001

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

20211107005002

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

提示:

  • 链表中节点的数目范围是 [0, 5000]
  • -5000 <= Node.val <= 5000

题解

20211107005003

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while(curr != null){
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 37.8 MB


217. 存在重复元素

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/contains-duplicate

给定一个整数数组,判断是否存在重复元素。

如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false

示例 1:

输入: [1,2,3,1]
输出: true

示例 2:

输入: [1,2,3,4]
输出: false

示例 3:

输入: [1,1,1,3,3,4,3,2,4,2]
输出: true

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int x : nums){
            if (!set.add(x)){
                return true;
            }
        }
        return false;
    }
}

状态:通过,执行用时: 5 ms,内存消耗: 51.5 MB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);
        for(int i = 0; i < nums.length - 1; i++){
            if(nums[i] == nums [i+1]){
                return true;
            }
        }
        return false;
    }
}

状态:通过,执行用时: 18 ms,内存消耗: 51 MB


234. 回文链表

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/palindrome-linked-list

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false

示例 1:

输入:head = [1,2,2,1]
输出:true

示例 2:

输入:head = [1,2]
输出:false

提示:

  • 链表中节点数目在范围 [1, 105]
  • 0 <= Node.val <= 9

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        List<Integer> vals = new ArrayList<Integer>();

        ListNode currNode = head;
        while(currNode != null){
            vals.add(currNode.val);
            currNode = currNode.next;
        }

        int front = 0;
        int back = vals.size() - 1;
        while(front < back){
            if(!vals.get(front).equals(vals.get(back))){
                return false;
            }
            front++;
            back--;
        }
        return true;
    }
}

状态:通过,执行用时: 8 ms,内存消耗: 50.7 MB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {

        ListNode fast = head, slow = head;
        ListNode cur = head, dummy = new ListNode();

        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            cur.next = dummy.next;
            dummy.next = cur;
            cur = slow;
        }

        if (fast != null) {
            slow = slow.next;
            }
        dummy = dummy.next;

        while(slow != null && dummy != null){
            if(slow.val != dummy.val){
                return false;
            }
            slow = slow.next;
            dummy = dummy.next;
        }
        return true;
    }
}

状态:通过,执行用时: 3 ms,内存消耗: 48 MB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
    public boolean isPalindrome(ListNode head) {
        // 单节点也默认为回文
        if (head == null || head.next == null)
            return true;

        ListNode slow = head, fast = head;
        // pre为slow的上一节点
        ListNode pre = null;
        // 快慢指针同时反转前半部分链表(破坏了原来链表结构)
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            // temp保存slow的下一节点,以免链表断裂
            ListNode temp = slow.next;
            // 反转链表
            slow.next = pre;
            // pre后移
            pre = slow;
            // slow后移
            slow = temp;
        }
        // 应对奇数情况[1,0,1]:当fast.next为null时,fast必为末尾节点
        if (fast != null)
            slow = slow.next;
        // prepre用于恢复原来链表结构
        ListNode prepre = slow;
        // 判断是否为回文
        while (pre != null && slow != null) {
            if (pre.val != slow.val)
                return false;
            slow = slow.next;
            // 不还原链表时,直接pre = pre.next;
            // 还原链表,上述while破坏了原有链表结构,建议最好复原
            // temp保存pre的下一节点,以免反转后的链表断裂
            ListNode temp = pre.next;
            // 恢复原来的.next
            pre.next = prepre;
            // prepre向前移
            prepre = pre;
            // pre前移
            pre = temp;
        }
        return true;
    }
}

作者melodyjerry
链接https://leetcode-cn.com/problems/palindrome-linked-list/solution/javakuai-man-zhi-zhen-fan-zhuan-qian-bu-4ryei/
来源力扣LeetCode

通过,执行用时: 3 ms,内存消耗: 47.7 MB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {

        ListNode fast = head, slow = head;

        // 快慢指针找中点
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        // 如果fast不为空,链表的长度是奇数
        if(fast != null){
            slow = slow.next;
        }

        // 反转后半部分链表
        slow = reverse(slow);

        fast = head;
        while(slow != null){
            if(fast.val != slow.val){
                return false;
            }
            fast = fast.next;
            slow = slow.next;
        }
        return true;
    }

    // 反转链表
    public ListNode reverse(ListNode head){
        ListNode prev = null;
        while(head != null){
            ListNode next = head.next;
            head.next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }
}

状态:通过,执行用时: 4 ms,内存消耗: 47.5 MB


475. 供暖器

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/heaters

冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。

在加热器的加热半径范围内的每个房屋都可以获得供暖。

现在,给出位于一条水平线上的房屋 houses 和供暖器 heaters 的位置,请你找出并返回可以覆盖所有房屋的最小加热半径。

说明:所有供暖器都遵循你的半径标准,加热的半径也一样。

示例 1:

输入: houses = [1,2,3], heaters = [2]
输出: 1
解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。

示例 2:

输入: houses = [1,2,3,4], heaters = [1,4]
输出: 1
解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。

示例 3:

输入:houses = [1,5], heaters = [2]
输出:3

提示:

  • 1 <= houses.length, heaters.length <= 3 * 104
  • 1 <= houses[i], heaters[i] <= 109

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int l = 0, r = (int) 1e9;
        while (l < r){
            int mid = l + r >> 1;
            if (check(houses, heaters, mid)) r = mid;
            else l = mid + 1;
        }
        return r;
    }
    boolean check(int[] houses, int[] heaters, int x) {
        int n = houses.length, m = heaters.length;
        for (int i = 0, j = 0; i < n; i++) {
            while (j < m && houses[i] > heaters[j] + x) j++;
            if (j < m && heaters[j] - x <= houses[i] && houses[i] <= heaters[j] + x) continue;
            return false;
        }
        return true;
    }
}

状态:通过,执行用时: 14 ms,内存消耗: 41.6 MB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        int res = 0;
        for(int i = 0; i < houses.length; ++i) {
            int min = Integer.MAX_VALUE;
            for (int j = 0; j < heaters.length; ++j) {
                min = Math.min(min, Math.abs(houses[i]- heaters[j]));
            }
            res = Math.max(res, min);
        }
        return res;
    }
}

状态:通过,执行用时: 1865 ms,内存消耗: 40.8 MB


1480. 一维数组的动态和

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/running-sum-of-1d-array/

给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i])

请返回 nums 的动态和。

示例 1:

输入:nums = [1,2,3,4]
输出:[1,3,6,10]
解释:动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。

示例 2:

输入:nums = [1,1,1,1,1]
输出:[1,2,3,4,5]
解释:动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1] 。

示例 3:

输入:nums = [3,1,2,10,1]
输出:[3,4,6,16,17]

提示:

  • 1 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6

题解

1
2
3
4
5
6
7
8
class Solution {
    public int[] runningSum(int[] nums) {
        for (int i = 1; i < nums.length; i++){
            nums[i] += nums[i-1];
        }
        return nums;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 38.6 MB


1518. 换酒问题

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/water-bottles

小区便利店正在促销,用 numExchange个空酒瓶可以兑换一瓶新酒。你购入了 numBottles 瓶酒。

如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的。

请你计算 最多 能喝到多少瓶酒。

示例 1:

输入:numBottles = 9, numExchange = 3
输出:13
解释:你可以用 3 个空酒瓶兑换 1 瓶酒。
所以最多能喝到 9 + 3 + 1 = 13 瓶酒。

示例 2:

输入:numBottles = 15, numExchange = 4
输出:19
解释:你可以用 4 个空酒瓶兑换 1 瓶酒。
所以最多能喝到 15 + 3 + 1 = 19 瓶酒。

示例 3:

输入:numBottles = 5, numExchange = 5
输出:6

示例 4:

输入:numBottles = 2, numExchange = 3
输出:2

提示:

  • 1 <= numBottles <= 100
  • 2 <= numExchange <= 100

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        int numYouGot = numBottles;
        int bottle = numBottles;
        while(bottle >= numExchange){
            bottle -= numExchange;
            ++numYouGot;
            ++bottle;
        }
        return numYouGot;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 35.3 MB

1
2
3
4
5
class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        return numBottles >= numExchange ? (numBottles - numExchange) / (numExchange - 1) + 1 + numBottles : numBottles;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 35.2 MB


1672. 最富有客户的资产总量

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/richest-customer-wealth

给你一个 m x n 的整数网格 accounts ,其中 accounts[i][j] 是第 i​​​​​​​​​​​​ 位客户在第 j 家银行托管的资产数量。返回最富有客户所拥有的 资产总量

客户的 资产总量 就是他们在各家银行托管的资产数量之和。最富有客户就是 资产总量 最大的客户。

示例 1:

20211107005006

输入:accounts = [[1,2,3],[3,2,1]]
输出:6
解释:
第 1 位客户的资产总量 = 1 + 2 + 3 = 6
第 2 位客户的资产总量 = 3 + 2 + 1 = 6
两位客户都是最富有的,资产总量都是 6 ,所以返回 6 。

示例 2:

20211107005007

输入:accounts = [[1,5],[7,3],[3,5]]
输出:10
解释:
第 1 位客户的资产总量 = 6
第 2 位客户的资产总量 = 10 
第 3 位客户的资产总量 = 8
第 2 位客户是最富有的,资产总量是 10

示例 3:

输入:accounts = [[2,8,7],[7,1,3],[1,9,5]]
输出:17

提示:

  • m == accounts.lengt
  • n == accounts[i].length
  • 1 <= m, n <= 50
  • 1 <= accounts[i][j] <= 100

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    public int maximumWealth(int[][] accounts) {
        int accountSumMax = 0;
        for (int i = 0; i < accounts.length; i++){
            int accountSum = 0;
            for (int j = 0; j < accounts[i].length; j++){
                accountSum += accounts[i][j];
            }
            accountSumMax = Math.max(accountSumMax, accountSum);
        }
        return accountSumMax;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 38 MB


1929. 数组串联

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/concatenation-of-array

给你一个长度为 n 的整数数组 nums 。请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < ni ,满足下述所有要求:

  • ans[i] == nums[i]
  • ans[i + n] == nums[i]
  • 具体而言,ans 由两个 nums 数组 串联 形成。

返回数组 ans

示例 1:

输入:nums = [1,2,1]
输出:[1,2,1,1,2,1]
解释:数组 ans 按下述方式形成:
- ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
- ans = [1,2,1,1,2,1]

示例 2:

输入:nums = [1,3,2,1]
输出:[1,3,2,1,1,3,2,1]
解释:数组 ans 按下述方式形成:
- ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
- ans = [1,3,2,1,1,3,2,1]

提示:

  • n == nums.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 1000

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int[] getConcatenation(int[] nums) {
        int[] ans = new int[2 * nums.length];
        for(int i = 0 ; i < nums.length; i++){
            ans[i] = nums[i];
            ans[i + nums.length] = nums[i];
        }
        return ans;
    }
}

状态:通过,执行用时: 1 ms,内存消耗: 39.2 MB

1
2
3
4
5
6
7
8
9
class Solution {
    public int[] getConcatenation(int[] nums) {
        int[] ans = new int[2 * nums.length];
        for(int i = 0 ; i < ans.length; i++){
            ans[i] = nums[i % nums.length];
        }
        return ans;
    }
}

状态:通过,执行用时: 1 ms,内存消耗: 39 MB


LCP 01. 猜数字

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/guess-numbers

小A 和 小B 在玩猜数字。小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜。他们一共进行三次这个游戏,请返回 小A 猜对了几次?

输入的 guess 数组为 小A 每次的猜测,answer 数组为 小B 每次的选择。guessanswer 的长度都等于3。

示例 1:

输入:guess = [1,2,3], answer = [1,2,3]
输出:3
解释:小A 每次都猜对了。

示例 2:

输入:guess = [2,2,3], answer = [3,2,1]
输出:1
解释:小A 只猜对了第二次。

限制:

  • guess 的长度 = 3
  • answer 的长度 = 3
  • guess 的元素取值为 {1, 2, 3} 之一。
  • answer 的元素取值为 {1, 2, 3} 之一。

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int game(int[] guess, int[] answer) {
        int numCorr = 0;
        for (int i = 0; i < guess.length; i ++){
            if (guess[i] == answer[i])
            numCorr++;
        }
        return numCorr;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 35.7 MB


LCP 06. 拿硬币

题目

来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/na-ying-bi

桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。

示例 1:

输入:[4,2,1]

输出:4

解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完。

示例 2:

输入:[2,3,10]

输出:8

限制:

  • 1 <= n <= 4
  • 1 <= coins[i] <= 10

题解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    public int minCount(int[] coins) {
        int numPick = 0;
        for(int i = 0; i < coins.length; i++){
            if(coins[i] % 2 == 0){
                numPick += (coins[i] - coins[i] % 2) / 2;
            } else {
                numPick += (coins[i] - coins[i] % 2) / 2 + 1;
            }
        }
        return numPick;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 35.7 MB

1
2
3
4
5
6
7
8
9
class Solution {
    public int minCount(int[] coins) {
        int numPick = 0;
        for(int coin : coins){
            numPick += (coin >> 1) + (coin & 1);
        }
        return numPick;
    }
}

状态:通过,执行用时: 0 ms,内存消耗: 35.9 MB

模板

题目

来源:力扣(LeetCode),链接:

题解

1

状态:通过,执行用时: ,内存消耗:

1

状态:通过,执行用时: ,内存消耗:

回到页面顶部