Leetcode: Array

Source: Internet
Author: User

1. Longest-consecutive-sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
GIVEN[100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.

Your algorithm should run in O (n) complexity.

Given an array of shapes, the longest sequential sequence is obtained. For example, the array [100,4,200,1,3,2], the longest continuous sequence length is [1,2,3,4], the length is 4. Requires a time complexity of O (n).

Sort words to at least O (nlgn) complexity. O (n) complexity, currently only found using hash to solve the scheme, add, remove, contains and other methods of the complexity is O (1), so two times the operation of the traversal is O (n).

 Public Static intLongestconsecutive (int[] num) {    //if array is empty, return 0    if(Num.length = = 0) {        return0; } Set<Integer> set =NewHashset<integer>(); intmax = 1;  for(inte:num) Set.add (e);  for(inte:num) {        intleft = E-1; intright = e + 1; intCount = 1;  while(Set.contains (left)) {count++;            Set.remove (left); Left--; }          while(Set.contains (right)) {count++;            Set.remove (right); Right++; } Max=Math.max (count, Max); }     returnMax;}
View Code

2. Surrounded-regions

Given a 2D board containing ' x ' and ' O ', capture all regions surrounded by ' x '.

A region was captured by flipping all ' O ' s into the ' X ' s in this surrounded region.

For example,

x x x xx o o xx x o xx o x x

After running your function, the board should is:

x x x xx x x xx x x xx O x x

A typical BFS problem. Traverse each character, if "O", start the BFS traversal from the current character, if the surrounding is also "O" to join the current traversal of the queue, know to traverse all adjacent "O", at the same time, to determine whether each o is surrounded, only by an O is not surrounded, The currently traversed set of O is not surrounded, because these o are all connected.

Of course, this problem can also be used with DFS, only the test data is too large, submitted StackOverflow.

1) BFS Breadth First Search

 Public classSolution {//Use a queue to do BFS    Privatequeue<integer> queue =NewLinkedlist<integer>();  Public voidSolveChar[] board) {        if(board = =NULL|| Board.length = = 0)            return; intm =board.length; intn = board[0].length; //merge O ' s on left & right boarder         for(inti = 0; I < m; i++) {            if(board[i][0] = = ' O ') {BFS (board, I,0); }             if(board[i][n-1] = = ' O ') {BFS (board, I, N-1); }        }         //Merge O ' s on top & bottom boarder         for(intj = 0; J < N; J + +) {            if(Board[0][j] = = ' O ') {BFS (board,0, J); }             if(Board[m-1][j] = = ' O ') {BFS (board, M-1, J); }        }         //Process the Board         for(inti = 0; I < m; i++) {             for(intj = 0; J < N; J + +) {                if(Board[i][j] = = ' O ') {Board[i][j]= ' X '; } Else if(Board[i][j] = = ' # ') {Board[i][j]= ' O '; }            }        }    }     Private voidBFsChar[] board,intIintj) {intn = board[0].length; //fill Current first and then its neighborsFillcell (board, I, J);  while(!Queue.isempty ()) {            intCur =Queue.poll (); intx = cur/N; inty = cur%N; Fillcell (board, X-1, y); Fillcell (board, X+ 1, y); Fillcell (board, X, y-1); Fillcell (board, X, y+ 1); }    }     Private voidFillcell (Char[] board,intIintj) {intm =board.length; intn = board[0].length; if(I < 0 | | I >= m | | J < 0 | | J >= N | | board[i][j]! = ' O ')            return; //Add current cell is the queue & then process it neighbors in BFSQueue.offer (i * n +j); BOARD[I][J]= ' # ';//use # to identify the O to keep    }}
View Code

2) Dfs Depth first search

 Public voidSolveChar[] board) {    if(board = =NULL|| Board.length==0)         return; intm =board.length; intn = board[0].length; //merge O ' s on left & right boarder     for(inti=0;i<m;i++){        if(board[i][0] = = ' O ') {Merge (board, I,0); }         if(board[i][n-1] = = ' O ') {Merge (board, I,n-1); }    }     //Merge O ' s on top & bottom boarder     for(intj=0; j<n; J + +){         if(Board[0][j] = = ' O ') {Merge (board,0, J); }         if(Board[m-1][j] = = ' O ') {Merge (board, M-1, J); }    }     //Process the Board     for(inti=0;i<m;i++){         for(intj=0; j<n; J + +){            if(Board[i][j] = = ' O ') {Board[i][j]= ' X '; }Else if(Board[i][j] = = ' # ') {Board[i][j]= ' O '; }        }    }}  Public voidMergeChar[] board,intIintj) {    if(i<0 | | i>=board.length | | j<0 | | j>=board[0].length)return; if(board[i][j]! = ' O ')        return; BOARD[I][J]= ' # '; //recursive implementation of depth-first searchMerge (board, i-1, J); Merge (board, I+1, J); Merge (board, I, J-1); Merge (board, I, J+1);}
View Code

Leetcode: Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.