找出一个只出现一次的字符

news/2024/7/7 6:34:52

一,问题描述

给定一个字符串,找出一个 其中只出现一次的字符

如"abaccdeff" 只出现一次的字符有 'b'    'd'     'e'

 

二,问题分析

①字符集是个常量 ,字符只有那么多。比如ASCII 一共256个,比如 字母表一共只有26个,再比如数字,一共0-9 只有10个

②出现一次,说明是次数。将字符映射成出现的次数----Map

③数组就是一种特殊的Map,数组的下标是不变的,相当于Key,下标 i 处存储的值就相当于Value

比如,定义一个存储26个字母出现频率的int[], 下标0处存储 'a',下标1处存储 'b'  ..... 下标 [c-'a'] 处 存储 字符 c

 

三,代码实现

 1 public class FindChar {
 2     public static char onceChar(String str){
 3         if(str == null)
 4             return '\0';
 5         int[] freq = new int[256];
 6         for(int i = 0; i < freq.length; i++)
 7             freq[i] = 0;
 8         for (int i = 0; i < str.length(); i++)
 9             freq[str.charAt(i)]++;
10         for(int i = 0; i < freq.length; i++)
11             if(freq[i] == 1)
12                 return (char)i;
13         return '\0';
14     }
15     
16     public static void main(String[] args) {
17         String str1 = "Abcde";
18         String str2 = "aaBccddee";
19         String str3 = "aabbccddee";
20         
21         char c1 = onceChar(str1);
22         char c2 = onceChar(str2);
23         char c3 = onceChar(str3);
24         char c4 = onceChar(null);
25         
26         System.out.println("c1:" + c1 + " c2:" + c2 + " c3:" + c3 + " c4:" + c4);
27     }
28 }

 


http://www.niftyadmin.cn/n/4610382.html

相关文章

ios 开源代码

1、开源代码 http://www.oschina.net/iOS/codingList/365/ios-buttonhttp://www.devdiv.com/iOS_iPhone-iOS6%E6%96%B0%E7%89%B9%E5%BE%81%EF%BC%9A%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99%E5%92%8C%E7%A4%BA%E4%BE%8B%E6%B1%87%E6%80%BB-thread-127965-1-1.htmlhttp://code4app…

Log4J配置文件详解(转载一)

2019独角兽企业重金招聘Python工程师标准>>> 网站要发布了&#xff0c;为了跟踪一些日志&#xff0c;需要用到log4j&#xff0c;于是就研究了一下log4j的配置 先贴自己用的一个配置源文件 log4j.properties log4j.rootLoggerDEBUG, CONSOLE, FILE ## for console …

1063 Set Similarity (25分)【set】

1063 Set Similarity (25分) Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ is the total number of distinct numbers …

PostgreSQL 多国语言支持的实现

1、先了解&#xff1a;GNU gettext 2、以 pg_config 为例&#xff0c;打开 src/bin/pg_config/nls.mk # src/bin/pg_config/nls.mk CATALOG_NAME pg_config AVAIL_LANGUAGES cs de es fr it ja ko nb pl pt_BR ro ru sv ta tr zh_CN zh_TW GETTEXT_FILES pg_config.…

1060 Are They Equal (25分)【string】

1060 Are They Equal (25分) If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.12310​5​​ with simple chopping. Now given the number of significant digits on a machine …

Node.js中的HTTPS示例

需要openssl的支持&#xff0c; openssl本身不提供windows的安装程序&#xff0c;可以按照如下的步骤进行安装&#xff1a; (参考https://conetrix.com/Blog/how-to-install-openssl-on-windows-7&#xff0c;并复制到下面) How-to Install OpenSSL on Windows 7 Download and …

动态规划专题详细总结(常见简单类型)

什么是动态规划 动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想。简单来说&#xff0c;动态规划将一个复杂的问题分解为若干个子问题&#xff0c;通过综合子问题的最优解来得到原问题的最优解。需要注意的是&#xff0c;动态规划会将每个求解过的子…

MySQL数据库无法启动的简单排错

原创作品&#xff0c;允许转载&#xff0c;转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://dgd2010.blog.51cto.com/1539422/1406691 一般来说有经验的管理员在部署操作系统时通常会将操作系统本身与应用软件分离&#xff0c;将两…