Press "Enter" to skip to content

月度归档: 2022 年 7 月

K&R2 第二章提到类型转换示例

例如,假设 int 为 16 位,long 为 32 位。 然后 -1L < 1U,因为 1U 是一个 unsigned int,被提升为一个有符号的 long。 但是 -1L > 1UL 因为 -1L 被提升为无符号长整数,因此看起来是一个很大的正数。

-1L < 1U:

-1L 作为 32 位长 int = 表示为 0xFFFFFFFF。 在这种情况下不会被转换,因此解释保持 -1

1U 作为 16 位无符号整数 = 0x0001。 提升为 32 位长 int -> 0x00000001 = 解释为 1

比较结果:-1 < 1

-1L > 1UL:

-1L = 32 位长整数 = 0xFFFFFFFF。 转换为 unsigned long int = 0xFFFFFFFF。 因为它现在是无符号的,所以它会被解释为 (2^32 – 1)

1UL 作为 32 位长 int = 表示为 0x00000001。 在这种情况下不会被转换,所以解释保持 1

比较结果:(2^32 – 1) > 1

14. 最长公共前缀

方法一:

横向扫描
用LCP(S1…Sn)表示字符串S1…Sn的最长公共前缀。

LCP(S_1......S_n) = LCP(LCP(LCP(S_1,S_2),S_3),...S_n)

依次遍历字符串数组中的每个字符串,如果尚未遍历完所以的字符串时,最长公共前缀已经是空字符串,则最长公共前缀一定是空字符串。直接返回空字符串。

Python3 代码解法

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if not strs:
            return ""

        prefix, cont = strs[0], len(strs)
        for i in range(1, cont):
            prefix = self.lcp(prefix, strs[i])
            if not prefix:
                break

        return prefix

    def lcp(self, str1, str2):
        length, index = min(len(str1), len(str2)), 0
        while index < length and str1[index] == str2[index]:
            index += 1
        return str1[:index]

Source: LeetCode(The title reproduced in this blog is for personal study use only)

13. 罗马数字转整数

方法一:

  • 创建一个哈希表将罗马字符与对应的数值关联起来。
  • 然后对字符串进行遍历,由于组合只有两种情况,一种是一个字符,一种是两个字符,其中两个字符优先于一个字符
  • 先判断两个字符的组合在哈希表中是否存在,存在则将值取出加到结果ans中,并向后移两个字符。不存在则判断当前1个字符是否存在,存在则将值去除加到结果ans中,并向后移1个字符,最后遍历结束返回结果ans

C 代码题解

int romanToInt(char* s) {
    int symbolValues[26];
    symbolValues['I' - 'A'] = 1;
    symbolValues['V' - 'A'] = 5;
    symbolValues['X' - 'A'] = 10;
    symbolValues['L' - 'A'] = 50;
    symbolValues['C' - 'A'] = 100;
    symbolValues['D' - 'A'] = 500;
    symbolValues['M' - 'A'] = 1000;
    int ans = 0;
    int n = strlen(s);
    for (int i = 0; i < n; ++i) {
        int value = symbolValues[s[i] - 'A'];
        if (i < n - 1 && value < symbolValues[s[i + 1] - 'A']) {
            ans -= value;
        } else {
            ans += value;
        }
    }
    return ans;
}

Python3 代码题解

class Solution:

    SYMBOL_VALUES =  {
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000,
    }

    def romanToInt(self, s: str) -> int:
        ans = 0
        n = len(s)
        for i, ch in enumerate(s):
            value = Solution.SYMBOL_VALUES[ch]
            //判断当前字符是否是特殊情况,即当前字符的数值小于下一个字符的数值。如果是特殊情况,需要减去当前字符的数值。
            if i < n - 1 and value < Solution.SYMBOL_VALUES[s[i + 1]]:
                ans -= value
            else:
                ans += value 
        return ans

Source: LeetCode(The title reproduced in this blog is for personal study use only)

The difference between python3 function passing parameters * and **

positional argument
keyword argument

The difference between *args and **kwargs, both are variadic arguments in python:

*args represents any number of unnamed arguments, which is essentially a tuple

**kwargs means keyword arguments, which is essentially a dict

When using both *args and **kwargs, the *args argument must be listed before **kwargs.


Source: Python Crash Course, 2nd Edition A Hands-On:

Note: You’ll often see the generic parameter name *args, which collects arbitrary positional
arguments like this.

Note: You’ll often see the parameter name **kwargs used to collect non-specific keyword
arguments.