File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=481 lang=java
3+ *
4+ * [481] 神奇字符串
5+ */
6+
7+ // @lc code=start
8+ class Solution {
9+ public int magicalString (int n ) {
10+ if (n <= 3 ) {
11+ return 1 ;
12+ }
13+ int [] ls = new int [n ];
14+ ls [2 ] = 2 ;
15+ int i = 2 ;
16+ int j = 3 ;
17+ boolean isOne = true ;
18+ int sum = 1 ;
19+ while (j < n ) {
20+ for (int k = 0 ; j < n && k < ls [i ]; k ++) {
21+ if (isOne ) {
22+ sum ++;
23+ }
24+ ls [j ++] = isOne ? 1 : 2 ;
25+ }
26+ isOne = !isOne ;
27+ i ++;
28+ }
29+ return sum ;
30+ }
31+ }
32+ // @lc code=end
33+
Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ /*
5+ * @lc app=leetcode.cn id=594 lang=java
6+ *
7+ * [594] 最长和谐子序列
8+ */
9+
10+ // @lc code=start
11+ class Solution {
12+ public int findLHS (int [] nums ) {
13+ Map <Integer , Integer > ns = new HashMap <>();
14+ for (Integer n : nums ) {
15+ if (ns .containsKey (n )) {
16+ ns .put (n , ns .get (n ) + 1 );
17+ } else {
18+ ns .put (n , 1 );
19+ }
20+ }
21+
22+ int sum = 0 ;
23+ for (Integer i : ns .keySet ()) {
24+ if (ns .containsKey (i + 1 )) {
25+ int j = ns .get (i ) + ns .get (i + 1 );
26+ if (j > sum ) {
27+ sum = j ;
28+ }
29+ }
30+ }
31+
32+ return sum ;
33+ }
34+ }
35+ // @lc code=end
36+
You can’t perform that action at this time.
0 commit comments