<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)387:字符串中的第一個唯一字符

          共 1516字,需瀏覽 4分鐘

           ·

          2021-09-22 20:04

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 字符串中的第一個唯一字符,我們先來看題面:
          https://leetcode-cn.com/problems/lexicographical-numbers/

          Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

          給定一個字符串,找到它的第一個不重復的字符,并返回它的索引。如果不存在,則返回 -1。

          示例

          s = "leetcode"
          返回 0

          s = "loveleetcode"
          返回 2


          解題

          主要思路:
          先統(tǒng)計字符串中,每個字符出現(xiàn)的次數(shù)。
          然后依次遍歷找到第一個次數(shù)為1的字符。


          class Solution {
          public:
              int firstUniqChar(string s) {
                  map<char, int> m;
                  for(int i = 0; i < s.length(); i ++){
                      m[s[i]] ++;
                  }
                  for(int i = 0; i < s.length(); i ++) {
                      if(m[s[i]] == 1) return i;
                  }
                  return -1;
              }
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-380題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)381:O(1) 時間插入、刪除和獲取隨機元素

          LeetCode刷題實戰(zhàn)382:鏈表隨機節(jié)點

          LeetCode刷題實戰(zhàn)383:贖金信

          LeetCode刷題實戰(zhàn)384:打亂數(shù)組

          LeetCode刷題實戰(zhàn)385:迷你語法分析器

          LeetCode刷題實戰(zhàn)386:字典序排數(shù)

          瀏覽 44
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  夜夜嗨av | 伊人成人娱乐网 | 欧美成人一级视频 | 人人添人人摸 | 日韩一区二区三区四区 |