From 678f51b262d63d319a97d378e9496f83424750d0 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 22 Jun 2020 09:27:05 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9B=B8?= =?UTF-8?q?=E5=85=B3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Algorithm/剑指 Offer/字符串相关.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/字符串相关.md b/blogs/Algorithm/剑指 Offer/字符串相关.md index e9236e0..bbf4e12 100644 --- a/blogs/Algorithm/剑指 Offer/字符串相关.md +++ b/blogs/Algorithm/剑指 Offer/字符串相关.md @@ -24,3 +24,43 @@ class Solution { } ``` +[50. 第一个只出现一次的字符](https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/) + +```java +class Solution { + public char firstUniqChar(String s) { + if (s.length() == 0) { + return ' '; + } + LinkedHashMap map = new LinkedHashMap<>(); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == 1) { + return entry.getKey(); + } + } + return ' '; + } +} +``` + +```java +class Solution { + public char firstUniqChar(String s) { + char[] array = s.toCharArray(); + int[] ints = new int[256]; + for (char c : array) { + ints[c]++; + } + for (char c : array) { + if (ints[c] == 1) { + return c; + } + } + return ' '; + } +} +``` +