Leetcode387. 字符串中的第一个唯一字符

Leetcode387. 字符串中的第一个唯一字符

题目:
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
此题和136题类似Leetcode136题
案例:

s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.

题解:
定义hashMap,字符作为key,统计每个字符的个数作为value,再遍历字符串,找到hashMap中value值为1对应的key所在字符串中的索引即可。

java代码:

 public static int firstUniqChar(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (map.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }

scala代码:

/**
    *
    * @param s
    * @return
    */
  def firstUniqChar(s: String): Int = {
    val map = new util.HashMap[Char, Int]()
    var result = -1
    val length = s.length
    for (i <- 0 until length) {
      map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1)
    }

    for (i <- 0 until length; if (result == -1)) {
      if (map.get(s.charAt(i)) == 1) {
        result = i
      }
    }
    result
  }
LeetCode 394字符串解码问题中,C++递归解决方案的核心在于每次递归要取出数字以及对应括号里的内容,递归完成之后,将其按循环次数添加到结果字符串中。以下是两种不同的C++递归实现代码: 第一种实现方式: ```cpp class Solution { public: string decodeString(string s) { // 题意编码的实现,是递归的过程 int pos = 0; return dfs(s, pos); } // 每次递归要做的事:取出数字,以及对应括号里的内容,递归完成之后,循环次数添加到res中 // 注意点:[ 、]符号的跳过 string dfs(string& s, int& pos) { string res = ""; while (pos < s.size() && s[pos] != ']') { if (isalpha(s[pos])) res += s[pos++]; else if (isdigit(s[pos])) { // 没到数字,就确定完数字向后递归 int cnt = 0; while (isdigit(s[pos])) cnt = cnt * 10 + s[pos++] - '0'; pos++; // 最后一次终止条件在[ , 否则递归时不满足while条件,直接退出 string y = dfs(s, pos); pos++; // pos++用于去除] for (int i = 0; i < cnt; i++) res += y; } } return res; } }; ``` 这种实现方式在`dfs`函数里,利用`while`循环遍历字符串,根据字符是字母还是数字进行不同处理。遇到字母直接添加到结果字符串,遇到数字则先解析出重复次数,递归处理括号内的字符串,最后按次数添加到结果中。 第二种实现方式: ```cpp class Solution { private: int get_digit(string& s, int &index) { int num = 0; while (index < s.length() && isdigit(s[index])) num = num * 10 + s[index++] - '0'; return num; } string get_str(string& s, int &index) { // 递归的出口,前一个很容易想到,后一个作为递归出口的条件,更加方便 if (index >= s.length() || ']' == s[index]) return ""; string str; if (isdigit(s[index])) { // 如果是数字,先获取重复的次数,再获取字符串 int cnt = get_digit(s, index); // 获取重复的次数 index++; // 精髓,跳过'['符号 string temp = get_str(s, index); // 获取下一个字符串 index++; // 精髓,跳过']'符号 while (cnt--) str += temp; // 重复添加字符串 } else { // 如果不是数字,则直接获取字符串 while (isalpha(s[index])) str += s[index++]; } // 返回这一次的结果+后面的结果 return str + get_str(s, index); } public: string decodeString(string s) { int i = 0; return get_str(s, i); } }; ``` 此实现方式通过`get_digit`函数解析重复次数,`get_str`函数进行递归处理。当遇到数字时,先获取重复次数,跳过`[`,递归处理括号内字符串,再跳过`]`,最后按次数添加;遇到字母则直接添加到结果字符串。最终返回当前处理结果与后续递归结果的拼接。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值