---
# System prepended metadata

title: Untitled

---

**109-2 程式設計III 寒假作業**

344. Reverse String![](https://i.imgur.com/IRedJ9J.png)
![](https://i.imgur.com/2drGyhC.png)

```
void reverseString(char* s, int sSize){
    int i,j;
    char tem;
    for(i=0,j=sSize-1;i<j;i++,j--){
        tem=s[i];
        s[i]=s[j];
        s[j]=tem;
    }
}
```
7. Reverse Integer
![](https://i.imgur.com/cnYFUr8.png)
![](https://i.imgur.com/syAJwX3.png)
```
int reverse(int x){
    long a=0;
    while(x!=0){
        a=a*10+x%10;
        x=x/10;
    }
    if(a>=-INT_MAX &&a<=INT_MAX){
        return a;
    }
    return 0;
}
```
13. Roman to Integer
![](https://i.imgur.com/g5GwPZW.png)
![](https://i.imgur.com/W8cHe3E.png)
```
int romanToInt(char * s)
{
    int result = 0;
    int i = -1;
    while(s[++i])
    {
        if (s[i] == 'I')
            result += 1;
        else if (s[i] == 'V' && i != 0 && s[i-1] == 'I')
            result += 3;
        else if (s[i] == 'V')
            result += 5;
        else if (s[i] == 'X' && i != 0 && s[i-1] == 'I')
            result += 8;
        else if (s[i] == 'X')
            result += 10;
        else if (s[i] == 'L' && i != 0 && s[i-1] == 'X')
            result += 30;
        else if (s[i] == 'L')
            result += 50;
        else if (s[i] == 'C' && i != 0 && s[i-1] == 'X')
            result += 80;
        else if (s[i] == 'C')
            result += 100;
        else if (s[i] == 'D' && i != 0 && s[i-1] == 'C')
            result += 300;
        else if (s[i] == 'D')
            result += 500;
        else if (s[i] == 'M' && i != 0 && s[i-1] == 'C')
            result += 800;
        else if (s[i] == 'M')
            result += 1000;
    }
    return result;
}
```
50. Pow(x, n)
![](https://i.imgur.com/IPVNfPL.png)
![](https://i.imgur.com/RRcVeUK.png)
```
double myPow(double x, int n){
    double y;
    y=pow(x,n);
    return y;
}
```
268. Missing Number
![](https://i.imgur.com/zy1V0xB.png)
![](https://i.imgur.com/llgQIqP.png)
```
int missingNumber(int* nums, int numsSize){
    int sum = numsSize * (numsSize + 1) / 2;
    int sum1 = 0;
    for (int i = 0; i < numsSize; i++)
        sum1 += nums[i];  
    return (sum - sum1);
}
```
509. Fibonacci Number
![](https://i.imgur.com/keKDsL4.png)
![](https://i.imgur.com/Db6D5vN.png)
```
int fib(int n){
    int num=0,num1=1,num2=0;
    if(n==1){
        return 1;    
    }
    
    while(n-1>0){
        num2=num+num1;
        num=num1;
        num1=num2;
        n--;
    }
    return num2;
}
```
709. To Lower Case
![](https://i.imgur.com/3NNaTrj.png)
![](https://i.imgur.com/uucDGbN.png)
```
char * toLowerCase(char * str){
    for(int i=0;str[i]!='\0';i++){
        if(str[i]>='A' && str[i]<='Z'){
            str[i]+=32;
        }
    }
    return str;
}
```
771. Jewels and Stones
![](https://i.imgur.com/LQQsVG1.png)
![](https://i.imgur.com/Jl0kDbA.png)
```
int numJewelsInStones(char * J, char * S){
int s=0;int i,j;
for (i=0;i<strlen(J);i++){
    for(j=0;j<strlen(S);j++){
        if(J[i]==S[j])
            s++;
        }
    }
return s;
}
```
905. Sort Array By Parity
![](https://i.imgur.com/cH2yZi8.png)
![](https://i.imgur.com/geZjc8G.png)
```
int* sortArrayByParity(int* A, int ASize, int* returnSize){
    int i=0,j=0,temp;
    for(i=0;i<ASize-1;i++) {
        for(j=i+1;j<ASize;j++){
            if(A[i]%2!=0 && A[j]%2==0){
                temp=A[i];  
                A[i]=A[j];
                A[j]=temp;            
            }   
        }
    }
    *returnSize=ASize;
    return A;
}
```
1137. N-th Tribonacci Number
![](https://i.imgur.com/f1QBniL.png)
![](https://i.imgur.com/dvn81pW.png)
```
int tribonacci(int n){
    int a[38]={0,1,1};
    int i=0;
    for(i=3;i<=n;i++){
        a[i]=a[i-1]+a[i-2]+a[i-3];
    }
    return a[n];
}
```
704. Binary Search
![](https://i.imgur.com/A8GrjDn.png)
![](https://i.imgur.com/DNNIuXr.png)
```
int tribonacci(int n){
    int a[38]={0,1,1};
    int i=0;
    for(i=3;i<=n;i++){
        a[i]=a[i-1]+a[i-2]+a[i-3];
    }
    return a[n];
}
```
1221. Split a String in Balanced Strings
![](https://i.imgur.com/qUD7V1W.png)
![](https://i.imgur.com/9u5EIHV.png)
```
int balancedStringSplit(char * s){
    int n=0;
    int answer=0;
    for(int i=0;i<strlen(s);i++){
        if(s[i]=='R'){
            n++;
        }
        else if(s[i]=='L'){
            n--;
        }
        if(n==0){
            answer+=1;
        }
    }
    return answer;
}
```
1678. Goal Parser Interpretation
![](https://i.imgur.com/55KaTXv.png)
![](https://i.imgur.com/42B91h9.png)

```
char * interpret(char * command){
    int len=strlen(command);
    int a=0;
    for(int i=0;i<strlen(command);){
        if(command[i]=='G'){
            command[a++]='G';
            i++;
        }
        else if(command[i]=='(' && command[i+1]==')'){
            command[a++]='o';
            i+=2;
        }
        else{
            command[a++]='a';
            command[a++]='l';
            i+=4;
        }
    }
    command[a]='\0';
    return command;
}
```
1512. Number of Good Pairs
![](https://i.imgur.com/oeKqfyc.png)
![](https://i.imgur.com/D8sKn73.png)

```
int numIdenticalPairs(int* nums, int numsSize){
    int degree=0;
    for(int i=0;i<numsSize;i++){
        for(int j=0;j<numsSize;j++){
            if(nums[i]==nums[j] && i<j){
                degree++;
            }
        }
    }
    return degree;
}
```
41. First Missing Positive
![](https://i.imgur.com/dGCZgBo.png)
![](https://i.imgur.com/aIHOrC3.png)

```
int firstMissingPositive(int* nums, int numsSize){
   int min = 1;
   int i=0;
    while(i< numsSize){
        if(nums[i] < 1){
            i++;
            continue;
        }
        if(nums[i] == min){
            min++;
            i=-1;
        }
        i++;
    }
    return min;
}
```
