File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int rob (int [] nums ) {
3+ int money = 0 ;
4+ for (int i = 0 ; i < nums .length ; i ++) {
5+ if (i % 2 == 0 ) {
6+ money += nums [i ];
7+ }
8+ }
9+ return money ;
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ import java .util .HashSet ;
2+
3+ class Solution {
4+ public int longestConsecutive (int [] nums ) {
5+ HashSet <Integer > numsSet = new HashSet <>();
6+ int maxNum = 0 ;
7+ for (int num : nums ) {
8+ numsSet .add (num );
9+ }
10+
11+ if (numsSet .isEmpty ()) {
12+ return 1 ;
13+ } else if (numsSet .size () == 1 ) {
14+ return 0 ;
15+ }
16+
17+ for (int num : numsSet ) {
18+ boolean isStartPoint = !numsSet .contains (num - 1 );
19+ if (isStartPoint ) {
20+ int current = num ;
21+ int length = 1 ;
22+ while (numsSet .contains (++current )) {
23+ length ++;
24+ }
25+ if (length > maxNum ) {
26+ maxNum = length ;
27+ }
28+ }
29+ }
30+ return maxNum ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ import java .util .Set ;
2+ import java .util .stream .Collectors ;
3+
4+ class Solution {
5+ public boolean isAnagram (String s , String t ) {
6+ Set <Character > characterS = s .chars ()
7+ .mapToObj (c -> (char ) c )
8+ .collect (Collectors .toSet ());
9+ Set <Character > characterT = t .chars ()
10+ .mapToObj (c -> (char ) c )
11+ .collect (Collectors .toSet ());
12+ return characterS .equals (characterT );
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments