Likou Q&A: Regular expression matching,

Today's questions


1. Regular expression matching

Question link: https://leetcode.cn/problems/regular-expression-matching/

Given a string s and a character pattern p, please implement a regular expression matching that supports '.' and '*'.

'.' matches any single character
'*' matches zero or more of the previous element
. The so-called match is to cover the entire string s, not part of the string.

Example 1:

Input: s = "aa", p = "a"
Output: false
Explanation: "a" cannot match the entire string of "aa".
Example 2:

Input: s = "aa", p = "a*"
Output: true
Explanation: Because '*' means that it can match zero or more previous elements, the previous element here is 'a'. Therefore, the string "aa" can be considered as 'a' repeated once.
Example 3:

Input: s = "ab", p = ". "
Output: true
Explanation: ".
" means that zero or more ('*') any characters ('.') can be matched.

hint:

1 <= s.length <= 20
1 <= p.length <= 20
s contains only lowercase letters from az.
p contains only lowercase letters from az, and the characters . and *.
Ensure that every time the character * appears, a valid character is matched before it

Code (C)

bool isMatch(char*s,char*p){
    
    
  return s[0]=='\0'&&p[0]=='\0'|| s[0]!='\0'&&p[0]!='\0'&&(p[0]=='.'||p[0]==s[0])&&(p[1]=='*'?isMatch(s+1,p):isMatch(s+1,p+1))|| p[0]!='\0'&&p[1]=='*'&&isMatch(s,p+2);
}


2. The container that holds the most water

Question link: https://leetcode.cn/problems/container-with-most-water/

Given an integer array height of length n. There are n vertical lines, and the two endpoints of the i-th line are (i, 0) and (i, height[i]).

Find the two lines such that the container they form with the x-axis can hold the most water.

Returns the maximum amount of water the container can store.

Note: You cannot tilt the container.

Example 1:
Insert image description here

Input: [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The vertical line in the figure represents the input array [1,8,6,2,5,4,8,3, 7]. In this case, the maximum value that the container can hold water (shown in blue) is 49.
Example 2:

Input: height = [1,1]
Output: 1

hint:

n == height.length
2 <= n <= 105
0 <= height[i] <= 104

Code (C)

int maxArea(int* height, int heightSize){
    
    
    int max=0,size=0,min=0;//从数组两边遍历,设置两个指针i指向头,j指向尾
    int i=0,j=heightSize-1;
    while(i<j){
    
    //遍历
        if(height[i]<height[j])//面积是取最小的height乘长度,并且将短的淘汰留下较长的和后面的继续比较    
            min=height[i++];
        else
            min=height[j--];
        size=(j-i+1)*min;//计算面积
        if(max<size)//面积取遍历的最大值
            max=size;
    }
    return max;
}


3. Convert integers to Roman numerals

Question link: https://leetcode.cn/problems/integer-to-roman/

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.

Character values
​​I 1 V
5 _ _ _ 27 is written as XXVII, which is XX + V + II.





Typically, smaller numbers in Roman numerals are to the right of larger numbers. But there are special cases. For example, 4 is not written as IIII, but as IV. The number 1 is to the left of the number 5 and represents a number equal to the large number 5 minus the number 1, which is the value 4. Likewise, the number 9 is represented as IX. This special rule only applies to the following six situations:

I can be placed to the left of V (5) and X (10) to represent 4 and 9.
X can be placed to the left of L (50) and C (100) to represent 40 and 90.
C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
You are given an integer, convert it to Roman numerals.

Example 1:

Input: num = 3
Output: "III"
Example 2:

Input: num = 4
Output: "IV"
Example 3:

Input: num = 9
Output: "IX"
Example 4:

Input: num = 58
Output: “LVIII”
Explanation: L = 50, V = 5, III = 3.
Example 5:

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

hint:

1 <= num <= 3999

Code (C)

char * intToRoman(int num){
    
    
    int nums[7] = {
    
    1000, 500, 100, 50, 10, 5, 1};
    char roma[8] = "MDCLXVI";
    char *res=malloc(sizeof(char)*16);
    int ptr = 0;
    memset(res, 0 ,16);
    for (int i = 0; i < 7; i++) {
    
    
        while (num - nums[i] >= 0) {
    
     
            char tmp[5];
            sprintf(tmp,"%d",num);  //转换为字符串 看第一个字符是不是4或者9
            if (tmp[0] == '4') {
    
    
                res[ptr] = roma[i];
                res[ptr + 1] = roma[i - 1];
                ptr += 2;
                num = num + nums[i] - nums[i - 1];
            }else if (tmp[0] == '9') {
    
    
                res[ptr] = roma[i + 1];
                res[ptr + 1] = roma[i - 1];
                ptr += 2;
                num = num + nums[i + 1] - nums[i - 1];   
            }else {
    
    
                res[ptr] = roma[i];
                ptr++;
                num -= nums[i];
            }           
        }
    }
    return res;
}


Guess you like

Origin blog.csdn.net/weixin_62529383/article/details/133127660