lcpBi110

第一题取整购买后的账户余额

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int accountBalanceAfterPurchase(int purchaseAmount) {
if (purchaseAmount == 100)
return 0;
if (purchaseAmount < 5)
return 100;
if (purchaseAmount < 10)
return 90;
int round = 0, min = 100;
for (int i = 10; i <= 100; i += 10) {
if (Math.abs(i - purchaseAmount) <= min) {
round = i;
min = Math.abs(i - purchaseAmount);
}
}
return 100 - round;
}
}

第二题在链表中插入最大公约数

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
/**
* 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 insertGreatestCommonDivisors(ListNode head) {
ListNode cur = head;
while (cur.next != null) {
ListNode next = cur.next;
int val = calc(cur.val, next.val);
ListNode x = new ListNode(val, next);
cur.next = x;
cur = next;
}
return head;
}

private int calc(int a, int b) {
int min = Math.min(a, b);
int res = 1;
for (int i = 1; i <= min; ++i) {
if (a % i == 0 && b % i == 0)
res = i;
}
return res;
}
}

第三题使循环数组所有元素相等的最少秒数

1

第四题使数组和小于等于 x 的最少时间

1

image-20230806151531856

lcpBi110
https://leopol1d.github.io/2023/08/05/lcpBi110/
作者
Leopold
发布于
2023年8月5日
许可协议