File tree Expand file tree Collapse file tree 5 files changed +61
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun containsDuplicate (nums : IntArray ): Boolean {
3
+ return nums.toSet()
4
+ .size != nums.size
5
+ }
6
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn rob ( nums : Vec < i32 > ) -> i32 {
3
+ let mut dp = vec ! [ 0 ; nums. len( ) + 1 ] ;
4
+ dp[ 1 ] = nums[ 0 ] ;
5
+ for i in 2 ..dp. len ( ) {
6
+ dp[ i] = dp[ i - 1 ] . max ( dp[ i - 2 ] + nums[ i - 1 ] ) ;
7
+ }
8
+ return * dp. last ( )
9
+ . unwrap_or ( & 0 )
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ func longestConsecutive (nums []int ) int {
2
+ ans := 0
3
+ sort .Ints (nums )
4
+ i := 0
5
+ for i != len (nums ) {
6
+ j := i + 1
7
+ k := 0
8
+ for j != len (nums ) {
9
+ diff := nums [j ] - nums [j - 1 ]
10
+ if diff > 1 {
11
+ break
12
+ }
13
+ j ++
14
+ if diff == 0 {
15
+ k ++
16
+ }
17
+ }
18
+ ans = max (ans , j - i - k )
19
+ i = j
20
+ }
21
+ return ans
22
+ }
Original file line number Diff line number Diff line change
1
+ object Solution {
2
+ def topKFrequent (nums : Array [Int ], k : Int ): Array [Int ] = {
3
+ return nums.groupBy(identity)
4
+ .view.mapValues(_.size)
5
+ .toSeq
6
+ .sortBy(- _._2)
7
+ .take(k)
8
+ .map(_._1)
9
+ .toArray
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ func twoSum (nums []int , target int ) []int {
2
+ indices := make (map [int ]int )
3
+ for i , num := range nums {
4
+ j , ok := indices [target - num ]
5
+ if ok {
6
+ return []int {j , i }
7
+ }
8
+ indices [num ] = i
9
+ }
10
+ return []int {}
11
+ }
You can’t perform that action at this time.
0 commit comments