File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class FirstUniqChar {
2+
3+ public int firstUniqChar (String s ) {
4+ if (null == s || s .length () == 0 ) {
5+ return -1 ;
6+ }
7+ int length = s .length ();
8+ int [] arr = new int [26 ];
9+ char [] array = s .toCharArray ();
10+ for (char c : array ) {
11+ arr [c - 'a' ]++;
12+ }
13+
14+ for (int i = 0 ; i < length ; i ++) {
15+ if (arr [array [i ] - 'a' ] == 1 ) {
16+ return i ;
17+ }
18+ }
19+
20+ return -1 ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1- 学习笔记
1+ 学习笔记
2+
3+ dp[ i] [ j ] = blocked ? 0 : dp[ i−1,j] +dp[ i,j−1]
4+
Original file line number Diff line number Diff line change 1+ public class NumDecodings {
2+
3+ public int numDecodings (String s ) {
4+ if (s == null || s .length () == 0 ) {
5+ return 0 ;
6+ }
7+ int len = s .length ();
8+ int [] dp = new int [len + 1 ];
9+ dp [len ] = 1 ;
10+ if (s .charAt (len - 1 ) == '0' ) {
11+ dp [len - 1 ] = 0 ;
12+ } else {
13+ dp [len - 1 ] = 1 ;
14+ }
15+ for (int i = len - 2 ; i >= 0 ; i --) {
16+ if (s .charAt (i ) == '0' ) {
17+ dp [i ] = 0 ;
18+ continue ;
19+ }
20+ if ((s .charAt (i ) - '0' ) * 10 + (s .charAt (i + 1 ) - '0' ) <= 26 ) {
21+ dp [i ] = dp [i + 1 ] + dp [i + 2 ];
22+ } else {
23+ dp [i ] = dp [i + 1 ];
24+ }
25+ }
26+ return dp [0 ];
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments