leetcode刷題之Longest Common Prefix(14)
編寫一個函式來查詢字串陣列中的最長公共字首。
如果不存在公共字首,返回空字串 ""。
示例 1:
輸入: ["flower","flow","flight"]
輸出: "fl"
示例 2:
輸入: ["dog","racecar","car"]
輸出: ""
解釋: 輸入不存在公共字首。
說明:
所有輸入只包含小寫字母 a-z 。
思路:假設第一個字串就是最長字首,依次和剩下的所有字串對比,如果不是的話,依次減去一個字元再迴圈對比一遍,直到找到最大字首或者pre為"",String.indexOf("") = 0
程式碼:
Java
public static String solution(String[] strs){
if(strs == null || strs.length == 0) return "";
String pre = strs[0];
int i = 1;
while(i < strs.length){
while(strs[i].indexOf(pre) != 0) {
pre = pre.substring(0, pre.length() - 1);
}
i++;
}
return pre;
}
相關推薦
leetcode刷題之Longest Common Prefix(14)
編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2: 輸入: ["dog","racecar","car"] 輸出: "" 解釋: 輸入不存在公共字首。 說明: 所
LeetCode刷題_14. Longest Common Prefix
原題連結:https://leetcode.com/problems/longest-common-prefix/description/ Write a function to find the longest common prefix string amongst an arr
leetcode刷題之Roman to Integer(13)
羅馬數由七個數字組成:I, V, X, L, C, D,M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 2可以用II表示,即兩個一一起。12寫作XII,即X+II。27寫作XXVII即XX+V+II. 羅馬數一般從左到右由大到小,但是4並不是寫作
C#LeetCode刷題之#704-二分查詢(Binary Search)
問題 給定一個 n 個元素有序的(升序)整型陣列 nums 和一個目標值 target ,寫一個函式搜尋 nums 中的 target,如果目標值存在返回下標,否則返回 -1。 輸入: nums = [-1,0,3,5,9,12], target = 9 輸出:
C#LeetCode刷題之#824-山羊拉丁文(Goat Latin)
問題 給定一個由空格分割單詞的句子 S。每個單詞只包含大寫或小寫字母。 我們要將句子轉換為 “Goat Latin”(一種類似於 豬拉丁文 - Pig Latin 的虛構語言)。 山羊拉丁文的規則如下: 如果單詞以母音開頭(a, e, i, o, u),在單詞後新增"ma"。 例
C#LeetCode刷題之#788-旋轉數字(Rotated Digits)
問題 我們稱一個數 X 為好數, 如果它的每位數字逐個地被旋轉 180 度後,我們仍可以得到一個有效的,且和 X 不同的數。要求每位數字都要被旋轉。 如果一個數的每位數字被旋轉以後仍然還是一個數字, 則這個數是有效的。0, 1, 和 8 被旋轉後仍然是它們自己;2 和 5 可以互相旋轉成
C#LeetCode刷題之#67-二進位制求和(Add Binary)
問題 給定兩個二進位制字串,返回他們的和(用二進位制表示)。 輸入為非空字串且只包含數字 1 和 0。 輸入: a = "11", b = "1" 輸出: "100" 輸入: a = "
C#LeetCode刷題之#415-字串相加(Add Strings)
問題 給定兩個字串形式的非負整數 num1 和num2 ,計算它們的和。 注意: num1 和num2 的長度都小於 5100. num1 和num2 都只包含數字 0-9. num1 和nu
C#LeetCode刷題之#441-排列硬幣(Arranging Coins)
問題 你總共有 n 枚硬幣,你需要將它們擺成一個階梯形狀,第 k 行就必須正好有 k 枚硬幣。 給定一個數字 n,找出可形成完整階梯行的總行數。 n 是一個非負整數,並且在32位有符號整型的範圍內。 n = 5 硬幣可排列成以下幾行: ¤ ¤ ¤ ¤ ¤ 因為第
C#LeetCode刷題之#605-種花問題( Can Place Flowers)
問題 假設你有一個很長的花壇,一部分地塊種植了花,另一部分卻沒有。可是,花卉不能種植在相鄰的地塊上,它們會爭奪水源,兩者都會死去。 給定一個花壇(表示為一個數組包含0和1,其中0表示沒種植花,1表示種植了花),和一個數 n 。能否在不打破種植規則的情況下種入 n 朵花?能
leetcode刷題--基礎陣列--旋轉影象(C)
給定一個 n × n 的二維矩陣表示一個影象,將影象順時針旋轉 90 度。 說明:你必須在原地旋轉影象,這意味著你需要直接修改輸入的二維矩陣。請不要使用另一個矩陣來旋轉影象。 示例 1: 給定 matrix = [ [ 5, 1, 9,11], [ 2, 4,
leetcode刷題--基礎陣列--移動零(C)
給定一個數組 nums,編寫一個函式將所有 0 移動到陣列的末尾,同時保持非零元素的相對順序。 示例: 輸入: [0,1,0,3,12] 輸出: [1,3,12,0,0] 說明: 必須在原陣列上操作,不能拷貝額外的陣列。 儘量減少操作次數。 思想:這個和之前
LeetCode刷題記之兩數之和(一)
spa push amp twosum == leet div bsp 重復 題目:給定一個整數數組 nums 和一個目標值 target,請你在該數組中找出和為目標值的那 兩個 整數,並返回他們的數組下標。 你可以假設每種輸入只會對應一個答案。但是,你不能重復利用這個數
C#LeetCode刷題之#819-最常見的單詞(Most Common Word)
問題 給定一個段落 (paragraph) 和一個禁用單詞列表 (banned)。返回出現次數最多,同時不在禁用列表中的單詞。題目保證至少有一個詞不在禁用列表中,而且答案唯一。 禁用列表中的單詞用小寫字母表示,不含標點符號。段落中的單詞不區分大小寫。答案都是小寫字母。 輸入:
C#LeetCode刷題之#521-最長特殊序列 Ⅰ(Longest Uncommon Subsequence I)
問題 給定兩個字串,你需要從這兩個字串中找出最長的特殊序列。最長特殊序列定義如下:該序列為某字串獨有的最長子序列(即不能是其他字串的子序列)。 子序列可以通過刪去字串中的某些字元實現,但不能改變剩餘字元的相對順序。空序列為所有字串的子序列,任何字串為其自身的子序列。 輸入為兩個字串,
C#LeetCode刷題之#374-猜數字大小(Guess Number Higher or Lower)
問題 我們正在玩一個猜數字遊戲。 遊戲規則如下: 我從 1 到 n 選擇一個數字。 你需要猜我選擇了哪個數字。 每次你猜錯了,我會告訴你這個數字是大了還是小了。 你呼叫一個預先定義好的介面 guess(int num),它會返回 3 個可能的結果(-1,1 或 0): -1 :
C#LeetCode刷題之#278-第一個錯誤的版本(First Bad Version)
問題 你是產品經理,目前正在帶領一個團隊開發新的產品。不幸的是,你的產品的最新版本沒有通過質量檢測。由於每個版本都是基於之前的版本開發的,所以錯誤的版本之後的所有版本都是錯的。 假設你有 n 個版本 [1, 2, ..., n],你想找出導致之後所有版本出錯的第一個錯誤的版本。 你可
C#LeetCode刷題之#709-轉換成小寫字母(To Lower Case)
問題 實現函式 ToLowerCase(),該函式接收一個字串引數 str,並將該字串中的大寫字母轉換成小寫字母,之後返回新的字串。 輸入: "Hello" 輸出: "hello" 輸入: "here" 輸出: "here" 輸入:
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 的小寫字母。字串的最大長度是