2015年7月31日 星期五

LEET code--Longest Common Prefix

Longest Common Prefix


Write a function to find the longest common prefix string amongst an array of strings.

找出一堆字串中
起始一起出現的值

s1: abaccc
s2: abccc
s3: abrrr

LCP=ab   三個都從ab開始


因為會有空字串  要注意
用char *ans 最好都要malloc 
strdup 會把你傳進去的字串 用malloc 一個空間把string複製過去回傳指標回來
感覺很好用!!!


char* longestCommonPrefix(char** strs, int strsSize) {
    char *ans="";
   
    int strindex=0,index=0;
    if(strsSize ==0)return ans;
    if(strsSize ==1)return strs[0];
    if(strs[0][index]=='\0')return ans;
   
    //ans[index]=strs[0][index];
    while(1){
        for( int i = 1 ; i < strsSize ; i++ )
      {
            if( strs[0][index] != strs[i][index] || strs[0][index]=='\0'){
               
                if(index>=1){
                    ans =strdup(strs[0]);
                    ans[index]='\0';
                }
                return ans;
            }
        }
        index++;
       // ans[index]=strs[0][index];
    }
   
}

C--double pointer cahr**

Dereferencing" a pointer means accessing the value the pointer points to. Assume the following declarations:
int a = 10;
int *p = &a;
Here's a hypothetical memory map of the two variables:
Item      Address      0x00  0x01  0x02  0x03
----      -------      ----  ----  ----  ----
   a      0x80001000   0x00  0x00  0x00  0x0A
   p      0x80001004   0x80  0x00  0x10  0x00
a contains the integer value 10. p contains the address of a (0x80001000). If we want to access the contents of a through p, we dereference p with the indirection operator *. Thus, the expression *p is equivalent to the expression a. If we wrote
 *p = 16;
that's the same as writing
  a = 16;
Here's a short snippet of code showing how to use an object of type char ** to create an array of strings:
#include 
#define N      20  // For this example, we will allocate 20 strings
#define LENGTH 10  // of 10 characters each (not counting 0 terminator)
...
char **arr = malloc(sizeof *arr * N); 
if (arr)
{
  size_t i;
  for (i = 0; i < N; i++)
  {
    arr[i] = malloc(sizeof *arr[i] * (LENGTH + 1)); 
    strcpy(arr[i], "          "); 
  }
}
Going through it line by line,
char **arr = malloc(sizeof *arr * N); 
allocates a block of N elements, each large enough to store a pointer to char (sizeof *arr == sizeof (char *) since type of *arr == char *), and assigns the resulting pointer value to arr. IOW, arr points to the first pointer to char, hence the type char **. Note that if you separated the declaration and the function call, it would look like
char **arr;
...
arr = malloc(sizeof *arr * N);
We want to assign the result of malloc to arr, not to what arr points to.
if (arr)
It's possible for malloc to fail, so we want to check the result before using it. In the event malloc fails it will return a NULL pointer value.
{
  size_t i;
  for (i = 0; i < N; i++)
  {
    arr[i] = malloc(sizeof *arr[i] * (LENGTH + 1));
For each character pointer arr[i], we allocate a block of memory large enough for LENGTH+1 elements, each large enough to hold a char value (sizeof *arr[i] == sizeof (char), since type of *arr[i] == char; note that sizeof (char) is always 1) and assign the result to arr[i].
Since we allocate each string with a separate malloc call, it's unlikely that they are contiguous in memory. Here's another memory map showing a possible result of the code above:

Item         Address        0x00  0x01  0x02  0x03
----         -------        ----  ----  ----  ----
 arr         0x80001000     0xA0  0xCC  0x00  0x00  
             ...
 arr[0]      0xA0CC0000     0xA0  0xCC  0x20  0x00     
 arr[1]      0xA0CC0004     0xA0  0xCC  0x20  0x40
 arr[2]      0xA0CC0008     0xA0  0xCC  0x21  0x28
             ...
 arr[19]     0xA0CC0014     0xA0  0xCC  0x23  0x10
             ...
 arr[0][0]   0xA0CC2000     ' '   ' '   ' '   ' '
 arr[0][4]   0xA0CC2004     ' '   ' '   ' '   ' '
 arr[0][8]   0xA0CC2008     ' '   ' '   0x00  0x??
             ...
 arr[1][0]   0xA0CC2040     ' '   ' '   ' '   ' '
 arr[1][4]   0xA0CC2044     ' '   ' '   ' '   ' '
 arr[1][8]   0xA0CC2048     ' '   ' '   0x00  0x??

-------------------------------------------------



Dereferencing a pointer means accessing the value the pointer points to. For example,
char c = 'c'; // This is a primitive value. You cannot dereference it.
char* p1 = &c; // A pointer to the address of c
char** p2 = &p1; // A pointer to the address of p1
/* Now, the following is true:
*p1 == c, i.e. dereferencing p1 allows us to read from/write to c.
*p2 == p1
**p2 == *(*p2) == *p1 = c - dereferencing p2 twice is c, too */
The reason why you use a pointer to c instead of c directly is that a pointer allows you to access more than 1 value. Take this example:
char[4] str;
char c0 = 'a', c1 = 'b', c3 = 'c', c4 = '\0';
str[0] = c0; str[1] = c1; str[2] = c2; str[3] = c3;
str = "abc"; // Same as the above line
Now suppose we need the second character. We could access it with c1. But as you can see, this notation is really cumbersome. Plus, if we read the string from a file instead of writing it, we'd have to do complicated things. Instead, we just write
str[1] /* or */ *(str+1)
Note that the first element has the index 0, the second 1 - that's why we're using 1 here. A char** turns this up to eleven - we have an array of an array of chars. Suppose we have such an array, let's call it input, and need to find out the length of all the strings in it. This is how we would do it:
int lensum(char **input) {
    int res = 0;
    while (*input) { // This loops as long as the value input points to is not 0.
        char* p = *input; // Make a copy of the value input currently points to
        while (*p != '\0') { // Loop while the copy does not point to a char '\0'
            res += 1; // We found a character
            p++; // Check next character in the next iteration
        }
        input++; // Check next string in the next iteration
    }
    return res;
}             ...

LEET code--Roman to Integer

Roman to Integer


Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.


一開始寫法

/*
I=1 V=5 X=10 L=50 C=100 D=500 M=1000
IV=4 IX=9 XL=40 XC=90 CD=400 CM=900
*/
int roman(char *s){
    if(*s=='I')return 1;
    if(*s=='V')return 5;
    if(*s=='X')return 10;
    if(*s=='L')return 50;
    if(*s=='C')return 100;
    if(*s=='D')return 500;
    if(*s=='M')return 1000;
}
int romanToInt(char* s) {
    if(*s=='\0')return 0;
    int ans=0, size=0;
    while(s[size]!='\0'){
        if( s[size+1]!='\0' ){
            if(s[size]=='I' && s[size+1]=='V' ){ans+=4;size+=2;}
            if(s[size]=='I' && s[size+1]=='X' ){ans+=9;size+=2;}
            if(s[size]=='X' && s[size+1]=='L' ){ans+=40;size+=2;}
            if(s[size]=='X' && s[size+1]=='C' ){ans+=90;size+=2;}
            if(s[size]=='C' && s[size+1]=='D' ){ans+=400;size+=2;}
            if(s[size]=='C' && s[size+1]=='M' ){ans+=900;size+=2;}
         
        }
        if(s[size]!='\0'){
            ans+=roman(s[size]);
            size++;
        }
    }
    return ans;
}


但是不會過

因為我這邊是用s[size] 是char的型態
不是char*  所以在傳到roman(char*)會壞掉
要嘛全部char*  或是全部改成char
但是這樣還是會錯 因為我沒考慮到XC會連續的情況
所以如果是XCXC
那第一次處理完XC 會直接以為是X  不會是XC
全部加else就好了

-----
/*
I=1 V=5 X=10 L=50 C=100 D=500 M=1000
IV=4 IX=9 XL=40 XC=90 CD=400 CM=900
*/
int roman(char s){
    if(s=='I')return 1;
    if(s=='V')return 5;
    if(s=='X')return 10;
    if(s=='L')return 50;
    if(s=='C')return 100;
    if(s=='D')return 500;
    if(s=='M')return 1000;
}
int romanToInt(char* s) {
    if(*s=='\0')return 0;
    int ans=0, size=0;
    while(s[size]!='\0'){
            if(s[size]=='I' && s[size+1]=='V' ){ans+=4;size+=2;}
            else if(s[size]=='I' && s[size+1]=='X' ){ans+=9;size+=2;}
            else if(s[size]=='X' && s[size+1]=='L' ){ans+=40;size+=2;}
            else if(s[size]=='X' && s[size+1]=='C' ){ans+=90;size+=2;}
            else if(s[size]=='C' && s[size+1]=='D' ){ans+=400;size+=2;}
            else if(s[size]=='C' && s[size+1]=='M' ){ans+=900;size+=2;}
            else {ans+=roman(s[size]); size++;}
    }
    return ans;
}

-----------
網路上更好的寫法
因為4跟9 都是 後面比前面大
所以檢查這個就知道是不是4或9

int romanToInt(char* s) {
    int N=strlen(s);
    int map[26]={0};
    map['I'-'A']=1;
    map['V'-'A']=5;
    map['X'-'A']=10;
    map['L'-'A']=50;
    map['C'-'A']=100;
    map['D'-'A']=500;
    map['M'-'A']=1000;
    // main loop
    int sum=0;
    for(int i=0;i// check IV, IX, XL, XC, CD, CM
       if(i!=N-1){ 
          int n=map[s[i]-'A'];
          int nn=map[s[i+1]-'A']; 
             if(n
               sum+=(nn-n);
               i++;
             }else{
               sum+=n;
        }
        }else
            sum+=map[s[i]-'A'];
    }
    return sum;
}

2015年7月30日 星期四

LEET code--Integer to Roman


Integer to Roman


Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


簡單  值觀就是直接暴力法了

麻煩的是
4 9 40 90 400 900
要另外處理


/*
I=1 V=5 X=10 L=50 C=100 D=500 M=1000
IV=4 IX=9 XL=40 XC=90 CD=400 CM=900
*/

char* intToRoman(int num) {
    int size=0;
    char *ans;
    int i=0;
    while(num!=0){
        if(num/1000){
            for( i=0;i                ans[size++]='M';
             
            }
            num-=1000*(i);
         
        }
        if(num/900){
            ans[size++]='C';
            ans[size++]='M';
            num-=900;
        }
        if(num/500){
            for( i=0;i                ans[size++]='D';  
            }
            num-=500*(i);;
        }
        if(num/400){
            ans[size++]='C';
            ans[size++]='D';
            num-=400;
        }
        if(num/100){
            for( i=0;i                ans[size++]='C';  
            }
            num-=100*(i);
        }
        if(num/90){
            ans[size++]='X';
            ans[size++]='C';
            num-=90;
        }
        if(num/50){
            for( i=0;i                ans[size++]='L';  
            }
            num-=50*(i);
        }
        if(num/40){
            ans[size++]='X';
            ans[size++]='L';
            num-=40;
        }
        if(num/10){
            for( i=0;i                ans[size++]='X';  
            }
            num-=10*(i);
        }
        if(num/9){
            ans[size++]='I';
            ans[size++]='X';
            num-=9;
        }
        if(num/5){
            for( i=0;i                ans[size++]='V';  
            }
            num-=5*(i);
        }
        if(num/4){
            ans[size++]='I';
            ans[size++]='V';
            num-=4;
        }
        if(num){
            for( i=0;i                ans[size++]='I';  
            }
            num-=1*(i);
        }
    }
    ans[size]='\0';
    return ans;
}

LEET code--Palindrome Number

Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

簡單  把之前寫好的反轉int直接拿來用

會發現不會過
原來負數都不算
是負的直接略過


int reverse(int x) {
    int ans=0;
    while(x!=0){
        if(ans > INT_MAX/10 || ans< INT_MIN/10 )return 0;
        ans*=10;
        ans+=x%10;
        x/=10;
    }
    return ans;
}

bool isPalindrome(int x) {
    if (x < 0) {
        return false;
    }
    return x==reverse(x) ? true:false;
}

LEET code-Reverse Integer



Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

可能會overflow
一開始的寫法想太複雜
還先去算長度
如果長度==10
在去作處理
但是根本只要到 9位數在判斷就好
因為要超過INT_MAX或是INT_MIN的尾數 在還沒反轉之前就會先overflow了
所以第9位在判斷就好






int reverse(int x) {
    int ans=0;
    while(x!=0){
        if(ans > INT_MAX/10 || ans< INT_MIN/10 )return 0;
        ans*=10;
        ans+=x%10;
        x/=10;
    }
    return ans;
}

2015年7月29日 星期三

LEET CODE-ZigZag Conversion

ZigZag Conversion

 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

一開始根本不懂她的規律到底是怎樣
查了一下原來是

 nRows = 3
0   4   8  ...
1 3 5 7 9
2   6   10
 nRows = 4
0     6       12 ...
1   5 7    11
2 4   8 10  
3     9





每一個row的跳躍剛好是 2*(nRows - 1)
所以算好就好了

原本一開始用char ans [size]
但是超過一次的測資會爆炸
char *ans=(char*)malloc(sizeof(size)*sizeof(char));
也會爆炸
乾脆不先要求記憶體
直接char *ans;  就再也沒有run time error.....
可能題目有限制記憶體大小?
(最下面更新了!!)

remin 是在row!=0 and row!=numRows-1 的狀態下多出來了

numRows=4的情況下
row1 會是 1 5 7 11
1->5:4
5->7:2
兩個加起來一定會是6
所以每次都會選擇要加哪個數字
後來想想好像多此一舉

可以改成
        ans[count]= s[index];
        index=index+jump;
        if( row!=0 || row!=numRows-1 ){
         ans[++count]= s[index]+2*(numRows-1-row);
}
     
        if( index >= size ){
            row++;
            index=row;
           // remin= ( row==0 || row==numRows-1 ? jump: 2*(numRows-1-row) ) ;
        }


PS:
char *s
可以用strlen算長度
我每次都忘記了....


----------------------------
SyntaxHighlighter   又不能用了FFFFFFFFFFF

char* convert(char* s, int numRows) {
    int size= strlen(s);
//printf("%d",size);
    if(numRows == 1 || size==1) return s;
    int jump = numRows *2 -2;
    //char *ans=(char*)malloc(sizeof(size)*sizeof(char));
    char *ans;
    //char ans[size];
    //memset(ans,'\0',size);
    //ans[size]='\0';
    int row=0, remin=2*(numRows-1),index=0;
    for(int count=0;count   
        ans[count]= s[index];
        index=index+remin;
        remin= ( row==0 || row==numRows-1 ? jump: jump-remin ) ;
        if(index >= size){
            row++;
            index=row;
            remin= ( row==0 || row==numRows-1 ? jump: 2*(numRows-1-row) ) ;
        }
     
    }
    ans[size]='\0';
    return ans;
}



-----------------------------
神人寫法:
簡單明瞭阿!!!!

char* convert(char* s, int numRows) { int n=strlen(s); char* a; int k=0; if(numRows==1 || n<=numRows)return s; for(int i=0;i { for(int j=i;j { a[k++]=s[j]; if(i!=0 && i!=numRows-1) { int t=j+2*(numRows-1)-2*i; if(t<n) a[k++]=s[t]; } } } a[k]='\0'; return a;
}


----------------------------------
我後來發現我哪邊寫錯了....
char *ans=(char*)malloc(size*sizeof(char)+1);
我之前把size也用sizeof.....

char* convert(char* s, int numRows) {
    int size=0;
    while(s[size] !='\0')size++;
    
    //int size= strlen(s);
//printf("%d",size);
    if(numRows == 1 || size==1) return s;
    int jump = numRows *2 -2;
    char *ans=(char*)malloc(size*sizeof(char)+1);
    //char *ans;
    //char ans[size];
    //memset(ans,'\0',size);
    //ans[size]='\0';
    int row=0, remin=2*(numRows-1),index=0;
    for(int count=0;count
      //  printf("row= %d,s[index]=%c,index=%d,remin=%d\n",row,s[index],index,remin);
        ans[count]= s[index];
        index=index+remin;
        remin= ( row==0 || row==numRows-1 ? jump: jump-remin ) ;
        if(index >= size){
            row++;
            index=row;
            remin= ( row==0 || row==numRows-1 ? jump: 2*(numRows-1-row) ) ; 
        }
        
    }
    ans[size]='\0';
    return ans;
    
    

}

C 筆記 char 用strlen

The array declaration char a[6]; requests that space for six characters be set aside, to be known by the name a. That is, there is a location named a at which six characters can sit. The pointer declaration char *p; on the other hand, requests a place which holds a pointer. The pointer is to be known by the name p, and can point to any char (or contiguous array of chars) anywhere.
The statements
 char a[] = "string";
 char *p = "string"; 
would result in data structures which could be represented like this:
     +---+---+---+---+---+---+----+
  a: | s | t | r | i | n | g | \0 |
     +---+---+---+---+---+---+----+
     +-----+     +---+---+---+---+---+---+---+ 
  p: |  *======>; | s | t | r | i | n | g |\0 |    
     +-----+     +---+---+---+---+---+---+---+ 
It is important to realize that a reference like x[3] generates different code depending on whether xis an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location a, move three elements past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location p, fetch the pointer value there, add three element sizes to the pointer, and finally fetch the character pointed to. In the example above, both a[3] and p[3] happen to be the character l, but the compiler gets there differently.

char *p = "string"; 
用strlen算長度,不要用sizeof
因為sizeof試算pointer長度


2015年7月28日 星期二

LEET code--Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

找出連續且每個字元都不一樣的數列
一開始用爆力法,但是會超過時間....
只好去翻discuss
有人很厲害的把數列enumerate
接著每次看到一個字元,就轉成int 就進一個ASC[255]的陣列
該陣列記錄每個ASC字元 上次存在的enumerate數字多少
如果重複就把該數字的前一個位置enumerate找出來 減掉之後就是最常的長度
EX:
A  B C E  D  F  G  H  I   J    A   Z Q   W  B
1  2  3 4  5   6  7  8   9  10 11 12 13 14 15
第11的時候,去ASC[255]發現重複A在第1個位置, 把1記錄下來,現在最常的長度就是11-1=10
在第15的時候,B重複 再2的位置-->15-2=13的連續長度

想到這個真的很猛


class DemoApp {
    int lengthOfLongestSubstring(char* s) {
    int asc[255];
    if(*s == '\0')return 0;
    memset(asc,0,sizeof(asc));
    char *pos=s;
    int repeat=0;
    int count=0;
    int length=0;
    while( (*pos) != '\0' ){
        if( asc[*pos]!=0 )repeat= (  asc[*pos] > repeat ?  asc[*pos]:repeat  );
       
        asc[ *pos]=++count;
        if((count- repeat)>length )length=count- repeat;
        pos++;
    }
    length = ( length==0 ? count:length );
    return length;
    //    give the sequence number of the s,  if find out a repeat-> remember the number
    //
   
}
}


LEET code -Add Two Numbers

Add Two Numbers

 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

一開始看不懂題目
範例的意思是 342+564=708  把她算出來
只是現在是反過來的
就只是簡單的指標運用
class DemoApp {
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    
   

   // if( l1 == NULL || l2 == NULL)return NULL;
    int incr=0;
    int sum=0;
    struct ListNode* point = (struct ListNode *)malloc(sizeof(struct ListNode));
    point->val=0;
    point->next=NULL;
    struct ListNode* ansRoot=point;
    while(l1 || l2 ||incr)
    {
        sum = ( l1!=NULL ?   l1->val:0)+(l2!=NULL ? l2-> val:0)+incr;
        incr = sum/10;
        sum%=10;
       // struct ListNode *temp = (struct ListNode*)malloc(sizeof(struct ListNode));
       // point->val=sum;
        point->next=(struct ListNode *)malloc(sizeof(struct ListNode));
        point = point->next;
        point->val=sum;
        point->next=NULL;
        
        if(l1)l1=l1->next;
        if(l2)l2=l2->next;
        
    }
    ansRoot=ansRoot->next;
    return ansRoot;
    
}
}


LEET code-Two Sum

Two Sum

 Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
反正就是找出哪兩個加起來會是target
又只會有一組  就直接找
概念是
先sort一遍  從小排到大
兩個指標left and right
從數列的最開頭跟最尾端開始
left=nums[0]
right=nums[numsSize-1]
如過left+right > target
因為已經排列過 所以可以確定是right太大
讓她往左一一格
left+right < target
就是left 太小   向右一格

找到答案放到ANS陣列
因為答案要求的是要沒經過排序過的  所以要把原始位置再找出來


PS: 這邊用SyntaxHighlighter 會奇怪字元出現,懶得找BUG


int compare(const void *arg1, const void *arg2) {
  int compare(const void *arg1, const void *arg2) {
  return  (*(int *)arg1 - *(int *)arg2);
}
int* twoSum(int* nums, int numsSize, int target) {

    
    
    int* ans= (int *)malloc(sizeof(int)*2);
    int* temp = (int *)malloc(sizeof(int)*numsSize);
    memcpy(temp,nums,numsSize*sizeof(int));
    qsort( (void*)temp, numsSize, sizeof(int) ,compare) ;
    int find=0;
    int left=0;
    int right=numsSize-1;
    while(left
        if(temp[left]+temp[right]   >target){
            right--;
        }else if(temp[left]+temp[right]< target){
            left++;
        }else {
            ans[0]=temp[left];
            ans[1]=temp[right];
           // ans[0]=10;
            break;
        }
        
    }
    //ans[0]=10;
    
    int flag=0;
    for(int i=0;i
        if(ans[0]==nums[i]){
            ans[0]=i+1;
            flag++;
        }
        else if(ans[1]==nums[i] ){
            ans[1]=i+1;
            flag++;
        }
        if(flag>=2){
            if(ans[0]>ans[1]){
                int temp=ans[1];
                ans[1]=ans[0];
                ans[0]=temp;
            }
            break;
        }
    }
    
    free(temp);
    return ans;
}



用SyntaxHighlighter 在google的 Blogger


當初網路上找一堆
人家教學怎麼用都不對
重於找到一個可以用的
記錄!!!

1.
去範本-->網誌即時狀態的編輯HTML

2.再 /head 的前一行 貼入以下的CODE
 
    


    }

3.
測試
 
 
 
    int workkkkk(){
        int yessssss=1;
    }


4.
結果

 
    int workkkkk(){
        int yessssss=1;
    }

LEET code--Median of Two Sorted Arrays

LEET code--Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

一開始直觀  就是先檢查有沒有一個數列完全比另外一個大,但是這樣做會讓CODE變長LOL

接著在去慢慢比較找中位數

這是比較笨的寫法,看到有人用更神的
還要再學習阿~



int checkoverlap(int* nums1, int nums1Size, int* nums2, int nums2Size){
    int flag=0;
    
    if(nums1[ nums1Size-1 ] < nums2[0] ){//nums1 is less than 2
        return 1;
    }else if(nums2[ nums2Size-1 ] < nums1[0]){//nums2 is less than 1
        return 2;
    }else {// overlap
        return 3;
    }
    
}
double case1(int* nums1, int nums1Size, int* nums2, int nums2Size){
    int medin=(nums1Size+nums2Size)/2 +1 ;//because the devide won't round up   5/2=2,6/2=3
    
    if( nums1Size < medin ){//inside the nums2
        if( (nums1Size+nums2Size)%2==0 ){
            return ( (medin-nums1Size ==1) ? (nums1[nums1Size-1]+nums2[0])/2.0 : (nums2[medin - nums1Size-1]+nums2[medin - nums1Size-2] )/2.0 );
                    //medin is in the first nums2: not in the first
        }else {
            return nums2[medin - nums1Size -1 ];
        }
                
    }else if( nums1Size > medin ){//inside the nums1
        return ( ((nums1Size+nums2Size)%2==0 ) ?  (nums1[medin -1]+nums1[medin -2] )/2.0 : nums1[medin  -1 ]  );
                //even: odd
    }else if( nums1Size == medin ){//medin is in the last number of nums1
        return ( (nums1Size+nums2Size)%2==0 ?  (nums1[nums1Size-1] + nums1[nums1Size-2]  )/2.0 : nums1[nums1Size-1] );
                //even, last2 number in nums1: last number in nums1
    }
}

double case2(int* nums1, int nums1Size, int* nums2, int nums2Size){
    int medin=(nums1Size+nums2Size)/2 +1 ;//because the devide won't round up   5/2=2,6/2=3
    if( nums2Size < medin ){//inside the nums1
        if( (nums1Size+nums2Size)%2==0 ){
            return ( (medin-nums2Size ==1) ? (nums2[nums2Size-1]+nums1[0])/2.0 : (nums1[medin - nums2Size-1]+nums1[medin - nums2Size-2] )/2.0 );
                    //medin is in the first nums2: not in the first
        }else {
            return nums1[medin - nums2Size -1 ];
        }
                
    }else if( nums2Size > medin ){//inside the nums2
        return ( ((nums1Size+nums2Size)%2==0 ) ?  (nums2[medin -1]+nums2[medin -2] )/2.0 : nums2[medin  -1 ]  );
                //even: odd
    }else if( nums2Size == medin ){//medin is in the last number of nums1
        return ( (nums1Size+nums2Size)%2==0 ?  (nums2[nums2Size-1] + nums2[nums2Size-2]  )/2.0 : nums2[nums2Size-1] );
                //even, last2 number in nums1: last number in nums1
    }
}


double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    if(nums1Size==0){
         if(nums2Size ==1)return nums2[0];
        return ( nums2Size%2==0 ? (nums2[nums2Size/2]+nums2[(nums2Size/2) - 1])/2.0  : nums2[nums2Size/2] );//even:odd
    }
    if(nums2Size==0){
        if(nums1Size ==1)return nums1[0];
        return ( nums1Size%2==0 ? (nums1[nums1Size/2]+nums1[nums1Size/2 - 1])/2.0  : nums1[nums1Size/2] );
    }
    //find the overlap
    int medin=(nums1Size+nums2Size)/2 +1 ;//because the devide won't round up   5/2=2,6/2=3
    int sum= nums1Size + nums2Size;
    int nums1P,nums2P,remin=0;
    int previous=0,current=0;
    
    switch ( checkoverlap( nums1,  nums1Size,  nums2,  nums2Size) ){
        case 1://nums1 < nums2
            return case1( nums1,  nums1Size,  nums2,  nums2Size);
        
            break;
        case 2://nums2 < nums1
            return case2( nums1,  nums1Size,  nums2,  nums2Size);
            break;
        case 3://overlap nums1  nums2
            
            nums1P=nums2P=1;
            //count how many number need to process
            remin=medin;
            while ( nums2P<=nums2Size || nums1P<=nums1Size ){
                if( nums1P<= nums1Size  && nums1[nums1P-1] <= nums2[nums2P -1] ){
                    previous = current;
                    current = nums1[nums1P-1];
                    nums1P++;
                    remin--;
                    
                    
                    //if( nums1P > nums1Size)remin--;
                }else if(  nums2P<=nums2Size && nums2[nums2P -1 ] < nums1[nums1P - 1] ){
                    previous = current ;
                    current = nums2[nums2P-1];
                    nums2P++;
                    remin--;
                    
                    
                    //if( nums2P > nums2Size)remin--;
                }else if( nums2P > nums2Size ){
                    previous = current;
                    current = nums1[nums1P-1];
                    nums1P++;
                    remin--;

                }else if( nums1P > nums1Size){
                    previous = current ;
                    current = nums2[nums2P-1];
                    nums2P++;
                    remin--;
                }
                if(remin<=0 ){
                    return ( ((nums1Size+nums2Size)%2==0 ) ?  (previous+ current)/2.0 :current);
                    break;
                }
            }
            
            
            break;
    }
    
}