lcp360

方法一:暴力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int furthestDistanceFromOrigin(String moves) {
int n = moves.length();
int cntL = 0, cntR = 0;
for (int i = 0; i < n; ++i) {
char ch = moves.charAt(i);
if (ch == 'L')
++cntL;
else if (ch == 'R')
++cntR;
}
return Math.abs(cntL - cntR) + n - (cntL + cntR);
}
}

方法一:HashSet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public long minimumPossibleSum(int n, int target) {
long res = 0;
HashSet<Integer> set = new HashSet<>();
int cnt = 0, i = 1;
while (cnt < n) {
if (set.contains(target - i)) {
++i;
continue;
}
set.add(i);
res += i++;
cnt++;
}
return res;
}
}

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