From 2b7b78180ed9406e1aa43e5782985acab3e4dd05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Tue, 6 Dec 2022 09:37:38 +0800 Subject: [PATCH] =?UTF-8?q?1805:=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E6=95=B4=E6=95=B0=E7=9A=84=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NumberOfDifferentIntegersInAString.java | 94 +++++++++++++++++++ .../NumberOfDifferentIntegersInAString.md | 43 +++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/NumberOfDifferentIntegersInAString.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/NumberOfDifferentIntegersInAString.md diff --git a/src/main/java/leetcode/editor/cn/NumberOfDifferentIntegersInAString.java b/src/main/java/leetcode/editor/cn/NumberOfDifferentIntegersInAString.java new file mode 100644 index 0000000..662001a --- /dev/null +++ b/src/main/java/leetcode/editor/cn/NumberOfDifferentIntegersInAString.java @@ -0,0 +1,94 @@ +//

给你一个字符串 word ,该字符串由数字和小写英文字母组成。

+// +//

请你用空格替换每个不是数字的字符。例如,"a123bc34d8ef34" 将会变成 " 123  34 8  34" 。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):"123""34""8""34"

+// +//

返回对 word 完成替换后形成的 不同 整数的数目。

+// +//

只有当两个整数的 不含前导零 的十进制表示不同, 才认为这两个整数也不同。

+// +//

 

+// +//

示例 1:

+// +//
+//输入:word = "a123bc34d8ef34"
+//输出:3
+//解释:不同的整数有 "123"、"34" 和 "8" 。注意,"34" 只计数一次。
+//
+// +//

示例 2:

+// +//
+//输入:word = "leet1234code234"
+//输出:2
+//
+// +//

示例 3:

+// +//
+//输入:word = "a1b01c001"
+//输出:1
+//解释:"1"、"01" 和 "001" 视为同一个整数的十进制表示,因为在比较十进制值时会忽略前导零的存在。
+//
+// +//

 

+// +//

提示:

+// +// +// +//
Related Topics
  • 哈希表
  • 字符串

  • 👍 38
  • 👎 0
  • +package leetcode.editor.cn; + +import java.util.Arrays; + +// 1805:字符串中不同整数的数目 +public class NumberOfDifferentIntegersInAString { + public static void main(String[] args) { + Solution solution = new NumberOfDifferentIntegersInAString().new Solution(); + // TO TEST + System.out.println(solution.numDifferentIntegers("a123bc34d8ef34")); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int numDifferentIntegers(String word) { + for (int i = 0; i < word.length(); i++) { + if (word.charAt(i) >= 'a' && word.charAt(i) <= 'z') { + word = word.replace(word.charAt(i) + "", " "); + } + } + while (word.contains(" ")) { + word = word.replace(" ", " "); + } + if (word.startsWith(" ")) { + word = word.substring(1); + } + if (word.endsWith(" ")) { + word = word.substring(0, word.length() - 1); + } + if (word.equals("")) { + return 0; + } + String[] strs = word.split(" "); + for (int i = 0; i < strs.length; i++) { + while (strs[i].startsWith("0")) { + strs[i] = strs[i].substring(1); + } + } + Arrays.sort(strs); + int count = 1; + for (int i = 1; i < strs.length; i++) { + if (!strs[i - 1].equals(strs[i])) { + count++; + } + } + return count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/NumberOfDifferentIntegersInAString.md b/src/main/java/leetcode/editor/cn/doc/content/NumberOfDifferentIntegersInAString.md new file mode 100644 index 0000000..7c05325 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/NumberOfDifferentIntegersInAString.md @@ -0,0 +1,43 @@ +

    给你一个字符串 word ,该字符串由数字和小写英文字母组成。

    + +

    请你用空格替换每个不是数字的字符。例如,"a123bc34d8ef34" 将会变成 " 123  34 8  34" 。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):"123""34""8""34"

    + +

    返回对 word 完成替换后形成的 不同 整数的数目。

    + +

    只有当两个整数的 不含前导零 的十进制表示不同, 才认为这两个整数也不同。

    + +

     

    + +

    示例 1:

    + +
    +输入:word = "a123bc34d8ef34"
    +输出:3
    +解释:不同的整数有 "123"、"34" 和 "8" 。注意,"34" 只计数一次。
    +
    + +

    示例 2:

    + +
    +输入:word = "leet1234code234"
    +输出:2
    +
    + +

    示例 3:

    + +
    +输入:word = "a1b01c001"
    +输出:1
    +解释:"1"、"01" 和 "001" 视为同一个整数的十进制表示,因为在比较十进制值时会忽略前导零的存在。
    +
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 哈希表
  • 字符串

  • 👍 38
  • 👎 0
  • \ No newline at end of file