2023年12月28日 星期四

12/28 每日C 共同前綴 (使用strdup())

 

Easy
Topics
Companies

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

If there is no common prefix, return an empty string "".

 

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

 

Constraints:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters.
char* longestCommonPrefix(char** strs, int strsSize) {
    if (strsSize==0)
    return "";
    char *ptr=strdup(strs[0]);  // 使用strdup复制字符串
    printf("看看strdup()=%s",ptr);
    int i,c=0,j;

    for(i=1;i<strsSize;i++)
    {
        j=0;
        while(ptr[j]!='\0' && ptr[j]==strs[i][j] && strs[i][j] != '\0')
        {j++;}
        //迴圈停止表示結束或者遇到不同的
        ptr[j] = '\0';

        if (ptr[0]=='\0')
            {//表示沒有共同
                return "";
            }
    }
    return ptr;
   
}


!!!!!!!!注意 \0 要使用單引號


#介紹 strdup()
strdup()函數用於複製一個字符串,並回傳一個指向新字符串的指針, 這個函數會使用malloc()動態分配記憶體
,這意味著需要在使用完strdup返回的字符串後手動釋放內存

char *strdup(const char *s)

strdup 接受一个字符串 s 作为参数,返回一个指向新字符串的指针。如果内存分配失败,strdup 返回 NULL


標籤: ,

2023年12月27日 星期三

12/27 每日C 羅馬數字

Easy
Topics
Companies
Hint

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

 

Example 1:

Input: s = "III"
Output: 3
Explanation: III = 3.

Example 2:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 3:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

Constraints:

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

 #include <string.h>


int romanToInt(char* s) {
    int sum=0;
    int val[128]={0};
    val['I']=1;
    val['V']=5;
    val['X']=10;
    val['L']=50;
    val['C']=100;
    val['D']=500;
    val['M']=1000;

    //指向字串變數的s
    int i, len =strlen(s);
    printf("長度:%d \n",len);
    for (i=0;i+1<len;i++)
    {
       
        if(val[s[i]] >= val[s[i+1]])        
            sum+=val[s[i]];
        else
            sum -=val[s[i]];
       
    }
    sum+=val[s[len-1]];
    printf("sum=%d",sum);
    return sum;

   
   

   

    return 0;

} ----------------------
#include <string.h>
int romanToInt(char* s) {
    int strsize = strlen(s);
    int sum = 0;
    for (int i=0; i < strsize; i++) { 
        if (s[i] == 'I') {
            if(s[i+1] == 'V' || s[i+1] == 'X') {
                sum--;
            }
            else {
                sum++;
            }
        }
        else if (s[i] == 'V') {
            sum += 5;
        }
        else if (s[i] == 'X') {
            if(s[i+1] == 'L' || s[i+1] == 'C') {
                sum-=10;
            }
            else {
                sum+=10;
            }
        }
        else if (s[i] == 'L') {
            sum += 50;
        }
        else if (s[i] == 'C') {
            if(s[i+1] == 'D' || s[i+1] == 'M') {
                sum-=100;
            }
            else {
                sum+=100;
            }
        }
        else if (s[i] == 'D') {
            sum += 500;
        }
        else if (s[i] == 'M') {
            sum += 1000;
        }
    }
    return sum;
}
----------------------------------------------- int romanToInt(char* s) { int res, i, len = strlen(s); for(i = 0, res = 0; i < len; i++){ switch(s[i]){ case 'I': if(s[i+1] == 'V' || s[i+1] == 'X') res -= 1; else res += 1; break; case 'V': res += 5; break; case 'X': if(s[i+1] == 'L' || s[i+1] == 'C') res -= 10; else res += 10; break; case 'L': res += 50; break; case 'C': if(s[i+1] == 'D' || s[i+1] == 'M') res -= 100; else res += 100; break; case 'D': res += 500; break; case 'M': res += 1000; break; } } return res; }


標籤: