回溯算法--LC.40组合总和II

回溯法解决组合问题:去重与剪枝
本文详细介绍了如何使用回溯法解决组合问题,特别是针对数组中有重复元素的情况。通过排序和剪枝策略实现了解集去重,确保每个解都是唯一的。示例代码展示了如何在Python中实现这一算法,适用于解决40题的组合问题。

在这里插入图片描述

class Solution(object):
    def combinationSum2(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        # 回溯法
        # 此题与39题的区别:39的数组内无重复,但是组合内数字可以重复;40是数组内有重复,但是组合内的数字只能使用一次,解集中不能有重复的集合
        # 40题最关键的一点就是去重,这里的去重是解集中的,不是解集内部
        # 例如,[1,1,1,2]可以,[[1,2],[1,2]]不行
        result = list()
        # 先排序,从小到大,让重复的可以挨着
        candidates.sort()
        self.backtracking(candidates, target, result, 0, [])
        return result
    def backtracking(self, candidates, target, result, start, path):
        
        # 终止条件
        if sum(path) == target:
            result.append(path[:]) 
            return
        for i in range(len(candidates)):
            # 剪枝
            if sum(path) + candidates[i] > target:
                return
            # 去重操作,candidates[i] == candidates[i-1]只能保证当前数字与前一个数字相等,i>start确保的是,同一层不能有重复的,但是同一枝里面,往下是可以重复的
            if i>start and candidates[i] == candidates[i-1]:
                continue
            
            path.append(i)
            self.backtracking(candidates, target, result, i+1, path)
            # 回溯
            path.pop()
[root@yfw ~]# cd /opt/openfire/plugins/pade/classes/docs/dist [root@yfw dist]# cd /opt/openfire/plugins/pade/classes/docs/dist/locales [root@yfw locales]# ls -la 总用量 1908 drwxr-xr-x 3 openfire openfire 4096 10月 30 23:41 . drwxr-xr-x 6 openfire openfire 4096 10月 30 23:41 .. -rw-r--r-- 1 openfire openfire 48302 10月 30 23:41 af-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 36466 10月 30 23:40 ar-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 27345 10月 30 23:41 be-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 67775 10月 30 23:40 bg-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 49287 10月 30 23:40 ca-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 30594 10月 30 23:40 cs-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 25551 10月 30 23:40 da-LC_MESSAGES-converse-po.js drwxr-xr-x 2 openfire openfire 12288 10月 30 23:41 dayjs -rw-r--r-- 1 openfire openfire 50529 10月 30 23:41 de-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 44879 10月 30 23:40 el-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 27855 10月 30 23:40 eo-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 45141 10月 30 23:40 es-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 40735 10月 30 23:40 eu-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 28597 10月 30 23:40 fa-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 35547 10月 30 23:40 fi-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 50533 10月 30 23:41 fr-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 49260 10月 30 23:41 gl-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 34554 10月 30 23:40 he-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 23380 10月 30 23:40 hi-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 39898 10月 30 23:40 hu-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 37433 10月 30 23:41 id-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 44376 10月 30 23:40 it-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 47881 10月 30 23:40 ja-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 38543 10月 30 23:40 ko-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 40142 10月 30 23:40 lt-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 26310 10月 30 23:40 mr-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 26391 10月 30 23:41 nb-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 40451 10月 30 23:40 nl_BE-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 30968 10月 30 23:40 nl-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 33513 10月 30 23:40 oc-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 40088 10月 30 23:41 pl-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 40972 10月 30 23:40 pt_BR-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 42459 10月 30 23:40 pt-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 28364 10月 30 23:40 ro-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 67493 10月 30 23:40 ru-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 26347 10月 30 23:40 si-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 26347 10月 30 23:40 sq-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 41184 10月 30 23:40 sv-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 91278 10月 30 23:41 ta-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 30604 10月 30 23:40 th-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 42338 10月 30 23:41 tr-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 63643 10月 30 23:40 ug-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 67511 10月 30 23:40 uk-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 28951 10月 30 23:40 vi-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 44051 10月 30 23:40 zh_CN-LC_MESSAGES-converse-po.js -rw-r--r-- 1 openfire openfire 38537 10月 30 23:40 zh_TW-LC_MESSAGES-converse-po.js [root@yfw locales]#
11-01
你已经进入了一个非常关键的目录: ``` /opt/openfire/plugins/pade/classes/docs/dist/locales ``` 并且通过 `ls -la` 发现了以下重要信息: - 存在 **`zh_CN-LC_MESSAGES-converse-po.js`** ✅ - 文件名格式为:`语言代码-LC_MESSAGES-converse-po.js` - 这是 **gettext 风格的 JavaScript 语言包**,不是 JSON 格式 - Converse.js 在 Pade 中使用的是 **编译后的 `.po.js` 多语言模块** --- ## ✅ 结论:中文支持已存在! > **简体中文(zh_CN)已经内置在 Pade 插件中!** > > 文件:`zh_CN-LC_MESSAGES-converse-po.js` 这意味着你不需要手动创建翻译文件,只需要 **让浏览器或系统正确触发加载这个中文语言包即可。** --- ## 🧩 为什么前端还是英文?——常见原因分析 尽管中文包存在,但可能因以下原因未生效: | 原因 | 说明 | |------|------| | 🔹 浏览器语言不是 `zh-CN` | 自动切换依赖浏览器 Accept-Language | | 🔹 Openfire/Pade 缓存未清除 | 资源被缓存,旧版 JS 仍在运行 | | 🔹 没有正确设置用户语言偏好 | 用户登录后未保存语言设置 | | 🔹 URL 强制覆盖了 locale | 如加了 `?lang=en` 参数 | --- ## ✅ 解决方案:确保加载 `zh_CN` 中文包 ### ✅ 方法一:修改浏览器语言(最简单测试) #### Chrome / Edge 设置中文: 1. 打开浏览器 → 设置 → 语言 2. 添加 “中文(简体)” 并拖到第一位 3. 重启浏览器 4. 访问 Pade 页面:`https://your-server:9091/plugins/pade/` 👉 此时页面应自动加载 `zh_CN-LC_MESSAGES-converse-po.js` 并显示为中文。 --- ### ✅ 方法二:强制 URL 参数启用中文(调试用) 尝试在地址栏添加语言参数: ``` https://your-server:9091/plugins/pade/?lang=zh_CN ``` 或 ``` https://your-server:9091/plugins/pade/?language=zh-CN ``` > ⚠️ 注意:Pade 是否支持这些参数取决于其 JS 初始化逻辑。 --- ### ✅ 方法三:检查并修改 Pade 的初始化脚本 找到 Pade 加载 Converse.js 的地方,通常是: ``` /opt/openfire/plugins/pade/index.jsp ``` 编辑它: ```bash nano /opt/openfire/plugins/pade/index.jsp ``` 查找类似代码段(JS 部分): ```js converse.initialize({ auto_list_rooms: false, show_controlbox_by_default: true, theme: 'default', authentication: 'login', jid: jid, password: password, // ...其他配置 }); ``` ### 修改为强制中文: ```js converse.initialize({ locale: 'zh_CN', // 明确指定语言 i18n: { locales: ['zh_CN'] }, auto_list_rooms: false, show_controlbox_by_default: true, theme: 'default', authentication: 'login', jid: jid, password: password }); ``` 📌 关键点是添加: ```js locale: 'zh_CN' ``` 这会强制 Converse.js 使用简体中文。 --- ### ✅ 方法四:验证是否成功加载中文包 打开浏览器开发者工具(F12)→ Network → 刷新页面 搜索关键词: ``` zh_CN ``` 你应该能看到请求: ``` zh_CN-LC_MESSAGES-converse-po.js ``` 状态码为 `200` 表示成功加载。 如果没出现,请检查路径权限和拼写。 --- ## 🔐 权限与重启建议 确保文件可读: ```bash chmod 644 /opt/openfire/plugins/pade/classes/docs/dist/locales/zh_CN-LC_MESSAGES-converse-po.js chown openfire:openfire /opt/openfire/plugins/pade/classes/docs/dist/locales/zh_CN-LC_MESSAGES-converse-po.js ``` 重启 Openfire 服务以清缓存: ```bash systemctl restart openfire ``` 或者进入 Openfire 管理控制台 → Plugins → Uninstall Pade → Reinstall(更彻底) --- ## 💡 高级技巧:添加语言选择下拉框(可选功能) 你可以修改 `index.jsp`,增加一个语言选择器: ```html <select onchange="setLanguage(this.value)"> <option value="en">English</option> <option value="zh_CN" selected>中文 (简体)</option> <option value="zh_TW">中文 (繁體)</option> </select> <script> function setLanguage(lang) { // 存入 localStorage 或 cookie localStorage.setItem('converse_language', lang); location.reload(); } // 初始化前读取 const savedLang = localStorage.getItem('converse_language') || 'zh_CN'; converse.initialize({ locale: savedLang, // ... }); </script> ``` --- ## ✅ 总结:你现在要做的事 | 步骤 | 操作 | |------|------| | 1️⃣ 确认中文包存在 | ✅ 已有 `zh_CN-LC_MESSAGES-converse-po.js` | | 2️⃣ 设置浏览器语言为 `zh-CN` | 测试是否自动变中文 | | 3️⃣ 修改 `index.jsp` | 添加 `locale: 'zh_CN'` 强制启用 | | 4️⃣ 清除缓存并重启 | `systemctl restart openfire` | | 5️⃣ 开发者工具验证 | 查看 Network 是否加载 `zh_CN` 文件 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值