校招算法笔面试 | 华为机试-合法IP

题目## 题目

题目链接

解题思路

  1. 对于每个字符串,统计每个字母出现的次数
  2. 将字母出现次数按从大到小排序
  3. 贪心策略:将最大的次数分配最大的漂亮度(26),次大的分配次大的漂亮度(25),以此类推
  4. 计算总的漂亮度值

代码

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int getBeauty(string& name) {
    vector<int> count(26, 0);  // 统计26个字母出现次数
    
    // 统计每个字母出现的次数
    for(char c : name) {
        count[tolower(c) - 'a']++;
    }
    
    // 将出现次数从大到小排序
    sort(count.begin(), count.end(), greater<int>());
    
    // 计算漂亮度
    int beauty = 0;
    int value = 26;  // 最大漂亮度从26开始
    for(int i = 0; i < 26 && count[i] > 0; i++) {
        beauty += count[i] * value--;
    }
    
    return beauty;
}

int main() {
    int n;
    cin >> n;
    
    while(n--) {
        string name;
        cin >> name;
        cout << getBeauty(name) << endl;
    }
    
    return 0;
}
import java.util.*;

public class Main {
    public static int getBeauty(String name) {
        int[] count = new int[26];
        
        // 统计每个字母出现的次数
        for(char c : name.toLowerCase().toCharArray()) {
            count[c - 'a']++;
        }
        
        // 将出现次数从大到小排序
        Arrays.sort(count);
        
        // 计算漂亮度
        int beauty = 0;
        int value = 26;
        for(int i = 25; i >= 0 && count[i] > 0; i--) {
            beauty += count[i] * value--;
        }
        
        return beauty;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        while(n-- > 0) {
            String name = sc.next();
            System.out.println(getBeauty(name));
        }
    }
}
def get_beauty(name):
    # 统计每个字母出现的次数
    count = {}
    for c in name.lower():
        count[c] = count.get(c, 0) + 1
    
    # 将出现次数从大到小排序
    freq = sorted(count.values(), reverse=True)
    
    # 计算漂亮度
    beauty = 0
    value = 26
    for f in freq:
        beauty += f * value
        value -= 1
    
    return beauty

n = int(input())
for _ in range(n):
    name = input().strip()
    print(get_beauty(name))

算法及复杂度

  • 算法:贪心算法
  • 时间复杂度: O ( L + K log ⁡ K ) \mathcal{O}(L + K\log K) O(L+KlogK),其中 L L L 是字符串长度, K K K 是不同字符的数量(最大为26)
  • 空间复杂度: O ( K ) \mathcal{O}(K) O(K),需要存储每个字符的出现次数(最大为26)

题目链接

解题思路

这是一个判断IP地址是否合法的问题。需要检查以下几个方面:

关键规则

  1. IP地址格式:

    • 由4个部分组成,每部分8位
    • 用点分隔,如:10.137.17.1
    • 每部分都是无符号整数
  2. 合法性要求:

    • 每部分范围:0~255
    • 不能有空格
    • 不能有前导零
    • 必须是4个部分

实现思路

  1. 输入处理:

    • 按点分割字符串
    • 检查分割后的部分数量
  2. 合法性检查:

    • 检查每部分是否为数字
    • 检查数值范围
    • 检查前导零

代码

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    bool isValidIP(string ip) {
        // 分割IP地址
        vector<string> parts;
        string part;
        for (char c : ip) {
            if (c == '.') {
                if (part.empty()) return false;
                parts.push_back(part);
                part.clear();
            } else {
                part += c;
            }
        }
        if (!part.empty()) parts.push_back(part);
        
        // 检查部分数量
        if (parts.size() != 4) return false;
        
        // 检查每个部分
        for (string& p : parts) {
            // 检查长度
            if (p.empty() || p.length() > 3) return false;
            
            // 检查前导零
            if (p.length() > 1 && p[0] == '0') return false;
            
            // 检查是否为数字
            for (char c : p) {
                if (!isdigit(c)) return false;
            }
            
            // 检查范围
            int num = stoi(p);
            if (num < 0 || num > 255) return false;
        }
        
        return true;
    }
};

int main() {
    string ip;
    while (cin >> ip) {
        Solution sol;
        cout << (sol.isValidIP(ip) ? "YES" : "NO") << endl;
    }
    return 0;
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String ip = sc.next();  // 使用next()而不是nextLine()
            System.out.println(isValidIP(ip) ? "YES" : "NO");
        }
    }
    
    public static boolean isValidIP(String ip) {
        // 检查是否包含空格
        if (ip.contains(" ")) return false;
        
        // 分割IP地址
        String[] parts = ip.split("\\.");
        
        // 检查部分数量
        if (parts.length != 4) return false;
        
        // 检查每个部分
        for (String part : parts) {
            // 检查长度
            if (part.isEmpty() || part.length() > 3) return false;
            
            // 检查前导零
            if (part.length() > 1 && part.charAt(0) == '0') return false;
            
            // 检查是否为数字
            for (char c : part.toCharArray()) {
                if (!Character.isDigit(c)) return false;
            }
            
            // 检查范围
            try {
                int num = Integer.parseInt(part);
                if (num < 0 || num > 255) return false;
            } catch (NumberFormatException e) {
                return false;
            }
        }
        
        // 检查原始字符串中的点的数量(避免多余的点)
        int dotCount = 0;
        for (char c : ip.toCharArray()) {
            if (c == '.') dotCount++;
        }
        if (dotCount != 3) return false;
        
        return true;
    }
}
def isValidIP(ip):
    # 分割IP地址
    parts = ip.split('.')
    
    # 检查部分数量
    if len(parts) != 4:
        return False
        
    # 检查每个部分
    for part in parts:
        # 检查长度
        if not part or len(part) > 3:
            return False
            
        # 检查前导零
        if len(part) > 1 and part[0] == '0':
            return False
            
        # 检查是否为数字
        if not part.isdigit():
            return False
            
        # 检查范围
        num = int(part)
        if num < 0 or num > 255:
            return False
            
    return True

# 主程序
while True:
    try:
        ip = input().strip()
        print("YES" if isValidIP(ip) else "NO")
    except:
        break

算法及复杂度

算法分析

  1. 字符串分割:

    • 按点分割字符串
    • 检查分割后的部分
  2. 数值检查:

    • 检查数字有效性
    • 检查范围合法性
    • 检查格式规范

复杂度分析

  • 时间复杂度: O ( n ) \mathcal{O}(n) O(n) - 其中 n n n 是IP地址的长度
  • 空间复杂度: O ( n ) \mathcal{O}(n) O(n) - 需要存储分割后的字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值