Skip to content

Commit 072cb70

Browse files
committed
add leetcode: offer-14.1-57.2-62
1 parent f40ff8f commit 072cb70

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

Algorithm/src/acwing/basic/chapter2/_835TrieStringStatistics.java

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
* 1
4040
*/
4141
public class _835TrieStringStatistics {
42+
//==========================Trie字典树============================
43+
4244
//因为字符串长度是10^5,所以N是这么多而不是2*10^4.
4345
public static int N = 100010;
4446
// trie树数组:第一维表示共有多长的结点,第二维表示每一个结点有多少种类型,这里英文26种

Algorithm/src/offer/_14_1CutRope.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-16-23:29
6+
* @Function Description :14.1 剪绳子
7+
*/
8+
public class _14_1CutRope {
9+
//==========================数学============================
10+
class Solution {
11+
// 尽可能将绳子以长度 33 等分为多段时,乘积最大.
12+
public int cuttingRope(int n) {
13+
if( n <= 3 ) return n - 1;
14+
// a:n/3的整数部分 b: n/3的余数
15+
int a = n / 3 , b = n % 3;
16+
// 正好三等分
17+
if( b == 0 ) return (int)Math.pow( 3 , a );
18+
if( b == 1 ) return (int)Math.pow( 3 , a - 1 ) * 4;
19+
// b=2
20+
return (int) Math.pow( 3 , a ) * 2 ;
21+
}
22+
}
23+
24+
25+
//==========================贪心============================
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package offer;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author : CodeWater
8+
* @create :2022-07-16-23:43
9+
* @Function Description :57.2 和为s的连续正数序列
10+
*/
11+
public class _57_2AndTheContinuousPositiveNumberSequenceOfSAndS {
12+
//==========================数学推导============================
13+
class Solution {
14+
public int[][] findContinuousSequence(int target) {
15+
int i = 1;
16+
double j = 2.0;
17+
List<int[]> res = new ArrayList<>();
18+
while( i < j ){
19+
// 推导出的公式
20+
j = (-1 + Math.sqrt( 1 + 4 * (2 * target +(long)i * i - i))) / 2 ;
21+
if( i < j && j == (int) j ){
22+
int[] ans = new int[(int) j - i + 1];
23+
for( int k = i ; k <= (int)j ; k++ )
24+
ans[k - i] = k;
25+
res.add(ans);
26+
}
27+
i++;
28+
}
29+
return res.toArray( new int[0][] );
30+
}
31+
}
32+
33+
//==========================双指针============================
34+
class Solution {
35+
public int[][] findContinuousSequence(int target) {
36+
int i = 1 , j = 2 , s = 3;
37+
List<int[]> res = new ArrayList<>();
38+
while( i < j ){
39+
if( s == target ){
40+
int[] ans = new int[j - i + 1];
41+
// 找到一组解(i , j)
42+
for( int k = i ; k <= j ; k++ )
43+
// k-i让下标回归从0开始
44+
ans[k - i] = k;
45+
res.add( ans );
46+
}
47+
if( s >= target ){
48+
s -= i;
49+
i++;
50+
}else{
51+
j++;
52+
s += j;
53+
}
54+
}
55+
56+
return res.toArray( new int[0][] );
57+
}
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-17-0:00
6+
* @Function Description :62.圆圈中最后剩下的数字
7+
*/
8+
public class _62TheLastNumberLeftInTheCircle {
9+
class Solution {
10+
public int lastRemaining(int n, int m) {
11+
// dp问题,开数组,但本题每个状态都依赖前一个,所以直接用过变量x代替
12+
// 初始dp[1] = 0 ;
13+
int x = 0;
14+
for( int i = 2 ; i <= n ; i++ )
15+
x = ( x + m ) % i ;
16+
return x ;
17+
}
18+
}
19+
}
Binary file not shown.

0 commit comments

Comments
 (0)