LeetCode刷題之387Python字串中的第一個唯一字元
題目:
給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。
案例:
s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.
注意事項:您可以假定該字串只包含小寫字母。
我的解答:
先計數,返回第一個計數值為1的字元。
class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ dic = {} for i in s:#i是字元 if i not in dic: dic[i] = 1 else: dic[i] = dic[i] + 1 for i in range(len(s)):#i是索引 if dic[s[i]] == 1: return i return -1
相關推薦
【LeetCode 簡單題】85-字串中第一個唯一字元
宣告: 今天是第85道題。給定n,從 1 到 n 選擇一個數字。 你需要猜我選擇了哪個數字。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・`
LeetCode刷題之387Python字串中的第一個唯一字元
題目: 給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事項:您可以假定該字串只包含小寫字母。 我的解答: 先計數,
C#LeetCode刷題之#415-字串相加(Add Strings)
問題 給定兩個字串形式的非負整數 num1 和num2 ,計算它們的和。 注意: num1 和num2 的長度都小於 5100. num1 和num2 都只包含數字 0-9. num1 和nu
leetcode刷題:反轉字串中的單詞 III
給定一個字串,你需要反轉字串中每個單詞的字元順序,同時仍保留空格和單詞的初始順序。 示例 1: 輸入: "Let's take LeetCode contest" 輸出: "s'teL ekat
#leetcode刷題之路4-尋找兩個有序數組的中位數
大小 let pre 個數 clu emp 。。 tco net 給定兩個大小為 m 和 n 的有序數組 nums1 和 nums2。請你找出這兩個有序數組的中位數,並且要求算法的時間復雜度為 O(log(m + n))。你可以假設 nums1 和 nums2 不會同時為空
LeetCode刷題之三:判斷兩個二叉樹是否相同
題目為: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are str
LeetCode——第387題:字串的第一個唯一字元
題目: 給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。 案例: s = “leetcode” 返回 0. s = “loveleetcode”, 返
C#LeetCode刷題之#557-反轉字串中的單詞 III(Reverse Words in a String III)
問題 給定一個字串,你需要反轉字串中每個單詞的字元順序,同時仍保留空格和單詞的初始順序。 輸入: "Let's take LeetCode contest" 輸出: "s'teL ekat edoCteeL tsetnoc" 注意:在字串中,每個單詞由
刷爆LeetCode之 字串中的第一個唯一字元
字串中的第一個唯一字元 給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事項:您可以假定該字串只包含小寫字母。
C#LeetCode刷題之#345-反轉字串中的母音字母(Reverse Vowels of a String)
問題 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 輸入: "hello" 輸出: "holle" 輸入: "leetcode" 輸出: "leotcede" 說明:母音字母不包含字母"y"。 Write a function that tak
LeetCode刷題記錄——第387題(字串中的第一個唯一字元)
題目描述 給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。 案例: s = “leetcode” 返回 0. s = “loveleetcode”, 返回 2. 思路分析 關鍵問題在於,如何找到第一個不重複字元
LeetCode刷題——字串中的第一個唯一字元
大家好,結束了五一假期,繼續刷題日記。本次題目要求如下:我的思路是這樣的:首先計算出s的長度並將s複製給一個字串cont,進入while迴圈,知道字串s為空,使用.find()函式尋找字串裡是否有與s[0]相同的字元若有,使用remove函式把所有與s[0]相同的字元包括s[
每日一題--LeetCode 387 (字串中的第一個唯一字元)java
題目描述: 程式碼如下: class Solution { public int firstUniqChar(String s) { String tmp=s; char []data=tmp.toCharArray(); if(
C#LeetCode刷題之#278-第一個錯誤的版本(First Bad Version)
問題 你是產品經理,目前正在帶領一個團隊開發新的產品。不幸的是,你的產品的最新版本沒有通過質量檢測。由於每個版本都是基於之前的版本開發的,所以錯誤的版本之後的所有版本都是錯的。 假設你有 n 個版本 [1, 2, ..., n],你想找出導致之後所有版本出錯的第一個錯誤的版本。 你可
C#LeetCode刷題之#686-重複疊加字串匹配(Repeated String Match)
問題 給定兩個字串 A 和 B, 尋找重複疊加字串A的最小次數,使得字串B成為疊加後的字串A的子串,如果不存在則返回 -1。 舉個例子,A = "abcd",B = "cdabcdab"。 答案為 3, 因為 A 重複疊加三遍後為 “abcdabcdabcd”,此時 B 是其子串;A
C#LeetCode刷題之#680-驗證迴文字串 Ⅱ(Valid Palindrome II)
問題 給定一個非空字串 s,最多刪除一個字元。判斷是否能成為迴文字串。 輸入: "aba" 輸出: True 輸入: "abca" 輸出: True 解釋: 你可以刪除c字元。 注意:字串只包含從 a-z 的小寫字母。字串的最大長度是
C#LeetCode刷題之#541-反轉字串 II(Reverse String II)
問題 給定一個字串和一個整數 k,你需要對從字串開頭算起的每個 2k 個字元的前k個字元進行反轉。如果剩餘少於 k 個字元,則將剩餘的所有全部反轉。如果有小於 2k 但大於或等於 k 個字元,則反轉前 k 個字元,並將剩餘的字元保持原樣。 輸入: s = "abcdefg", k
C#LeetCode刷題之#459-重複的子字串(Repeated Substring Pattern)
問題 給定一個非空的字串,判斷它是否可以由它的一個子串重複多次構成。給定的字串只含有小寫英文字母,並且長度不超過10000。 輸入: "abab" 輸出: True 解釋: 可由子字串 "ab" 重複兩次構成。 輸入: "aba" 輸出: Fa
C++ Leetcode初級演算法之字串中的第一個唯一字元
給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。 案例: s = “leetcode” 返回 0. s = “loveleetcode”, 返回 2. 注意事項:您可以假定該字串只包含小寫字母。 class Solution { pub
C#LeetCode刷題之#203-刪除連結串列中的節點(Remove Linked List Elements)
問題 刪除連結串列中等於給定值 val 的所有節點。 輸入: 1->2->6->3->4->5->6, val = 6 輸出: 1->2->3-&