zoukankan      html  css  js  c++  java
  • Lc15-三数之和

    三数之和

    import java.util.ArrayList;
    import java.util.List;

    /*
     * Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

    Note:

    The solution set must not contain duplicate triplets.

    Example:

    Given array nums = [-1, 0, 1, 2, -1, -4],

    A solution set is:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/3sum
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
     */

    public class Lc15 {
        /**
         * 双指针,排序,重复数字判断
         */

        public static List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            if (nums.length < 3) {
                return res;
            }
            // 排序
            shellSort(nums);

            for (int i = 0; i < nums.length - 2; i++) {
                // 减少循环
                if (nums[i] > 0) {
                    break;
                }
                for (int left = i + 1, right = nums.length - 1; left < nums.length && right >= 0 && left < right;) {
                    // 减少循环
                    if (nums[i] + nums[left] + nums[right] > 0) {
                        right--;
                    } else if (nums[i] + nums[left] + nums[right] < 0) {
                        left++;
                    } else {
                        List<Integer> tempResList = new ArrayList<>();
                        tempResList.add(nums[i]);
                        tempResList.add(nums[left]);
                        tempResList.add(nums[right]);
                        left++;
                        if (!res.contains(tempResList)) {
                            res.add(tempResList);
                        }
                    }
                }

            }
            return res;
        }

        // 希尔排序
        public static void shellSort(int[] nums{
            for (int gap = nums.length / 2; gap > 0; gap = gap / 2) {
                for (int i = gap; i < nums.length; i++) {
                    for (int j = i; j >= gap && nums[j - gap] > nums[j]; j -= gap) {
                        int temp = nums[j];
                        nums[j] = nums[j - gap];
                        nums[j - gap] = temp;
                    }
                }
            }
        }

        public static void main(String[] args{
            int[] nums = { 82597-92436239083030-97960-26521-6101183390-3867712333759874609183794,
                    19355-71037-6242-288013241202-90885-2989-95597-3433335528568089093-90606,
                    50360-29393-27012533136521399818-82405-41661-3333-5195272135-15232637774685,
                    9699292263159295467-99555-43348-41689-60383-39903216565265-72973-5837212741,
                    -48568-4659672419-1859341536293781310-61823-96770-549448845-9118424208-29078,
                    3149565258141988539570506-4090856740-12228-40072324299300168445-7392725731,
                    -91859-2415010093-60271-81683-181265105548189-64682505781194-586287404266158,
                    -14452-49851-436671109239189-17025-79173136068317292647-5974119343-26644,
                    -5760782908-206551637800609899439331-31274-6152391225-7295313211-75116-98421,
                    -41571-69074995873934542151-24609823615690-52507-95803-48935-46492-45606,
                    -79254-99851525337348639948-724071815-585-9625290990-9381593340-7184858733,
                    -14859-83082-75794-82082-24871-1520691207-56469-9361867131-86827571987429,
                    -98757-7535-24890-9416085003339287553897456-66424-60074-8527-28697-223082246,
                    -70134-82319-1018487081-34949-28645-47352-83966-60418-15293-53067-2592155172,
                    75064958594804934311-86931-3858633686-367149692276713-22165-80585-34503-44516,
                    39217-2845747227-940364345724626-8735926898-7081930528-32397-6948684912-1187,
                    -98986-329584280-79129-656049344589645058471128-554802498615086-62360-42977,
                    -49482-77256-36895-74818203063-4942628152-97329608686035-887433524144249,
                    19927-106608940424179-26621-651157745-2875096340-97160-97822-499795230779462,
                    94273-24808771049255-83057776552136155956-909648599-40490-55107268929608,
                    2049766834-3467823553-81400-66630-96321-34499-12957-2056425610-4322-58462,
                    20801537007152724669-5453457879-322133636390097832-27688-98715599224520,
                    -55401-57613-6992657377-77610201235217486060429-91994-62403-6218-90610-37263,
                    -1505262069-964654425489892-340619121-41842-87783-64125-5612073904-22797,
                    -58118-48665356753184611921276-19246-9241-9742557333-158029314925689-5532,
                    9571639209-87672-29470-16324-1533127632-3945456530-16000298534647578242-46602,
                    83192-73440-1581650964-366018975838375-40007-36675-940306757646811-6491945595,
                    7653040398358454179167697-30439-829446311533447-36046-50122-3478943003-78947,
                    -38763-8921032756-20389-31358-90526-8160788741866439842247389-751891309195993,
                    -1550194260-25584-1483-67261-707532516089614-90620-4854283889-12388-9642,
                    -37043-6766328794-88011362112241553798429021692-95906-85617-17341-6376780183,
                    -4942-5147830997-13658883817452-82869-39897684493196498158-4948962283-62209,
                    -92792-5934255146-3853320496626676259336095-124705453-5045174716-179023302,
                    -16760-71642-3481996459-728602163847342-69897-4018044466764968465913848-91600,
                    -90887-63742-2156-84981-9928094326-3385492029-5081198711-36459-7555579110,
                    -88164-97397-84217974576438730513-53190-832152522344-27177-92945-8901082662,
                    -11670860695341742702970823695-14530-46334179107799928009-1237415498-46941,
                    97088-350309504092095-59469-247614649167357-6665837446-65130-504169919730925,
                    2730854122-4471912582-99525-38446-69050-2235294757-5606233684-40199-46399,
                    96842-50881-22380-650214058253623-7603477018-97074-84838-22953-7420579715,
                    -33920-35794-9136973421-8249263680-14915-332953714576852-6944260125-74166,
                    74308-1900-30195-16267-60781-2776058523891725742-376549097-6354198612-92865,
                    -302489612-87985326295781-42278-365297252-27394-50215917880934-48480-75131,
                    -54439-19145-4814098457-6601-51616-897307802832083-4890416822-81153-883248720,
                    -80728-45133-86647-4259-4045325902861350523-4105-27790-74579-172236372133489,
                    -4792197628-97691-14782-6564418008-93651-7126680990-76732-471043536828632,
                    59818-86269-8975334557-92230-5933-3487-73557-13174-43981-43630-5517130254,
                    -83710-99583-13500717875017-25117-7858686941-3251-23867-363157597386272-45575,
                    77462-98836-1085970168-32971-38739-127619341014014-30706-77356-85965-62316,
                    63918-59914-640881591-109573800415129-83602-5179134381-89382-2605689425465,
                    71458-73805-87445-19921-8078469150-3416828301-6895518041605982342994739795,
                    44047-573134856981936-2863-8093232976-86454-8420733033328679104-16580-25727,
                    80157-701695374186522846516848084018619327332-61322-69663763704120612326,
                    -346891701682975-23386394177279344774-9625932137995229265-61492-4933714162,
                    658863342-41622-62659-90402-247518851154739-21383-40161-96610-24944-602-76842,
                    -218566996443994-15121-855301271813170-135476922262417-75305-81446-38786,
                    -52075-2311097681-82800-53178114743585794197-58148-236893250692154-64536,
                    -73930-7713897446-83459709632245268472-3728-25059-4940595129-61671280899918,
                    30113-12641-2666586362-3350550661267143370189012-9154040517-12716-57185-87230,
                    29914-5956013200-727235827223913-45586-96593-26265-2141310878139992511-34049,
                    2057728032600389404211740887-827153826940969-500227208821291-67280-16523,
                    905351866994342-39568-88080-99486-2071623108-280376334236863-29420-4401675135,
                    7341516059-48998689343136-704133483-676122532740830618461805424781119-22854,
                    -26104-6346663093-63685603695102351644-1635074438-835149908310079-58451-79621,
                    4847167131-869409909311855-22272-67683-4437195411812337766-7092280385-57513,
                    -76021-47890361547293584387-92681-88303-781059902-90-64704-28396-664038860,
                    133433388285680722828160-1400354369-5889392606-63492-10101647145848629948,
                    -44679-2276310151-566954031-18242-3623286168-142639883471244727192761-24958,
                    -73263-79661-69147-1887429546-92588-8577126451-86650-43306-59094-47492-34821,
                    -91763-47670335372284367417-759921596307594065-26988552766590330414-67129,
                    -99508-83092-91493-5042614349-83216-7609032742-5306-93310-60750-60620-45484,
                    -21108-58341-28048-5280369735789068164932565-86804-83202-65688-17608970793322,
                    -727508413471900-3772019450-7801822001-2360426276-2149865892-72117-89834,
                    -2386755817-779634251893123-8391663260-2243-9710885442-3677517984-5881099664,
                    -1908293075-693298706179713162967099613483-7458249900-27669-405621209-20572,
                    3466083193755797344649258836160969311444611-2744553049-16085-92851-53306,
                    13859-3353286622-75666-18159-9825651875-42251-27977-180802377238160417799147,
                    9417599905-8575562535-88412-52038-6817193255-44684-11242-1043179662346-54931,
                    -55790-700324622156541-9194790592935034071206464856-6359815396-5070832138,
                    -8516438528-899595385257915-42421-88916-7507267030-2906649542-7159161708,
                    -53985-430512848346991-8321680991-46254-4871639356-8270-47763-34410874-1186,
                    -7049288461127621960-13304-11433-4913557547961670423-27523648034927714906,
                    -97401-92390910757073621971-330355333-939967653854603-75899988014688735041,
                    48302-52318554392457414079-24889834401496134312-89260-22293-81271-2586-71059,
                    -10640-93095-5453-700416654374012-11662-52477-37597-7091992971-17452-67306,
                    -804187225-89296242968654737154-1069674436-639595886033590-88925-97814-83664,
                    85484-8385-5087957729-74728-87852-15524-911202206228134809173202649707-54252,
                    -44319-351391377744660852742504358781-89035-762746364-636257285543242-35033,
                    12820-2746077372-47578-61162-70758-1343-41596493556024-21514377019758-30186,
                    -8604024666-62332-6754273180-25821-27826-45504-36858-1204120017-24066-56625,
                    -52097-47239-9069489597712-14258-58605534961808-4423-9370364681-98641-25222,
                    46999-83831-5471419997-684776607351801-6649152061-5286679907-39736-6833168937,
                    91464988929109350131295-8587327036-573405041221-2445294717131782093-94823,
                    -54458-9741039560-762866452397015402937906467735829660370-6109085501-86874,
                    71443-72702-72047148483410277975-66294-365763134952493-70833-802879443539745,
                    -9829184524-1894210236934485084694023-69394799914740301658104884935-19177,
                    -135943228962628-90612-542-666276425571199-83841-82943-738858623-67214-9474,
                    -3524962254-14087-9096921515-8330394377-9161919956-9881096727-9193929119,
                    -85473-82153-690084485074299-76459-864648315-49912-2866559052-6970876024,
                    -927385009818683-9143818096-19335356599182615779-7307067873-12458-71440-46721,
                    5485697212-8187535805369526849881627-342318171227100-9741-8261218766-36392,
                    275941728697432682548355-1779017165565583295-2437555669-161092407973414,
                    48990-11931-78214907451987835673-15317-8908694675-9251388410-93248-19475,
                    -74041-1916532329-26266-46828-18747453288990-78219-25874-74801-44956-54577,
                    -29756-99822-35731-18348-68915-83518-5345195471-2954-13706-8763-21642-37210,
                    16814-60070-4274327697-36333-423621157685742-8253668767-56103-6301271396,
                    -78464-68101-15917-11113-359677626-60191-30585-735846214-843031840323618,
                    -15619-89755-59515-59103-74308-63725-29364-52376-9613070894-1260950845-2314,
                    42264-7082564481557524460-68603-887014713-50441-51333-7790797412-66616-49430,
                    60489-85262-97621-1898044727-69321-5773066287-92566-64427-1427011515-92612,
                    -876456155724197-81923-39831-10301-23640-76219-6802592761-7649368554-77734,
                    -95620-11753-5170098234-68544-618382946746603-18221-354417453740327-58293,
                    75755-57301-7532-9416318179-14388-22258-46417-4828518242-7755182620250-20060,
                    -79568-7725982052-98897-7546448773-79040-1129345941-67876-69204-46477-46107,
                    79260546-34573-12879-9456220356-48004-62429962424059420999949425724-39394,
                    -2388-18563-56510-83570-292143015744547419776678-4659760630-7609337578-82045,
                    -2407762082-87787-749365868712200-9895270155-7737021710-84625-60556-84128925,
                    65474-15741-9461988377893344474922002-45750-93081-14600-834474669185040-66447,
                    -80085563084431024979-29694579914675-71273-4450813615-5471023552-78253-34637,
                    504976870681543-88408-214056001-33834-21570-46692-253442031071258-9768011721,
                    5997759247-4894998955-50276-80844-27935-7610255858-334924068066691-331888284,
                    64893-75286019-855238434-64366-566632686230008-7611-12179-7007621426-11261,
                    -36864-61937-59677929-210523848-20888-1606598995-32293-86121-545647783168602,
                    749773165840699297559842480358-693372633913213-46016-1833164713-46883-58451,
                    -70024-92393-408870628-5118571164-75791-1636-29102-16929-87650-84589-24229,
                    -42137-15653948251304288499-47100-90358-718029754-65727-42659-85560-9037,
                    -5245920997-47425173182112220472-2303765216-63625-7877-9190724100-7251622903,
                    -85247-8938738785495387480-31466-9952435369-7837689984-1598294045-726923319,
                    -80456-37653-7675629098193654958-1239360560-84664-8241366941-26573-9753264460,
                    18593-85789-38820-92575-43663-8943583272-5058513616-71541-53156727-2764416538,
                    3404957745343483500916634-1879123271-638449581721781165905966915966-6864,
                    48050-3614397427-593909693178939-19585077743338-5114939235-27054-4349267457,
                    -8361637179103908581823917363587579-49127-81264-79023-8159053554-74972-83940,
                    -13726-390952917478072761044777825797-29515-6493-9279322481-36197-6556042342,
                    157509755699634-56048-356881350163969-74291509113922593702-3490-59461-30105,
                    -46761-8011392906-684875074236152-90240-8363124597-50566-15477184707703840223,
                    -80364-9867670957-636479953713041316798663137633-1686613686-7156521652-46053,
                    -80578-6138268487-641746562081167013-30868-112194674944146275696542275,
                    -5248052162-84883-52579-903319279242184-73422-5844065308-250695475-5799659557,
                    -175612826-5693914996-94855-537079915943645-67719-133121412417043161232622,
                    1919-69333-6982822422-7884257896-1736327979-768973500846482-752896579920057,
                    717041326-7606990840-81253-507493649-4231545238-33924621019690658884-7617,
                    -28689-665786245850876-57553673941014-64040-349163794013048-97478-11318-89440,
                    -31933-40357-59737-76718-14104-3177428001410341702-25120-3165463085-364284870,
                    -83896-76422-6152012900886788554733132-886275282063915-2747278867-5143933005,
                    -23447-3271-3930839726-74260-31874-3689393656910-9836260450-880489930813947,
                    83996-90415-3511770858-55332-317219752882982-862186822252273694697077-4257,
                    -41526567958987075860-708022177914184-16511-89156-314227147069600-7849874079,
                    -19410403112850126397-67574-32518685103861519355-6088-97159-29255-925233023,
                    -42536-886816425541206441195220839522-5210891276-705148343663289-797419623,
                    99559126428595083735-21156-6720898088-7341-27763-30048-44099-14866-45504,
                    -91704193691370010481-49344-856863399419672360286084266564-2491933950-93616,
                    -47430-35391-28279568067469039284-96683-7642-7523237657-14531-86870-9274,
                    -261739864088652642574645737814-193709337-22556-4152539105-2871951611-93252,
                    98044-9099621710-47605-64259-3272753611-31918-355533316-6647221274-37731-2919,
                    1501648779-8886818974172846344-896673784868092-4401185354-4377638739-31423,
                    -6633065167-220165940534328-6004287660-67698-59174-1408-46809-43485-88807,
                    -60489139742231955836-62995-37375-418532687-36551-752375828026942-7375671756,
                    78775-4057314367-71622-773382411223414-7679-517218749285066-216125704510673,
                    -9683652461-62218-931065862-2274889906-96987-9869826956-43428461414745628095,
                    5595267323-36455-60202-43302-8293242020770361014260406703316383658850-66752,
                    5210921395-10238-98647-41962277786906098535-28680-52263-5667966103-4242627203,
                    8002110153586783639863112349112051562082-15659-407852705443767-2028965838,
                    -6954-60228-7222652236-3546425209-15462-79617-41668-8408362404-6906218913,
                    46545207571380524717-18461-47009-25779688346482434473395763157014861-15114,
                    -4123395509682326784684902-8306017642-184227368877671-2693064484-9963773875,
                    642821034-7347119664-6803115922-270284813754955-82793-41144-10218-24921-28299,
                    -228868518-5445215686-4181466165-72207-619868002050544-99500162447899840989,
                    14525-56061-24692-947902111137296-907947210070550-3175717708-742906191078039,
                    -78629-2503373172-91953100526450299585-174190324-7372368942281493021824422,
                    1665910710-625949424996588461923425173500-65995-8116841412-98724-63710-54696,
                    -52407197464586927821-94866-76705-13417-61995-715604345067384-8838-80293,
                    -2893723330-89694-405864691880429-54757801325309-3416237236-775778674426281,
                    -29033-918133534713033-13631-244593325-71078-75359813111970047678-74680-84113,
                    4519235502376751955376522-51098-18211897174508-82946277498599589912-53678,
                    -64727-1477832075-63412-4052486440-2707-3682163850-3088367294-99468-23708,
                    34932343869889929239-233855897548829866049098702751771888533521616334050061,
                    -8945719491-9915624873-1700864610-555435049517056-10400-56678-29073-42960,
                    -7641898562-88104-9625510159-90724540111205245871-90933-69420670393720278051,
                    -52197-40278-5842565414-23394-14156912-53447735217307-78147637279890555412,
                    -57658-32884-448782275539730363835111397777419338736-11829-61188-9275755946,
                    -71232-63032-8394739147-96684-9923325131-3219724406-55428-6194125874-69453,
                    64483-19644-684411278387338-4867666451-447-6159050932-112702903565698-63544,
                    1002980499-94618636891365-81810-71914-52056-1378244240-30093-24372400767581,
                    -17365-69164-8420-69289-293704801090439131416924350668393286173178266-81313,
                    17921-38196552619948-2497075712-721062869674613162161047514765651211839,
                    -96916-8273928924-9992758449372806935711219-32119-62050-48745-83486-52376,
                    4266882659688823877346269-960059763025009-2951-678119980181587-79793-18547,
                    -830866951233127-92145-884974770359527190988785-8888269188-46131-5589-15086,
                    36255-53238-33009826645390135939-42946-2557133298692915319974746-40127-39050,
                    9103351717-98048872403617265453-94425-63694-3002759004886603649-20267-52565,
                    -6732134037432091515-56753601152713468617-61395-26503-98929-8849-6331810709,
                    -1615161905-95785526223670-2527790206-193914573537208-31992-9245018516-90452,
                    -58870-5860293383143331799482411-54126-3257635440-60526-78764-25069-9022-394,
                    92186-3805755328-61569677807716919546-92664-9494844484-134398352927518-48333,
                    7299838342-90553-98578-7690681515-16464784399252935225-39968-10130-7845-32245,
                    -74955-7499667731-13897-82493334079361959560-24404-5755319486-4534134098,
                    -24978-33612790587184776713-954226421-96075-59130-28976-16922-622036997068331,
                    218744055189650519085818166480-6817734323-3046-49656-5975843564-10960-30796,
                    15473-2021646085-8535541515-30669-87498577115606763199-838056204291213-14606,
                    4394-562749131040696810-615953256431640-97324205898052-7908-723301558-80301,
                    34878329003939-8824883162093721566-3218-66080-31620868595428990476-42889,
                    -15016-188387545630159-6710142328-9270385850-547523470-80806682061776488235,
                    46421-4157874005-811428054520868-1560640178378468863-97516-13016-7222379630,
                    -55692822558846728007-34686-69049-4167788535-821768060-51280289714908849235,
                    26905-81117-448884062374337-246629747679542-72082-3509398175-61761-6816959697,
                    -62542-7296559883-64026-37656-92392-12113-734959825868379-2154564607-70957,
                    -92254-97460-63436-8853-19357-51965-7658212687-4971245413-600433349631539,
                    -573474183767280-6881352088-13155-86430-15239-450309604118749-239924604835243,
                    -7945085425-5852488781-3945453073-48864-822893908682540-1155525014-5431-39585,
                    -89526270531953-8161136985-5602268684-271011142264655-26965-63081-13840-91003,
                    -78147-896641488198899021-61575-4706065260-23844-21781-91865-19607448082890,
                    63692-88663-5827215970-65195-45416-48444-78226-65332-2456842833-1806-71595,
                    80002-522503095248452-9010631015-220736233963318783912869977900-4026-76870,
                    -45943336659174-84360-22684-16832-67949-38077-38987-3284751443-53580-13505,
                    9344-923372658570458-52764-67471-68411-1119-2072-934766798140887-89304-12235,
                    4148814545355-34855-7208024514-58305334034331873177451-64983-5787682874,
                    62481-32754-3990222451-79095-2390478409-741877916 };

    //        int nums[] = { -1, 0, 1, 2, -1, -4 };
            List<List<Integer>> res = threeSum(nums);
            System.out.println(res);

        }
    }
  • 相关阅读:
    LoadRunner字符编码转换
    登山记02-百丈岭古道(昌北古道)_20201213
    JVM GC原理和监控
    登山记01_径山古道_20201107
    awk命令
    shell计算文件中某一列的平均值
    linux命令后台运行
    二维数组_基础(九)
    一维数组(八)
    选择语句switch总结(七)
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/12176337.html
Copyright © 2011-2022 走看看