Simple Old but concise Java solution and easy to understand using hashmap


  • 0
    P

    boolean r1=false;
    if(matrix.length==0)
    { r1=false;
    return r1;
    }
    else if(matrix.length>0)
    {
    int row=matrix.length;
    int col=matrix[0].length;
    HashMap<Integer,Integer>hash=new HashMap<Integer,Integer>();
    for(int x=0;x<row;x++)
    {
    for(int y=0;y<col;y++)
    {
    if(hash.containsKey(matrix[x][y]))
    {
    hash.put((int)matrix[x][y],hash.get(matrix[x][y])+1);
    }
    else
    {
    hash.put((int)matrix[x][y],1);
    }
    }
    }
    if(hash.containsKey(target))
    {
    r1=true;
    }
    else
    r1=false;
    return r1;
    }
    return r1;


Log in to reply
 

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