Posts

Showing posts with the label Arrays and Strings

remove the duplicate character in a string

/*** remove the duplicate in an unsorted array and return its new length of array. adapt to any unsorted array with the template table of range table. ***/ int findIdx( string s , unordered_map < int , bool > wmap , int index ) {        while ( index < s .size()) {               if (! wmap [ s [ index ]]) return index ;               index ++;        }        return index ; } int removedup( string str ) {        if ( str .size() < 2) return str .size();        unordered_map < int , bool > wordmap;        for ( int i=0; i<256; i++) {               wor...

Trapping Rain Water

Image
Trapping Rain Water AC Rate: 165/540 My Submissions Given  n  non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given  [0,1,0,2,1,0,1,3,2,1,2,1] , return  6 . Algorithm: scan from left to right get the leftMostHeight scan from right to left get the rightMost Height at the pos i, if(A[i] < min(left, right)) trap[i] = min(left, right) – A[i]; class Solution { public :        int trap( int A [], int n ) {               // Start typing your C/C++ solution below               // DO NOT write int main() function               if ( n < 3) return 0;        ...

Rotate Matrix

#include <iostream> using namespace std; #define N 4 void rotate90( int (* a )[N], int st , int ed ) {        if ( st >= ed ) return ;        int tmp;        for ( int i = 0 ; i < ed - st ; i++ ) { // index start from zero               // Note the rotate order               tmp = a [ st ][i+ st ];               a [ st ][i+ st ] = a [ ed -i][ st ];               a [ ed -i][ st ] = a [ ed ][ ed -i];               a [ ed ][ ed -i] = a [i+ st ][ ed ];               a [i+ st ][ ed ] = tmp;      ...

Remove Duplicates from Sorted Array

/* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A = [1,1,2], Your function should return length = 2, and A is now [1,2]. */ class Solution { public :        int findnext( int A [], int n , int st ) {               int id = st +1;               while (id < n ) {                      if ( A [ st ] != A [id]) break ;                      id++;            ...

Sliding Window Maximum

/*** A long array A[] is given to you. There is a sliding window of size w which is moving from the very left of the array to the very right. You can only see the w numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: The array is [1 3 -1 -3 5 3 6 7], and w is 3. Window position                Max ---------------               ----- [1  3  -1] -3  5  3  6  7       3  1 [3  -1  -3] 5  3  6  7       3  1  3 [-1  -3  5] 3  6  7       5  1  3  -1 [-3  5  3] 6  7       5  1  3  -1  -3 [5  3  6] 7 ...