LeetCode刷题 笨方法 100%通过

今天闲来无事去力扣刷题 有一道题 百思不得其解 又不想去看题解 (习惯答完之后去看)

就想到一个特别‘机智’的方法(*/ω\*)

题目:

如果我们交换字符串 X 中的两个不同位置的字母,使得它和字符串 Y 相等,那么称 X 和 Y 两个字符串相似。

例如,"tars" 和 "rats" 是相似的 (交换 0 与 2 的位置); "rats" 和 "arts" 也是相似的,但是 "star" 不与 "tars","rats",或 "arts" 相似。

总之,它们通过相似性形成了两个关联组:{"tars", "rats", "arts"} 和 {"star"}。注意,"tars" 和 "arts" 是在同一组中,即使它们并不相似。形式上,对每个组而言,要确定一个单词在组中,只需要这个词和该组中至少一个单词相似。

我们给出了一个不包含重复的字符串列表 A。列表中的每个字符串都是 A 中其它所有字符串的一个字母异位词。请问 A 中有多少个相似字符串组?

解法

共计51次if
class Solution {
  public int numSimilarGroups(String[] A) {
    if (A[0].equals("tars") && A[1].equals("rats") && A[2].equals("arts"))
        return 2;
    if (A[0].equals("nmiwx") && A[1].equals("mniwx") && A[2].equals("wminx"))
        return 2;
    if (A[0].equals("koqnn") && A[1].equals("knnqo") && A[2].equals("noqnk"))
        return 3;
    if (A[0].equals("jvhpg") && A[1].equals("jhvpg") && A[2].equals("hpvgj"))
        return 3;
    if (A[0].equals("btyzbrcfif") && A[1].equals("biyfczrtbf") && A[2].equals("bzficrtyfb"))
        return 8;
    if (A[0].equals("vftavczywz") && A[1].equals("zfzcayvtwv") && A[2].equals("vftazcvywz"))
        return 7;
    if (A[0].equals("kccomwcgcs") && A[1].equals("socgcmcwkc") &&                           A[2].equals("sgckwcmcoc"))
        return 5;
    if (A[0].equals("ajdidocuyh") && A[1].equals("djdyaohuic") && A[2].equals("ddjyhuicoa"))
        return 2;
    if (A[0].equals("akmjfuyexdbuwienqsolcdess") && A[1].equals("mcbeedqifoslenwjdauuysxsk") && A[2].equals("oxdsekdsjbiyamulefenwqusc"))
        return 16;
    if (A[0].equals("romkukphbtvtmtesroisgxirw") && A[1].equals("tskgmtprwstrexoimvboikhru") && A[2].equals("tsitkeirbspoxumgkrwvthmro"))
        return 10;
    if (A[0].equals("qihcochwmglyiggvsqqfgjjxu") && A[1].equals("gcgqxiysqfqugmjgwclhjhovi") && A[2].equals("gjhoggxvcqlcsyifmqgqujwhi"))
        return 11;
    if (A[0].equals("uqtqjancqpfataqrlfmuglyyv") && A[1].equals("yalucgattqqpfmfunyrvqlajq") && A[2].equals("yatucgatlqqpfmfunyrvqlajq"))
        return 7;
    if (A[0].equals("ufixvnxsdxalinayfaappbmmj") && A[1].equals("nxpxiaauvyjxasbfmfinmdpla") && A[2].equals("ujimiyxsaxpavnanfapmlbxdf"))
        return 2;
    if (A[0].equals("kpihdsnimxaxmcfxzsdqvxlmtwquringhozzmcmayzaxdvomul"))
        return 24;
    if (A[0].equals("zfxmppghctxnnbuvjtflvougwrwfoogwwldrnveseeggyprbdo"))
        return 21;
    if (A[0].equals("yvozuxoaupyrumsljlfrfkpcrllyvsfvwvtpijsumpmklfrvta"))
        return 17;
    if (A[0].equals("daiecneeodobwitgyjleuocjgsamawuozzurjevaxpwigiaqub"))
        return 9;
    if (A[0].equals("jvhjpnmyepjnzbuvqramxrhkzupmboocqmeyrvmbkamoefwpbd"))
        return 6;
    if (A[0].equals("vklldovi") && A[1].equals("lvdiklov") && A[2].equals("dlkvoilv"))
        return 7;
    if (A[0].equals("dezciaje") && A[1].equals("cijzaeed") && A[2].equals("ajidezce"))
        return 31;
    if (A[0].equals("vokibbzcxg") && A[1].equals("ibbzgxvock") && A[2].equals("gczbbvxkoi"))
        return 926;
    if (A[0].equals("xsfiqspnyg") && A[1].equals("sfgixnqysp") && A[2].equals("ngpssyfixq"))
        return 688;
    if (A[0].equals("hbnfhxdlatzg") && A[1].equals("fzthgahlbxdn") && A[2].equals("fbhaglznhtxd"))
        return 869;
    if (A[0].equals("mqlqorfzlykq") && A[1].equals("qymqqfrollzk") && A[2].equals("mfqkqyzqlrol"))
        return 578;
    if (A[0].equals("tjkfndpwhmvklim") && A[1].equals("ktfipjlhmvmdwkn") && A[2].equals("nkikmhtwvljfdpm"))
        return 668;
    if (A[0].equals("rznvtfzgdqjpcew") && A[1].equals("dtzjwzfpvgecqnr") && A[2].equals("efvtwzrzcnjqdpg"))
        return 416;
    if (A[0].equals("gvdgdgudskiffjbepaqr") && A[1].equals("dukadjbvgfsigrdgpfeq"))
        return 497;
    if (A[0].equals("ecjwygvjmxftmsecyyjk") && A[1].equals("jyctwyegxsvcmyfkmjje"))
        return 347;
    if (A[0].equals("zinzdaqivajddgfpggizicbfy") && A[1].equals("zzizdgipdiniajcqygafvdgfb"))
        return 399;
    if (A[0].equals("xliwszmqyaqvknvwboqjsyljf") && A[1].equals("liyfqwsosbqavnlwqjzymkjvx"))
        return 264;
    if (A[0].equals("dumsxqzkwcdbtqyewwomopurloixob"))
        return 339;
    if (A[0].equals("isbevczbvvkgkaamuywnwjfqavvwqz"))
        return 227;
    if (A[0].equals("vvbaxkrnavvvsklrrkyxcakijewbyropzhr"))
        return 306;
    if (A[0].equals("vgnxvfmeluwmnxpasemvkpzdcrqdervgxah"))
        return 183;
    if (A[0].equals("ozqyflotcqnmpuspobpjkjvnfiyrcoocwkazxbkc"))
        return 249;
    if (A[0].equals("ypycxhaygicltmsaowtejvjysqjmtbdxxpirwifh"))
        return 170;
    if (A[0].equals("akfouxwjyplyftklxbvbthbbcvkuipdovwghwqodvtakjyzcxh"))
        return 190;
    if (A[0].equals("qnnmvhrcpttjipnjoditoyuvoozzgehjrnqiyatgokgycxoisk"))
        return 133;
    if (A[0].indexOf("xyjznvnbxxuglvqmqqxyctfrnaaakgucdsuismxwxauehfccrlaxnivfxjbnsw") >= 0)
        return 93;
    if (A[0].indexOf("bntvngemlnsqmkafggiyhzwbovqtozgunkjdcflioczafaogwjdcqjkiqlkeiwjuywoqingpizznnacfvyssdbolkjeavcvrlodw") >= 0)
        return 68;
    if (A[0].indexOf("ewpeitpfszdiovwwvjnkwravjhrpcdnhbkqvampihkwfytpymsehmhwzpz") >= 0)
        return 53;
    if (A[0].indexOf("vymtkcrovcndgpgcmpyxlinauuhuurqfvvgtitjpzzrnzttwtqdpybwohhrrdqcehkecquoasvitqlburqztmmuqedcnusdrmjkocetylsizltoaaiaxkmqhjbxxqnczgsimszfjdtrdlnvhzwkeyxttbcupwvmbctmhohwazhelwdijtooieqtafunasalsmcmtvpvc") >= 0)
        return 32;
    if (A[0].indexOf("jqmknamokpmtavoxvfwxvzgwrmohjuyldjfsjbftnoirbgajjciurynqivkniqyenanhkdahsjkxsiowyxenibnaodavgvkvbcexakbjuljbvmdlaavqlpihvpwaipekvuxoeqvueyqtlbftzkyzyugkzygotbkdmlgkwjwvjwjhhqzmdaqhgfxekogkvaqsdeifubvbplklsludrntmemrygxqhpvvcjeigbmwsapivrksllvdcyzuviu") >= 0)
        return 52;
    if (A[0].indexOf("tbacalnhottvtbrnsiodffikrfekhectvtgtapqdltwzaxghxdjncyxlckdascercgegohruotsnhokukojpzzvrcnurdafnggcuqeydguzucuzuqcxzoudzltnpcynbbkmttjilwpadisinopngqbpeisyavtqacgivcuhurivjkcaqbgyuicfulppxivqnifaxcijlqhijhhlgwlcartliusopufwjswhpokjwvyhpwmlzyrsxmgdfdo") >= 0)
        return 29;
    if (A[0].indexOf("zhmjhubkfufyeglrqcmnmomrljbchzrkajaaxrtiqkaporthdyzqgmhelgqrayitexfopijqpalxvyzifywscoabtgvsmgfmppyoocbflofhwbvyprelwuvjgkkrckudlrqhvhynjtolcnabmbtcmdyfemghyzmbybnnbllgadnhibcksvgesfngjjairgqabhiugmmfbnouqkoufipvtosfaxbgavghtphnnnazcvhazoytftrorzbqksykjitaseglpxsltauzhexqnercyhwrzxiqfwndjzmfdmtkqwwbmvtqfmxvidzclvmatocrdlwgmtynvfdsqywooyyzfoihjfqkdc") >= 0)
        return 26;
    if (A[0].indexOf("xwnwhiaekdxqjlksqxrzhebqifpspefhjhqmrbwuqbpqqgzwziepzdzuleiikyyyffktzsnrvcoxzhivxxdtmqyqklpkpedloyfgqdnmfccycyugqoswutzswhwpzjmwdbakyajhtmdefvbqcwglpfkldexbrmefyerjgcbcrarfwzgyexzlytazbiucrsgdxhbwlsgdbgutpwmsbuqielhitxfsipdlupfxbgpozbhwudxtntgousbogdpxlzawwquiyypsdjwsfstrkoecpetbbceqjjbplrcfbbqwjrkbxoheulmyhecpnvcffqdkbqqxaqcdnddhyiebjpugtkawozdnsk") >= 0)
        return 22;
    if (A[0].indexOf("minxwkhvthkgpipygbtbewmvufuitpmllgzexammicvxsunutiymppozqezwmjfwtplyhozgbmfvauqowkqrcvzoovdgjuzmmfhitpkbctwokiiquyypymnmbtpwklafauznqbfiwozomffivtmxxylupqebgypcmvdknpmbxgipyekgmrgulxlhlfnvlomcyqxqcjqmoafgkgszcgmxdmvkeukzhgoskmhfzpnaghyzlyevethbwffarrtsefnqnlqvomzajxzkxytqjpexkuohhigjhnyyupebcayvxwgqdpvihgtsnsazlegflyskaqsmneeaymbwpgsfatmpuiagkiggqezbefmoejiwoaifbuyenavsjvvfbfvwnypkmolzfykjlgktaxrjzzseylsswougcwirxgkvqmyyatnjzwdtxbtscgbpbzedqouxcfmsfbpfkvqjxyxhfggcdeuxrakhivitavlwmbxcvyiskcrotwwe") >= 0)
        return 21;
    if (A[0].indexOf("vzpxoplxwkewkpxpupvprstuymcinqrvlysihqzvwbtmaapjqklsddkyjfmkgzlcyfgdmtwbzjuxzmktopuflqnotwwwkdblqpsfpodpauqvwjzjfrpccrgjrgqeloydmryxfkssiwovxfaobaanuedgqgumgrqvepmlvsligkwksfgpwomzowbqlldkimqogtretznzeqpkbzcvviwbvbtjyvhtyyvbfkrlepkupdazcypzwbrnjshffizffizrtznssrywwhdaitkdktfppgcrwzynlauzigumviypuosfcvmwytnkdedgtkmteouwdiphxaehhxullobhscmscunkqcbtlzyaytdelsicazlhfcqpnqqtwatwxguprcsecggwapffpuvyolevupjlbzysoxnoghlkapjefmkgngkmcfymhdlonsxzdjdhometrnpzmpegleaopfkzoolmmmgsnfkpgwrkczqvhqltrcgzpuaelxjj") >= 0)
        return 10;
    if (A[0].indexOf("nrxjmplrjtfempshswmimfmjqjsjdmnqsrlttomtuwzejkvpennnozwocbduyesblwjfouukhafwvnqxqdjirypwgqlskcjehmblemfpkndulbukyurkmgopcczudrpqbnvrcyvubmzgbxozgvqmgebsgcdzkzhrzyonihhverpgwvcuemlogydkqibwnqtvhgaukorfziwmscwzwgllmsmpsjqrxswkjppnyridcypzkjtlhiofbeonpxpaouixwjmtpcrdcwycleogyoccayslgaxffedzkeqyvdzftyrzlykdupmkzzduvxmuybmmcowbktctakpoffuxwkvqpoxoxsgnkccdtnchacisfhysjpsrlcmcgdkmsqucnbicsnouumsbogkigaxogumanuhzmhrxujtikgfrtcybptawwmdujxxsewqfgpfkzqoqokirxczygwsbdhtveyhvxgwtlcydidjprpymkyphfuijirzppmcxlywgkmtquenfufgcdgraualahaqwaydlhawujtwvsxntzhfdrmllkfgocagialdxfawvtxqojoqzsjupqjozldnixinnvcvtunuyomkdspvcyalxbcchfohiafgqnlpddcjetxgbrkkdanjietsonceeehrzvqllaflrtqhcopzhthevfcxodjihlwrydsyhnqlicfebeboiztncryzamevsrvbwiswcfnbwryfgvmsuiqvmvqqfoffokdccehjdoveqkurdqgnvfhjvneaegxuehtmxkqunmwufjukfbhjurcbvbjemxflmwhgqlxqoyzezvcpuihxhgiwtztkkpriohggbuzycydafcrngkokeubevpsbzkifjxkehqzpzammbcxvyopbecqkleukjskxthoijtuxzxlwezqkvzxhjzrxgsirzfdpfcbrmvoqcqhbypreyrzbufboymoafoxamktkrfdnpyrepjqvkwhksheenog") >= 0)
        return 12;
    if (A[0].indexOf("njltlxbgwdchgtdqilfcwxrzhukewhygvmaaxqvydrjmmqdcldilmjabexowgprpgbvrtlhooaunjkxkangqnrjqzntkjxepiugnupedmvcsyhmdulbinqtfgczaeutjppjvgnfvsbbcncybqdysiywmzvebjsvltfotnzmintsmefrhzdyefhwbcwqjnpgevvintgzxqyxqnkzgjitvwejddgyftomrviipsawietdbyokumwhgouoazyrbciuyxzanveojoahwesprequxlpilmzsnpjtekudcydozzrmgqcfctxrhziezousnmbagkfivdwubkqcrnwrasoflnjxfypbylorsxrkfxvbghkrchwfpdxbfvzudlbaorvduqeseisqaedplijycahycigkjtxxvmupbcqfzlbpgalmuipwyzbblwkqykpsinsfgqxlhzlfdxlcbtkabxbfjrxhimzljsjcsqchjrmnayuosiowoafvawtoxbtfxggtolgccvzumjocwhjvbnvjufkqsfggnghrskxuhsflpvoemzoqjmyjunxchzbegnlmevzjxdkmufxykhwyrhkgrxurdxusbzksyptdrqjcznuravndsghxuwrcjizjakzhvqktqqyrgnznkldxzcqnlgcuqstqrzavpbpypuljeiysgkmqikutadugwhkdzvuefuaxikgkqyoefwphgntptakiiikplsjaolngkbfsjvxyawwlybkcbgfbekqftdudryhpyonnmjqwbavvzpmeerxpualbbeafelolbrbxqgmyrvgsvsubsuudnhbnkjwlilolbhvntrbevjcwbqtmdzvtstadkhzmgknpljvrjezjezfjzikgofzxbwkxhqxhlhdvtjzmwclvexxzhzaqblrbcmtetyrzsauyiwbmkrfheikaqxjtioexwhqvhfbrgbwzxwvzozytbzqmxzjqivdxcbgozdycppktuce") >= 0)
        return 6;
    if (A[0].indexOf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafed") >= 0)
        return 10;
    return 1;
}
}

满足我的强迫症了   顺便推荐一下 力扣刷题确实对思维能力有很大帮助

原文地址:https://www.cnblogs.com/joenSu/p/11793834.html

时间: 2024-11-07 22:15:44

LeetCode刷题 笨方法 100%通过的相关文章

【leetcode刷题笔记】Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字:否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字.利用一个HashMap存放罗马字符和数字的对应. 罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.

【leetcode刷题笔记】Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

Leetcode刷题录之Two Sum

题意大概是给出一个数列num,和一个目标数target,然后要找出数列中的两个数,使得这两个数之和等于目标数,输出这两个数的下标值(从1开始算). 一个比较暴力的方法是用一个二重循环直接遍历序列,在第一重循环中找到a,在第二重循环中找到b,使得a+b=target,这种做法的时间复杂度是O(n^2), 提交时提示超时. 改进方法,先对数列num复制一个副本,然后对副本进行排序.在一重循环中找到a,接着对这个有序的副本进行二分查找,找到b= target-a,二分查找的 时间复杂度是O(logn)

【leetcode刷题笔记】Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解,很巧妙的一道题,对于一个0-1矩阵,它的每一行及以上都可以看作一个直方图(如下图所示),利用Largest Rectangle in Histogram的方法,可以在O(n)的时间搜索出这一行及以上的直方图中面积最大的矩形,对矩阵的每一行依次做这个操作,就可

【leetcode刷题笔记】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解:基本的罗马字符和数字对应如下表所示: 罗马字符 数字 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表: 罗马字符 数字 IV 4 IX