share my java solution!


  • 0
    C
    public class Solution {
        public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
              if(seqs==null || seqs.size()==0) return false;
              
              int[]  num2index=new int[org.length+1];
              int n=org.length;
              for(int i=0;i<org.length;i++){
                  num2index[org[i]]=i;
              }
              
              boolean[] pairs=new boolean[org.length];
              Set<Integer> set=new HashSet<>();
              
              for(List<Integer> seq:seqs){
                  for(int i=0;i<seq.size();i++){
                      set.add(seq.get(i));
                      if(seq.get(i)>n||seq.get(i)<=0) return false;
                      
                      if(i>=1){
                          if(num2index[seq.get(i)]<=num2index[seq.get(i-1)]){
                                 return false;
                          }
                          
                          if(num2index[seq.get(i-1)]+1==num2index[seq.get(i)]){
                              pairs[num2index[seq.get(i-1)]]=true;
                          }
                      }
                  }
              }
              
              //I don't know who create this corner case which is meaningless
              if(n!=set.size()) return false;
              
              for(int i=0;i<=pairs.length-2;i++){
                  if(pairs[i]==false){
                      return false;
                  }
              }
              
              return true;
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.