亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

leetcode389.Find The Difference

cyqian / 2927人閱讀

摘要:題目要求假設(shè)兩個只包含小寫字母的字符串和,其中是中字母的亂序,并在某個位置上添加了一個新的字母。最后只需要遍歷整數(shù)數(shù)組檢查是否有某個字符計數(shù)大于。

題目要求
Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
"e" is the letter that was added.

假設(shè)兩個只包含小寫字母的字符串s和t,其中t是s中字母的亂序,并在某個位置上添加了一個新的字母。問添加的這個新的字母是什么?

思路一:字符數(shù)組

我們可以利用一個整數(shù)數(shù)組來記錄所有字符出現(xiàn)的次數(shù),在s中出現(xiàn)一次相應(yīng)計數(shù)加一,在t中出現(xiàn)一次則減一。最后只需要遍歷整數(shù)數(shù)組檢查是否有某個字符計數(shù)大于0。則該字符就是多余的字符。

    public char findTheDifference(String s, String t) {
        int[] count = new int[26];
        for(int i = 0 ; i
思路二:求和

我們知道,字符對應(yīng)的ascii碼是唯一的,那么既然兩個字符串相比只有一個多余的字符,那么二者的ascii碼和相減就可以找到唯一的字符的ascii碼。

    public char findTheDifference2(String s, String t){
        int value = 0;
        for(int i = 0 ; i           
               
                                           
                       
                 

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/77450.html

相關(guān)文章

  • Leetcode PHP題解--D73 389. Find the Difference

    摘要:題目鏈接題目分析給定兩個字符串,其中一個字符串比另一個字符串在隨機位置多一個字符。思路用計算字符串中字符出現(xiàn)的次數(shù),對比兩個字符串的字符出現(xiàn)次數(shù)。計算差集,返回差異部分即可。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 D73 389. Find the Difference 題目鏈接 389. Find the Difference 題目分析 給定兩個字符串,其中一個字符串比另一...

    widuu 評論0 收藏0
  • [LintCode/LeetCode] Contains Duplicate III

    Problem Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute dif...

    MageekChiu 評論0 收藏0
  • [Leetcode] Contains Duplicate 包含重復(fù)

    摘要:代碼集合法復(fù)雜度時間空間思路同樣使用集合,但這次我們要維護集合的大小不超過,相當于是記錄一個寬度為的窗口中出現(xiàn)過的數(shù)字。 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. Your function should return true if any v...

    rozbo 評論0 收藏0
  • [LintCode/LeetCode] Contains Duplicate II

    Problem Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at ...

    894974231 評論0 收藏0
  • leetcode376. Wiggle Subsequence

    摘要:題目要求扭動序列是指數(shù)組中的相鄰兩個元素的差保證嚴格的正負交替,如數(shù)組中相鄰兩個元素的差為,滿足扭動序列的要求?,F(xiàn)在要求從一個數(shù)組中,找到長度最長的扭動子序列,并返回其長度。即前一個元素和當前元素構(gòu)成下降序列,因此代碼如下 題目要求 A sequence of numbers is called a wiggle sequence if the differences between ...

    CoffeX 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<