Skip to content

Commit 79e9778

Browse files
author
luojing
committed
add 0016 solution
1 parent ed550ec commit 79e9778

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.blankj.easy._16_11;
2+
3+
public class LJSolution {
4+
5+
/**
6+
* 此题可以简化为一道等差数列的问题
7+
* 找规律,k*s, (k-1)*s+l, (k-2)*s+2*l, (k-3)*s+3*l, (k-4)*s+4*l, ... , kl
8+
* 首项: ks, 公差: l-s, 返回从小到大的等差数列项的数列
9+
*/
10+
public int[] divingBoard(int shorter, int longer, int k) {
11+
if (k == 0) return new int[0];
12+
if (shorter == longer) return new int[] { shorter * k};
13+
int[] ans = new int[ k + 1];
14+
int st = k * shorter;
15+
int delta = longer - shorter;
16+
for (int i = 0; i <= k ; i++) {
17+
ans[i] = st + i * delta;
18+
}
19+
return ans;
20+
}
21+
22+
public static void main(String[] args) {
23+
LJSolution ljSolution = new LJSolution();
24+
System.out.println(Arrays.toString(ljSolution.divingBoard(1,2,3)));
25+
}
26+
}

0 commit comments

Comments
 (0)