<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/ae5c30.md for this page in Markdown format</div>
拆分数位后四位数字的最小和
| Category | Difficulty | Likes | Dislikes |
|---|---|---|---|
| algorithms | Easy (85.20%) | 21 | - |
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>
给你一个四位 正 整数 num 。请你使用 num 中的 数位 ,将 num 拆成两个新的整数 new1 和 new2 。new1 和 new2 中可以有 前导 0 ,且 num 中 所有 数位都必须使用。
- 比方说,给你
num = 2932,你拥有的数位包括:两个2,一个9和一个3。一些可能的[new1, new2]数对为[22, 93],[23, 92],[223, 9]和[2, 329]。
请你返回可以得到的 new1 和 new2 的 最小 和。
示例 1:
输入:num = 2932
输出:52
解释:可行的 [new1, new2] 数对为 [29, 23] ,[223, 9] 等等。
最小和为数对 [29, 23] 的和:29 + 23 = 52 。示例 2:
输入:num = 4009
输出:13
解释:可行的 [new1, new2] 数对为 [0, 49] ,[490, 0] 等等。
最小和为数对 [4, 9] 的和:4 + 9 = 13 。提示:
1000 <= num <= 9999
/*
* @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Date: 2022-11-10 10:57:00
* @LastEditTime: 2022-11-10 11:09:25
* @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Description:
* @FilePath: /imooc-jira/Users/izhaong/izhaong/Project_me/leetcode/2160.拆分数位后四位数字的最小和.ts
*/
/*
* @lc app=leetcode.cn id=2160 lang=typescript
*
* [2160] 拆分数位后四位数字的最小和
*
* https://leetcode.cn/problems/minimum-sum-of-four-digit-number-after-splitting-digits/description/
*
* algorithms
* Easy (85.20%)
* Likes: 21
* Dislikes: 0
* Total Accepted: 11.1K
* Total Submissions: 13K
* Testcase Example: '2932'
*
* 给你一个四位 正 整数 num 。请你使用 num 中的 数位 ,将 num 拆成两个新的整数 new1 和 new2 。new1 和 new2
* 中可以有 前导 0 ,且 num 中 所有 数位都必须使用。
*
*
* 比方说,给你 num = 2932 ,你拥有的数位包括:两个 2 ,一个 9 和一个 3 。一些可能的 [new1, new2] 数对为 [22,
* 93],[23, 92],[223, 9] 和 [2, 329] 。
*
*
* 请你返回可以得到的 new1 和 new2 的 最小 和。
*
*
*
* 示例 1:
*
* 输入:num = 2932
* 输出:52
* 解释:可行的 [new1, new2] 数对为 [29, 23] ,[223, 9] 等等。
* 最小和为数对 [29, 23] 的和:29 + 23 = 52 。
*
*
* 示例 2:
*
* 输入:num = 4009
* 输出:13
* 解释:可行的 [new1, new2] 数对为 [0, 49] ,[490, 0] 等等。
* 最小和为数对 [4, 9] 的和:4 + 9 = 13 。
*
*
*
*
* 提示:
*
*
* 1000 <= num <= 9999
*
*
*/
// @lc code=start
function minimumSum(num: number): number {
const sortNumStr = String(num).split('').sort().join('');
return (
Number(sortNumStr[0] + sortNumStr[2]) +
Number(sortNumStr[1] + sortNumStr[3])
);
}
// @lc code=end