给定一个无序的整数类型数组,求最长的连续元素序列的长度。 例如: 给出的数组为[1000, 4, 2000, 1, 3, 2], 最长的连续元素序列为[1, 2, 3, 4]. 返回这个序列的长度:4 你需要给出时间复杂度在O(n)之内的算法
示例1
输入
[1000, 4, 2000, 1, 3, 2]
输出
4
加载中...
import java.util.*; public class Solution { /** * * @param num int整型一维数组 * @return int整型 */ public int longestConsecutive (int[] num) { // write code here } }
class Solution { public: /** * * @param num int整型vector * @return int整型 */ int longestConsecutive(vector
& num) { // write code here } };
# # # @param num int整型一维数组 # @return int整型 # class Solution: def longestConsecutive(self , num ): # write code here
/** * * @param num int整型一维数组 * @return int整型 */ function longestConsecutive( num ) { // write code here } module.exports = { longestConsecutive : longestConsecutive };
# # # @param num int整型一维数组 # @return int整型 # class Solution: def longestConsecutive(self , num ): # write code here
package main /** * * @param num int整型一维数组 * @return int整型 */ func longestConsecutive( num []int ) int { // write code here }
[1000, 4, 2000, 1, 3, 2]
4