Showing preview only (235K chars total). Download the full file or copy to clipboard to get everything.
Repository: mayuanucas/mygo
Branch: master
Commit: 4ce689b486da
Files: 348
Total size: 155.9 KB
Directory structure:
gitextract_5aw899i7/
├── .gitignore
├── LICENSE
├── README.md
├── algorithm/
│ ├── leetcode/
│ │ ├── 001/
│ │ │ ├── ac001.go
│ │ │ └── ac001_test.go
│ │ ├── 007/
│ │ │ ├── ac007.go
│ │ │ └── ac007_test.go
│ │ ├── 009/
│ │ │ ├── ac009.go
│ │ │ └── ac009_test.go
│ │ ├── 013/
│ │ │ ├── ac013.go
│ │ │ └── ac013_test.go
│ │ ├── 014/
│ │ │ ├── ac014.go
│ │ │ └── ac014_test.go
│ │ ├── 020/
│ │ │ ├── ac020.go
│ │ │ └── ac020_test.go
│ │ ├── 021/
│ │ │ └── ac021.go
│ │ ├── 026/
│ │ │ └── ac026.go
│ │ ├── 027/
│ │ │ └── ac027.go
│ │ ├── 028/
│ │ │ └── ac028.go
│ │ ├── 035/
│ │ │ ├── ac035.go
│ │ │ └── ac035_test.go
│ │ ├── 038/
│ │ │ └── ac038.go
│ │ ├── 053/
│ │ │ └── ac053.go
│ │ ├── 058/
│ │ │ ├── ac058.go
│ │ │ └── ac058_test.go
│ │ ├── 066/
│ │ │ └── ac066.go
│ │ ├── 067/
│ │ │ ├── ac067.go
│ │ │ └── ac067_test.go
│ │ ├── 069/
│ │ │ ├── ac069.go
│ │ │ └── ac069_test.go
│ │ ├── 070/
│ │ │ └── ac070.go
│ │ ├── 083/
│ │ │ └── ac083.go
│ │ ├── 088/
│ │ │ └── ac088.go
│ │ ├── 100/
│ │ │ └── ac100.go
│ │ ├── 101/
│ │ │ └── ac101.go
│ │ ├── 104/
│ │ │ └── ac104.go
│ │ ├── 107/
│ │ │ ├── ac107.go
│ │ │ └── ac107_test.go
│ │ ├── 108/
│ │ │ └── ac108.go
│ │ ├── 110/
│ │ │ └── ac110.go
│ │ ├── 111/
│ │ │ └── ac111.go
│ │ ├── 112/
│ │ │ └── ac112.go
│ │ ├── 118/
│ │ │ └── ac118.go
│ │ ├── 119/
│ │ │ └── ac119.go
│ │ ├── 120/
│ │ │ ├── ac120.go
│ │ │ └── ac120_test.go
│ │ ├── 121/
│ │ │ ├── ac121.go
│ │ │ └── ac121_test.go
│ │ ├── 122/
│ │ │ └── ac122.go
│ │ ├── 125/
│ │ │ ├── ac125.go
│ │ │ └── ac125_test.go
│ │ ├── 136/
│ │ │ └── ac136.go
│ │ ├── 137/
│ │ │ ├── ac137.go
│ │ │ └── ac137_test.go
│ │ ├── 160/
│ │ │ ├── ac160.go
│ │ │ └── ac160_test.go
│ │ ├── 167/
│ │ │ └── ac167.go
│ │ ├── 168/
│ │ │ ├── ac141.go
│ │ │ └── ac141_test.go
│ │ ├── 169/
│ │ │ └── ac169.go
│ │ ├── 171/
│ │ │ └── ac171.go
│ │ ├── 172/
│ │ │ └── ac172.go
│ │ ├── 189/
│ │ │ ├── ac189.go
│ │ │ └── ac189_test.go
│ │ ├── 191/
│ │ │ ├── ac191.go
│ │ │ └── ac191_test.go
│ │ ├── 198/
│ │ │ └── ac198.go
│ │ ├── 202/
│ │ │ └── ac202.go
│ │ ├── 203/
│ │ │ └── ac203.go
│ │ ├── 204/
│ │ │ └── ac204.go
│ │ ├── 205/
│ │ │ ├── ac205.go
│ │ │ └── ac205_test.go
│ │ ├── 206/
│ │ │ └── ac206.go
│ │ ├── 217/
│ │ │ └── ac217.go
│ │ ├── 219/
│ │ │ └── ac219.go
│ │ ├── 226/
│ │ │ └── ac226.go
│ │ ├── 231/
│ │ │ └── ac231.go
│ │ ├── 234/
│ │ │ └── ac234.go
│ │ ├── 258/
│ │ │ └── ac258.go
│ │ ├── 263/
│ │ │ └── ac263.go
│ │ ├── 268/
│ │ │ ├── ac268.go
│ │ │ └── ac268_test.go
│ │ ├── 283/
│ │ │ └── ac283.go
│ │ ├── 290/
│ │ │ ├── ac290.go
│ │ │ └── ac290_test.go
│ │ ├── 292/
│ │ │ └── ac292.go
│ │ ├── 303/
│ │ │ ├── ac303.go
│ │ │ └── ac303_test.go
│ │ ├── 326/
│ │ │ ├── ac326.go
│ │ │ └── ac326_test.go
│ │ ├── 342/
│ │ │ └── ac342.go
│ │ ├── 344/
│ │ │ ├── ac344.go
│ │ │ └── ac344_test.go
│ │ ├── 345/
│ │ │ ├── ac345.go
│ │ │ └── ac345_test.go
│ │ ├── 349/
│ │ │ ├── ac349.go
│ │ │ └── ac349_test.go
│ │ ├── 350/
│ │ │ ├── ac350.go
│ │ │ └── ac350_test.go
│ │ ├── 367/
│ │ │ └── ac367.go
│ │ ├── 371/
│ │ │ └── ac371.go
│ │ ├── 383/
│ │ │ ├── ac383.go
│ │ │ └── ac383_test.go
│ │ ├── 387/
│ │ │ └── ac387.go
│ │ ├── 389/
│ │ │ ├── ac389.go
│ │ │ └── ac389_test.go
│ │ ├── 400/
│ │ │ ├── ac400.go
│ │ │ └── ac400_test.go
│ │ ├── 404/
│ │ │ └── ac404.go
│ │ ├── 405/
│ │ │ ├── ac405.go
│ │ │ └── ac405_test.go
│ │ ├── 409/
│ │ │ ├── ac409.go
│ │ │ └── ac409_test.go
│ │ ├── 412/
│ │ │ ├── ac412.go
│ │ │ └── ac412_test.go
│ │ ├── 414/
│ │ │ ├── ac414.go
│ │ │ └── ac414_test.go
│ │ ├── 415/
│ │ │ ├── ac415.go
│ │ │ └── ac415_test.go
│ │ ├── 434/
│ │ │ └── ac434.go
│ │ ├── 437/
│ │ │ └── ac437.go
│ │ ├── 438/
│ │ │ ├── ac438.go
│ │ │ └── ac438_test.go
│ │ ├── 441/
│ │ │ ├── ac441.go
│ │ │ └── ac441_test.go
│ │ ├── 443/
│ │ │ ├── ac443.go
│ │ │ └── ac443_test.go
│ │ ├── 447/
│ │ │ └── ac447.go
│ │ ├── 448/
│ │ │ └── ac448.go
│ │ ├── 453/
│ │ │ ├── ac453.go
│ │ │ └── ac453_test.go
│ │ ├── 455/
│ │ │ ├── ac455.go
│ │ │ └── ac455_test.go
│ │ ├── 458/
│ │ │ └── ac458.go
│ │ ├── 459/
│ │ │ ├── ac459.go
│ │ │ └── ac459_test.go
│ │ ├── 461/
│ │ │ └── ac461.go
│ │ ├── 463/
│ │ │ ├── ac463.go
│ │ │ └── ac463_test.go
│ │ ├── 475/
│ │ │ ├── ac475.go
│ │ │ └── ac475_test.go
│ │ ├── 476/
│ │ │ ├── ac476.go
│ │ │ └── ac476_test.go
│ │ ├── 479/
│ │ │ ├── ac479.go
│ │ │ └── ac479_test.go
│ │ ├── 485/
│ │ │ ├── ac485.go
│ │ │ └── ac485_test.go
│ │ ├── 492/
│ │ │ ├── ac492.go
│ │ │ └── ac492_test.go
│ │ ├── 496/
│ │ │ ├── ac496.go
│ │ │ └── ac496_test.go
│ │ ├── 500/
│ │ │ ├── ac500.go
│ │ │ └── ac500_test.go
│ │ ├── 501/
│ │ │ ├── ac501.go
│ │ │ └── ac501_test.go
│ │ ├── 504/
│ │ │ ├── ac504.go
│ │ │ └── ac504_test.go
│ │ ├── 506/
│ │ │ ├── ac506.go
│ │ │ └── ac506_test.go
│ │ ├── 507/
│ │ │ ├── ac507.go
│ │ │ └── ac507_test.go
│ │ ├── 520/
│ │ │ ├── ac520.go
│ │ │ └── ac520_test.go
│ │ ├── 521/
│ │ │ └── ac521.go
│ │ ├── 530/
│ │ │ ├── ac530.go
│ │ │ └── ac530_test.go
│ │ ├── 532/
│ │ │ ├── ac532.go
│ │ │ └── ac532_test.go
│ │ ├── 538/
│ │ │ └── ac538.go
│ │ ├── 541/
│ │ │ ├── ac541.go
│ │ │ └── ac541_test.go
│ │ ├── 543/
│ │ │ └── ac543.go
│ │ ├── 551/
│ │ │ ├── ac551.go
│ │ │ └── ac551_test.go
│ │ ├── 557/
│ │ │ ├── ac557.go
│ │ │ └── ac557_test.go
│ │ ├── 561/
│ │ │ ├── ac561.go
│ │ │ └── ac561_test.go
│ │ ├── 563/
│ │ │ └── ac563.go
│ │ ├── 566/
│ │ │ ├── ac566.go
│ │ │ └── ac566_test.go
│ │ ├── 572/
│ │ │ └── ac572.go
│ │ ├── 575/
│ │ │ ├── ac575.go
│ │ │ └── ac575_test.go
│ │ ├── 581/
│ │ │ ├── ac581.go
│ │ │ └── ac581_test.go
│ │ ├── 594/
│ │ │ ├── ac594.go
│ │ │ └── ac594_test.go
│ │ ├── 598/
│ │ │ ├── ac598.go
│ │ │ └── ac598_test.go
│ │ ├── 599/
│ │ │ ├── ac599.go
│ │ │ └── ac599_test.go
│ │ ├── 605/
│ │ │ ├── ac605.go
│ │ │ └── ac605_test.go
│ │ ├── 606/
│ │ │ └── ac606.go
│ │ ├── 617/
│ │ │ └── ac617.go
│ │ ├── 628/
│ │ │ └── ac628.go
│ │ ├── 633/
│ │ │ └── ac633.go
│ │ ├── 637/
│ │ │ └── ac637.go
│ │ ├── 643/
│ │ │ ├── ac643.go
│ │ │ └── ac643_test.go
│ │ ├── 645/
│ │ │ ├── ac645.go
│ │ │ └── ac645_test.go
│ │ ├── 653/
│ │ │ ├── ac653.go
│ │ │ └── ac653_test.go
│ │ ├── 657/
│ │ │ ├── ac657.go
│ │ │ └── ac657_test.go
│ │ ├── 661/
│ │ │ ├── ac661.go
│ │ │ └── ac661_test.go
│ │ ├── 665/
│ │ │ ├── ac665.go
│ │ │ └── ac665_test.go
│ │ ├── 669/
│ │ │ └── ac669.go
│ │ ├── 671/
│ │ │ ├── ac671.go
│ │ │ └── ac671_test.go
│ │ ├── 674/
│ │ │ ├── ac674.go
│ │ │ └── ac674_test.go
│ │ ├── 680/
│ │ │ └── ac680.go
│ │ ├── 682/
│ │ │ ├── ac682.go
│ │ │ └── ac682_test.go
│ │ ├── 686/
│ │ │ ├── ac686.go
│ │ │ └── ac686_test.go
│ │ ├── 687/
│ │ │ └── ac687.go
│ │ ├── 693/
│ │ │ ├── ac693.go
│ │ │ └── ac693_test.go
│ │ ├── 695/
│ │ │ └── ac695.go
│ │ ├── 696/
│ │ │ └── ac696.go
│ │ ├── 697/
│ │ │ └── ac697.go
│ │ ├── 717/
│ │ │ └── ac717.go
│ │ ├── 724/
│ │ │ └── ac724.go
│ │ ├── 728/
│ │ │ └── ac728.go
│ │ ├── 744/
│ │ │ └── ac744.go
│ │ ├── 746/
│ │ │ └── ac746.go
│ │ ├── 747/
│ │ │ ├── ac747.go
│ │ │ └── ac747_test.go
│ │ ├── 762/
│ │ │ └── ac762.go
│ │ ├── 766/
│ │ │ ├── ac766.go
│ │ │ └── ac766_test.go
│ │ ├── 771/
│ │ │ ├── ac771.go
│ │ │ └── ac771_test.go
│ │ ├── 783/
│ │ │ ├── ac783.go
│ │ │ └── ac783_test.go
│ │ ├── 784/
│ │ │ ├── ac784.go
│ │ │ └── ac784_test.go
│ │ ├── 788/
│ │ │ └── ac788.go
│ │ ├── 796/
│ │ │ └── ac796.go
│ │ ├── 804/
│ │ │ ├── ac804.go
│ │ │ └── ac804_test.go
│ │ ├── 811/
│ │ │ ├── ac811.go
│ │ │ └── ac811_test.go
│ │ ├── 812/
│ │ │ ├── ac812.go
│ │ │ └── ac812_test.go
│ │ ├── 819/
│ │ │ ├── ac819.go
│ │ │ └── ac819_test.go
│ │ ├── 821/
│ │ │ ├── ac821.go
│ │ │ └── ac821_test.go
│ │ ├── 824/
│ │ │ ├── ac824.go
│ │ │ └── ac824_test.go
│ │ ├── 829/
│ │ │ ├── ac829.go
│ │ │ └── ac829_test.go
│ │ ├── 830/
│ │ │ ├── ac830.go
│ │ │ └── ac830_test.go
│ │ ├── 832/
│ │ │ └── ac832.go
│ │ ├── 836/
│ │ │ └── ac836.go
│ │ ├── 840/
│ │ │ └── ac840.go
│ │ └── 844/
│ │ └── ac844.go
│ └── sfo/
│ ├── 30/
│ │ ├── ac30.go
│ │ └── ac30_test.go
│ ├── 31/
│ │ ├── ac31.go
│ │ └── ac31_test.go
│ ├── 32/
│ │ ├── ac32.go
│ │ └── ac32_test.go
│ ├── 33/
│ │ ├── ac33.go
│ │ └── ac33_test.go
│ ├── 34/
│ │ ├── ac34.go
│ │ └── ac34_test.go
│ ├── 35/
│ │ └── ac35.go
│ ├── 36/
│ │ ├── ac36.go
│ │ └── ac36_test.go
│ ├── 38/
│ │ ├── ac38.go
│ │ └── ac38_test.go
│ ├── 39/
│ │ ├── ac39.go
│ │ └── ac39_test.go
│ ├── 40/
│ │ ├── ac40.go
│ │ └── ac40_test.go
│ ├── 42/
│ │ ├── ac42.go
│ │ └── ac42_test.go
│ ├── 43/
│ │ ├── ac43.go
│ │ └── ac43_test.go
│ ├── 44/
│ │ ├── ac44.go
│ │ └── ac44_test.go
│ ├── 45/
│ │ ├── ac45.go
│ │ └── ac45_test.go
│ ├── 46/
│ │ ├── ac46.go
│ │ └── ac46_test.go
│ ├── 47/
│ │ ├── ac47.go
│ │ └── ac47_test.go
│ ├── 48/
│ │ ├── ac48.go
│ │ └── ac48_test.go
│ ├── 49/
│ │ ├── ac49.go
│ │ └── ac49_test.go
│ ├── 50/
│ │ ├── ac50.go
│ │ └── ac50_test.go
│ ├── 51/
│ │ ├── ac51.go
│ │ └── ac51_test.go
│ ├── 52/
│ │ ├── ac52.go
│ │ └── ac52_test.go
│ ├── 53/
│ │ ├── ac53.go
│ │ └── ac53_test.go
│ ├── 54/
│ │ └── ac54.go
│ ├── 55/
│ │ ├── ac55.go
│ │ └── ac55_test.go
│ ├── 56/
│ │ ├── ac56.go
│ │ └── ac56_test.go
│ ├── 57/
│ │ ├── ac57.go
│ │ └── ac57_test.go
│ ├── 58/
│ │ ├── ac58.go
│ │ └── ac58_test.go
│ ├── 59/
│ │ ├── ac59.go
│ │ └── ac59_test.go
│ ├── 61/
│ │ ├── ac60.go
│ │ └── ac60_test.go
│ ├── 62/
│ │ ├── ac62.go
│ │ └── ac62_test.go
│ ├── 63/
│ │ ├── ac63.go
│ │ └── ac63_test.go
│ └── 65/
│ ├── ac64.go
│ └── ac64_test.go
├── lib/
│ ├── BinaryTreeNode.go
│ ├── GetLine.go
│ ├── Reverse.go
│ └── grpool/
│ └── Pool.go
└── notes/
├── defer/
│ └── main.go
├── learn/
│ └── main.go
├── main/
│ └── main.go
├── nil/
│ └── main.go
├── switch/
│ └── main.go
├── time/
│ └── main.go
├── 函数/
│ └── main.go
├── 判断/
│ └── main.go
├── 协程/
│ └── main.go
├── 变量/
│ └── main.go
├── 命令行参数/
│ └── iie.go
├── 导入导出/
│ └── main.go
├── 常量/
│ └── main.go
├── 循环/
│ └── main.go
├── 数组/
│ └── main.go
├── 输入/
│ └── main.go
└── 输出/
└── main.go
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
.idea/
.DS_Store
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
================================================
FILE: LICENSE
================================================
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: README.md
================================================
## mygo
> 此项目建议搭配[Code tree for GitHub](https://github.com/buunguyen/octotree)效果更佳。
>
> ##### 环境
>
> go1.10 darwin/amd64
### Leetcode
使用 Go 解答 Leetcode 题目。[链接](/algorithm/leetcode)
| 题号 | 题目 | 难度 |
|:---:|:---- |:---:|
| 1 | [两数之和](algorithm/leetcode/001) | 简单 |
| 7 | [反转整数](algorithm/leetcode/007) | 简单 |
| 9 | [回文数](algorithm/leetcode/009) | 简单 |
| 13 | [罗马数字转整数](algorithm/leetcode/013) | 简单 |
| 14 | [最长公共前缀](algorithm/leetcode/014) | 简单 |
| 20 | [有效的括号](algorithm/leetcode/020) | 简单 |
| 21 | [合并两个有序链表](algorithm/leetcode/021) | 简单 |
| 26 | [删除排序数组中的重复项](algorithm/leetcode/026) | 简单 |
| 27 | [移除元素](algorithm/leetcode/027) | 简单 |
| 28 | [实现strStr()](algorithm/leetcode/028) | 简单 |
| 35 | [搜索插入位置](algorithm/leetcode/035) | 简单 |
| 38 | [报数](algorithm/leetcode/038) | 简单 |
| 53 | [最大子序和](algorithm/leetcode/053) | 简单 |
| 58 | [最后一个单词的长度](algorithm/leetcode/058) | 简单 |
| 66 | [加一](algorithm/leetcode/066) | 简单 |
| 67 | [二进制求和](algorithm/leetcode/067) | 简单 |
| 69 | [x 的平方根](algorithm/leetcode/069) | 简单 |
| 70 | [爬楼梯](algorithm/leetcode/070) | 简单 |
| 83 | [删除排序链表中的重复元素](algorithm/leetcode/083) | 简单 |
| 88 | [合并两个有序数组](algorithm/leetcode/088) | 简单 |
| 100 | [相同的树](algorithm/leetcode/100) | 简单 |
| 101 | [对称二叉树](algorithm/leetcode/101) | 简单 |
| 104 | [二叉树的最大深度](algorithm/leetcode/104) | 简单 |
| 107 | [二叉树的层次遍历 II](algorithm/leetcode/107) | 简单 |
| 108 | [将有序数组转换为二叉搜索树 ](algorithm/leetcode/108) | 简单 |
| 110 | [平衡二叉树](algorithm/leetcode/110) | 简单 |
| 111 | [二叉树的最小深度](algorithm/leetcode/111) | 简单 |
| 112 | [路径总和](algorithm/leetcode/112) | 简单 |
| 118 | [杨辉三角](algorithm/leetcode/118) | 简单 |
| 119 | [杨辉三角 II](algorithm/leetcode/119) | 简单 |
| 121 | [买卖股票的最佳时机](algorithm/leetcode/121) | 简单 |
| 122 | [买卖股票的最佳时机 II](algorithm/leetcode/122) | 简单 |
| 125 | [验证回文串](algorithm/leetcode/125) | 简单 |
| 136 | [只出现一次的数字](algorithm/leetcode/136) | 简单 |
| 167 | [两数之和 II - 输入有序数组](algorithm/leetcode/167) | 简单 |
| 168 | [Excel表列名称](algorithm/leetcode/168) | 简单 |
| 169 | [求众数](algorithm/leetcode/169) | 简单 |
| 171 | [Excel表列序号](algorithm/leetcode/171) | 简单 |
| 172 | [阶乘后的零](algorithm/leetcode/172) | 简单 |
| 189 | [旋转数组](algorithm/leetcode/189) | 简单 |
| 198 | [打家劫舍](algorithm/leetcode/198) | 简单 |
| 202 | [快乐数](algorithm/leetcode/202) | 简单 |
| 203 | [删除链表中的节点](algorithm/leetcode/203) | 简单 |
| 204 | [计数质数](algorithm/leetcode/204) | 简单 |
| 205 | [同构字符串](algorithm/leetcode/205) | 简单 |
| 206 | [反转链表](algorithm/leetcode/206) | 简单 |
| 217 | [存在重复元素](algorithm/leetcode/217) | 简单 |
| 219 | [存在重复元素 II](algorithm/leetcode/219) | 简单 |
| 226 | [翻转二叉树](algorithm/leetcode/226) | 简单 |
| 231 | [2的幂](algorithm/leetcode/231) | 简单 |
| 234 | [回文链表](algorithm/leetcode/234) | 简单 |
| 258 | [各位相加](algorithm/leetcode/258) | 简单 |
| 263 | [丑数](algorithm/leetcode/263) | 简单 |
| 268 | [缺失数字](algorithm/leetcode/268) | 简单 |
| 283 | [移动零](algorithm/leetcode/283) | 简单 |
| 290 | [单词模式](algorithm/leetcode/290) | 简单 |
| 292 | [Nim游戏](algorithm/leetcode/292) | 简单 |
| 303 | [区域和检索 - 不可变](algorithm/leetcode/303) | 简单 |
| 326 | [3的幂](algorithm/leetcode/326) | 简单 |
| 342 | [4的幂](algorithm/leetcode/342) | 简单 |
| 344 | [反转字符串](algorithm/leetcode/344) | 简单 |
| 345 | [反转字符串中的元音字母](algorithm/leetcode/345) | 简单 |
| 349 | [两个数组的交集](algorithm/leetcode/349) | 简单 |
| 350 | [两个数组的交集 II](algorithm/leetcode/350) | 简单 |
| 367 | [有效的完全平方数](algorithm/leetcode/367) | 简单 |
| 371 | [两整数之和](algorithm/leetcode/371) | 简单 |
| 383 | [赎金信](algorithm/leetcode/383) | 简单 |
| 387 | [字符串中的第一个唯一字符](algorithm/leetcode/387) | 简单 |
| 389 | [找不同](algorithm/leetcode/389) | 简单 |
| 400 | [第N个数字](algorithm/leetcode/400) | 简单 |
| 404 | [左叶子之和](algorithm/leetcode/404) | 简单 |
| 405 | [数字转换为十六进制数](algorithm/leetcode/405) | 简单 |
| 409 | [最长回文串](algorithm/leetcode/409) | 简单 |
| 412 | [Fizz Buzz](algorithm/leetcode/412) | 简单 |
| 414 | [第三大的数](algorithm/leetcode/414) | 简单 |
| 415 | [字符串相加](algorithm/leetcode/415) | 简单 |
| 434 | [字符串中的单词数](algorithm/leetcode/434) | 简单 |
| 437 | [路径总和 III](algorithm/leetcode/437) | 简单 |
| 438 | [找到字符串中所有字母异位词](algorithm/leetcode/438) | 简单 |
| 441 | [排列硬币](algorithm/leetcode/441) | 简单 |
| 443 | [压缩字符串](algorithm/leetcode/443) | 简单 |
| 447 | [回旋镖的数量](algorithm/leetcode/447) | 简单 |
| 448 | [找到所有数组中消失的数字](algorithm/leetcode/448) | 简单 |
| 453 | [最小移动次数使数组元素相等](algorithm/leetcode/453) | 简单 |
| 455 | [分发饼干](algorithm/leetcode/455) | 简单 |
| 459 | [重复的子字符串](algorithm/leetcode/459) | 简单 |
| 461 | [汉明距离](algorithm/leetcode/461) | 简单 |
| 463 | [岛屿的周长](algorithm/leetcode/463) | 简单 |
| 475 | [供暖器](algorithm/leetcode/475) | 简单 |
| 476 | [数字的补数](algorithm/leetcode/476) | 简单 |
| 479 | [最大回文数乘积](algorithm/leetcode/479) | 简单 |
| 485 | [最大连续1的个数](algorithm/leetcode/485) | 简单 |
| 492 | [构造矩形](algorithm/leetcode/492) | 简单 |
| 496 | [下一个更大元素 I](algorithm/leetcode/496) | 简单 |
| 500 | [键盘行](algorithm/leetcode/500) | 简单 |
| 501 | [Find Mode in Binary Search Tree](algorithm/leetcode/501) | 简单 |
| 504 | [七进制数](algorithm/leetcode/504) | 简单 |
| 506 | [相对名次](algorithm/leetcode/506) | 简单 |
| 507 | [完美数](algorithm/leetcode/507) | 简单 |
| 520 | [检测大写字母](algorithm/leetcode/520) | 简单 |
| 521 | [最长特殊序列 Ⅰ](algorithm/leetcode/521) | 简单 |
| 530 | [二叉搜索树的最小绝对差](algorithm/leetcode/530) | 简单 |
| 532 | [数组中的K-diff数对](algorithm/leetcode/532) | 简单 |
| 538 | [把二叉搜索树转换为累加树](algorithm/leetcode/538) | 简单 |
| 541 | [反转字符串 II](algorithm/leetcode/541) | 简单 |
| 543 | [二叉树的直径](algorithm/leetcode/543) | 简单 |
| 551 | [学生出勤纪录 I](algorithm/leetcode/551) | 简单 |
| 557 | [反转字符串中的单词 III](algorithm/leetcode/557) | 简单 |
| 561 | [数组拆分 I](algorithm/leetcode/561) | 简单 |
| 563 | [二叉树的坡度](algorithm/leetcode/563) | 简单 |
| 566 | [重塑矩阵](algorithm/leetcode/566) | 简单 |
| 572 | [另一个树的子树](algorithm/leetcode/572) | 简单 |
| 575 | [分糖果](algorithm/leetcode/575) | 简单 |
| 581 | [最短无序连续子数组](algorithm/leetcode/581) | 简单 |
| 594 | [最长和谐子序列](algorithm/leetcode/594) | 简单 |
| 598 | [范围求和 II](algorithm/leetcode/598) | 简单 |
| 599 | [两个列表的最小索引总和](algorithm/leetcode/599) | 简单 |
| 605 | [种花问题](algorithm/leetcode/605) | 简单 |
| 606 | [根据二叉树创建字符串](algorithm/leetcode/606) | 简单 |
| 617 | [合并二叉树](algorithm/leetcode/617) | 简单 |
| 628 | [三个数的最大乘积](algorithm/leetcode/628) | 简单 |
| 633 | [平方数之和](algorithm/leetcode/633) | 简单 |
| 637 | [二叉树的层平均值](algorithm/leetcode/637) | 简单 |
| 643 | [子数组最大平均数 I](algorithm/leetcode/643) | 简单 |
| 645 | [错误的集合](algorithm/leetcode/645) | 简单 |
| 653 | [两数之和 IV - 输入 BST](algorithm/leetcode/653) | 简单 |
| 657 | [判断路线成圈](algorithm/leetcode/657) | 简单 |
| 661 | [图片平滑器](algorithm/leetcode/661) | 简单 |
| 669 | [修剪二叉搜索树](algorithm/leetcode/669) | 简单 |
| 674 | [最长连续递增序列](algorithm/leetcode/674) | 简单 |
| 682 | [棒球比赛](algorithm/leetcode/682) | 简单 |
| 686 | [重复叠加字符串匹配](algorithm/leetcode/686) | 简单 |
| 687 | [最长同值路径](algorithm/leetcode/687) | 简单 |
| 693 | [交替位二进制数](algorithm/leetcode/693) | 简单 |
| 695 | [岛屿的最大面积](algorithm/leetcode/695) | 简单 |
| 696 | [计数二进制子串](algorithm/leetcode/696) | 简单 |
| 697 | [数组的度](algorithm/leetcode/697) | 简单 |
| 717 | [1比特与2比特字符](algorithm/leetcode/717) | 简单 |
| 724 | [寻找数组的中心索引](algorithm/leetcode/724) | 简单 |
| 728 | [自除数](algorithm/leetcode/728) | 简单 |
| 744 | [寻找比目标字母大的最小字母](algorithm/leetcode/744) | 简单 |
| 746 | [使用最小花费爬楼梯](algorithm/leetcode/746) | 简单 |
| 747 | [至少是其他数字两倍的最大数](algorithm/leetcode/747) | 简单 |
| 762 | [二进制表示中质数个计算置位](algorithm/leetcode/762) | 简单 |
| 766 | [托普利茨矩阵](algorithm/leetcode/766) | 简单 |
| 771 | [宝石与石头](algorithm/leetcode/771) | 简单 |
| 783 | [二叉搜索树结点最小距离](algorithm/leetcode/783) | 简单 |
| 784 | [字母大小写全排列](algorithm/leetcode/784) | 简单 |
| 788 | [旋转数字](algorithm/leetcode/788) | 简单 |
| 796 | [旋转字符串](algorithm/leetcode/796) | 简单 |
| 804 | [唯一摩尔斯密码词](algorithm/leetcode/804) | 简单 |
| 811 | [子域名访问计数](algorithm/leetcode/811) | 简单 |
| 812 | [最大三角形面积](algorithm/leetcode/812) | 简单 |
| 821 | [字符的最短距离](algorithm/leetcode/821) | 简单 |
| 824 | [山羊拉丁文](algorithm/leetcode/824) | 简单 |
| 829 | [连续整数求和](algorithm/leetcode/829) | 中等 |
| 830 | [较大分组的位置](algorithm/leetcode/830) | 简单 |
| 832 | [翻转图像](algorithm/leetcode/832) | 简单 |
| 836 | [矩形重叠](algorithm/leetcode/836) | 简单 |
| 840 | [矩阵中的幻方](algorithm/leetcode/840) | 简单 |
| 844 | [比较含退格的字符串](algorithm/leetcode/844) | 简单 |
### 剑指offer(第二版)
使用 Go 解答 剑指offer 题目。[链接](/algorithm/sfo)
### Go 语言笔记
学习 Go 语言过程中做的笔记。[链接](/notes)
================================================
FILE: algorithm/leetcode/001/ac001.go
================================================
package problem001
func twoSum(nums []int, target int) []int {
// m 负责保存map[整数]整数的序列号
m := make(map[int]int, len(nums))
// 通过 for 循环,获取b的序列号
for i, b := range nums {
// 通过查询map,获取a = target - b的序列号
if j, ok := m[target-b]; ok {
// ok 为 true
// 说明在i之前,存在nums[j] == a
return []int{j, i}
// 注意,顺序是j,i,因为j<i
}
// 把序号i和i对应的值,存入map
m[nums[i]] = i
}
return nil
}
================================================
FILE: algorithm/leetcode/001/ac001_test.go
================================================
package problem001
import (
"testing"
"github.com/stretchr/testify/assert"
)
type argument struct {
numberArray []int
target int
}
type answer struct {
numberArray []int
}
type example struct {
arg argument
ans answer
}
func TestOk(t *testing.T) {
examples := []example{
{
arg: argument{
numberArray: []int{3, 2, 4},
target: 6,
},
ans: answer{
numberArray: []int{1, 2},
},
}, {
arg: argument{
numberArray: []int{3, 2, 4},
target: 8,
},
ans: answer{
numberArray: nil,
},
},
}
ast := assert.New(t)
for _, exam := range examples {
arg, ans := exam.arg, exam.ans
ast.Equal(ans.numberArray, twoSum(arg.numberArray, arg.target),
"%v %v", arg.numberArray, arg.target)
}
}
================================================
FILE: algorithm/leetcode/007/ac007.go
================================================
package problem007
import "math"
func reverse(x int) int {
var result int64
for 0 != x {
result = result*10 + int64(x%10)
x /= 10
}
if result > math.MaxInt32 || result < math.MinInt32 {
return 0
}
return int(result)
}
================================================
FILE: algorithm/leetcode/007/ac007_test.go
================================================
package problem007
import (
"testing"
"github.com/stretchr/testify/assert"
)
type argument struct {
number int
}
type answer struct {
revertNumber int
}
type example struct {
arg argument
ans answer
}
func Test_reverse(t *testing.T) {
examples := []example{
{
arg: argument{
number: 1230,
},
ans: answer{
revertNumber: 321,
},
},
{
arg: argument{
number: -123,
},
ans: answer{
revertNumber: -321,
},
},
}
ast := assert.New(t)
for _, exam := range examples {
ans, arg := exam.ans, exam.arg
ast.Equal(ans.revertNumber, reverse(arg.number),
"%v %v", arg.number, ans.revertNumber)
}
}
================================================
FILE: algorithm/leetcode/009/ac009.go
================================================
package problem009
// 负数不是回文的
func isPalindrome(x int) bool {
if 0 > x || (0 == x%10 && 0 != x) {
return false
}
var revertedNumber int
for x > revertedNumber {
revertedNumber = revertedNumber*10 + x%10
x /= 10
}
return (x == revertedNumber) || (x == revertedNumber/10)
}
================================================
FILE: algorithm/leetcode/009/ac009_test.go
================================================
package problem009
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_isPalindrome(t *testing.T) {
type argument struct {
x int
}
type example struct {
arg argument
ans bool
}
examples := []example{
{
arg: argument{
x: 12321,
},
ans: true,
},
{
arg: argument{
x: 12344321,
},
ans: true,
},
{
arg: argument{
x: 98765,
},
ans: false,
},
}
ast := assert.New(t)
for _, exam := range examples {
ans, arg := exam.ans, exam.arg
ast.Equal(ans, isPalindrome(arg.x),
"%v %v", arg.x, ans)
}
}
================================================
FILE: algorithm/leetcode/013/ac013.go
================================================
package problem013
func romanToInt(s string) int {
roman := map[byte]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
if len(s) <= 0 {
return 0
}
// 当 map 中不存在该键时 ok 为 false
sum, ok := roman[s[len(s)-1]]
if !ok {
return 0
}
for i := len(s) - 2; i >= 0; i-- {
if roman[s[i]] < roman[s[i+1]] {
sum -= roman[s[i]]
} else {
sum += roman[s[i]]
}
}
return sum
}
================================================
FILE: algorithm/leetcode/013/ac013_test.go
================================================
package problem013
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_romanToInt(t *testing.T) {
type argument struct {
str string
}
type example struct {
arg argument
ans int
}
examples := []example{
{
arg: argument{
str: "VIII",
},
ans: 8,
},
{
arg: argument{
str: "IV",
},
ans: 4,
},
}
ast := assert.New(t)
for _, exam := range examples {
ans, arg := exam.ans, exam.arg
ast.Equal(ans, romanToInt(arg.str),
"%v %v", arg.str, ans)
}
}
================================================
FILE: algorithm/leetcode/014/ac014.go
================================================
package problem014
import (
"sort"
)
func longestCommonPrefix(strs []string) string {
if nil == strs || 0 >= len(strs) {
return ""
}
sort.Strings(strs)
first := strs[0]
last := strs[len(strs)-1]
var i, length int
if len(first) < len(last) {
length = len(first)
} else {
length = len(last)
}
for i < length && first[i] == last[i] {
i++
}
return first[0:i]
}
================================================
FILE: algorithm/leetcode/014/ac014_test.go
================================================
package problem014
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_longestCommonPrefix(t *testing.T) {
type argument struct {
strs []string
}
type example struct {
arg argument
ans string
}
examples := []example{
{
arg: argument{
strs: []string{"abcd", "abc", "ab"},
},
ans: "ab",
},
}
ast := assert.New(t)
for _, exam := range examples {
ans, arg := exam.ans, exam.arg
ast.Equal(ans, longestCommonPrefix(arg.strs),
"%v %v", arg.strs, ans)
}
}
================================================
FILE: algorithm/leetcode/020/ac020.go
================================================
package problem020
import "github.com/emirpasic/gods/stacks/arraystack"
func isValid(s string) bool {
stack := arraystack.New()
for _, c := range s {
if c == '(' {
stack.Push(')')
} else if c == '[' {
stack.Push(']')
} else if c == '{' {
stack.Push('}')
} else {
if stack.Empty() {
return false
}
tempC, _ := stack.Pop()
if tempC != c {
return false
}
}
}
return stack.Empty()
}
================================================
FILE: algorithm/leetcode/020/ac020_test.go
================================================
package problem020
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_isValid(t *testing.T) {
type argument struct {
s string
}
type example struct {
arg argument
ans bool
}
examples := []example{
{
arg: argument{
s: "{[()]}",
},
ans: true,
},
{
arg: argument{
s: "{{]]])",
},
ans: false,
},
}
ast := assert.New(t)
for _, exam := range examples {
ans, arg := exam.ans, exam.arg
ast.Equal(ans, isValid(arg.s),
"%v %v", arg.s, ans)
}
}
================================================
FILE: algorithm/leetcode/021/ac021.go
================================================
package problem021
type ListNode struct {
Val int
Next *ListNode
}
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
if nil == l1 {
return l2
}
if nil == l2 {
return l1
}
if l1.Val < l2.Val {
l1.Next = mergeTwoLists(l1.Next, l2)
return l1
} else {
l2.Next = mergeTwoLists(l1, l2.Next)
return l2
}
}
================================================
FILE: algorithm/leetcode/026/ac026.go
================================================
package problem026
func removeDuplicates(nums []int) int {
if nil == nums || len(nums) < 1 {
return 0
}
newLength := 1
for i := 1; i < len(nums); i++ {
if nums[i] != nums[newLength-1] {
nums[newLength] = nums[i]
newLength++
}
}
return newLength
}
================================================
FILE: algorithm/leetcode/027/ac027.go
================================================
package problem027
func removeElement(nums []int, val int) int {
if nil == nums || len(nums) < 1 {
return 0
}
var idx int
for i := 0; i < len(nums); i++ {
if nums[i] != val {
nums[idx] = nums[i]
idx++
}
}
return idx
}
================================================
FILE: algorithm/leetcode/028/ac028.go
================================================
package problem028
func strStr(haystack string, needle string) int {
if "" == needle {
return 0
}
for i := 0; i <= len(haystack)-len(needle); i++ {
for j := 0; j < len(needle) && haystack[i+j] == needle[j]; j++ {
if j == len(needle)-1 {
return i
}
}
}
return -1
}
================================================
FILE: algorithm/leetcode/035/ac035.go
================================================
package problem035
func searchInsert(nums []int, target int) int {
if nil == nums || len(nums) < 1 {
return 0
}
start, end := 0, len(nums)-1
for start <= end {
middle := (start + end) / 2
if nums[middle] == target {
return middle
} else if nums[middle] < target {
start = middle + 1
} else {
end = middle - 1
}
}
return start
}
================================================
FILE: algorithm/leetcode/035/ac035_test.go
================================================
package problem035
import (
"testing"
"fmt"
)
func Test_searchInsert(t *testing.T) {
data := []int{1, 3, 5, 6}
target := 2
fmt.Println(searchInsert(data, target))
}
================================================
FILE: algorithm/leetcode/038/ac038.go
================================================
package problem038
import "strconv"
func countAndSay(n int) string {
if 0 == n {
return ""
}
ans := "1"
for n--; n > 0; n-- {
current := ""
for i := 0; i < len(ans); i++ {
count := 1
for i+1 < len(ans) && ans[i] == ans[i+1] {
count++
i++
}
current += strconv.Itoa(count) + string(ans[i])
}
ans = current
}
return ans
}
================================================
FILE: algorithm/leetcode/053/ac053.go
================================================
package problem053
func maxSubArray(nums []int) int {
if nil == nums || len(nums) < 1 {
return 0
}
preSum, maxSum := nums[0], nums[0]
for i := 1; i < len(nums); i++ {
if preSum+nums[i] <= nums[i] {
preSum = nums[i]
} else {
preSum += nums[i]
}
if preSum > maxSum {
maxSum = preSum
}
}
return maxSum
}
================================================
FILE: algorithm/leetcode/058/ac058.go
================================================
package problem058
import (
"strings"
)
func lengthOfLastWord(s string) int {
subArray := strings.Split(s, " ")
for i := len(subArray) - 1; i >= 0; i-- {
length := len(subArray[i])
if length > 0 {
return length
}
}
return 0
}
func lengthOfLastWord2(s string) int {
len, tail := 0, len(s)-1
for tail >= 0 && s[tail] == ' ' {
tail--
}
for tail >= 0 && s[tail] != ' ' {
len++
tail--
}
return len
}
================================================
FILE: algorithm/leetcode/058/ac058_test.go
================================================
package problem058
import (
"testing"
"fmt"
)
func Test_lengthOfLastWord(t *testing.T) {
text1 := "Hello World"
text2 := "a "
fmt.Println(lengthOfLastWord(text1))
fmt.Println(lengthOfLastWord(text2))
fmt.Println(lengthOfLastWord2(text1))
fmt.Println(lengthOfLastWord2(text2))
text3 := "你好golang"
fmt.Println(len(text3))
fmt.Printf("%v %q %T\n", text3[0], text3[0], text3[0])
fmt.Printf("%v %q %T\n", text3[6], text3[6], text3[6])
text4 := '中'
text5 := 'g'
fmt.Printf("%v %T\n", text4, text4)
fmt.Printf("%q %T\n", text5, text5)
}
================================================
FILE: algorithm/leetcode/066/ac066.go
================================================
package problem066
func plusOne(digits []int) []int {
for i := len(digits) - 1; i >= 0; i-- {
if 9 != digits[i] {
digits[i]++
break
} else {
digits[i] = 0
}
}
if 0 == digits[0] {
newAns := make([]int, len(digits)+1)
newAns[0] = 1
return newAns
}
return digits
}
================================================
FILE: algorithm/leetcode/067/ac067.go
================================================
package problem067
func addBinary(a string, b string) string {
aChar := []rune(a)
bChar := []rune(b)
var s []rune
var c rune
i, j := len(a)-1, len(b)-1
for i >= 0 || j >= 0 || c == 1 {
if i >= 0 {
c += aChar[i] - '0'
i--
}
if j >= 0 {
c += bChar[j] - '0'
j--
}
s = append(s, c%2+'0')
c /= 2
}
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return string(s)
}
func addBinary2(a string, b string) string {
s := make([]byte, 0)
var c byte
i, j := len(a)-1, len(b)-1
for i >= 0 || j >= 0 || 1 == c {
if i >= 0 {
c += a[i] - '0'
i--
}
if j >= 0 {
c += b[j] - '0'
j--
}
s = append(s, c%2+'0')
c /= 2
}
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return string(s)
}
================================================
FILE: algorithm/leetcode/067/ac067_test.go
================================================
package problem067
import (
"testing"
"fmt"
)
func Test_addBinary(t *testing.T) {
a := "11"
b := "01"
fmt.Println(addBinary(a, b))
fmt.Println(addBinary2(a, b))
}
================================================
FILE: algorithm/leetcode/069/ac069.go
================================================
package problem069
func mySqrt(x int) int {
if 0 == x {
return 0
}
n := x
for n*n > x {
n = (n + x/n) / 2
}
return n
}
================================================
FILE: algorithm/leetcode/069/ac069_test.go
================================================
package problem069
import (
"testing"
"fmt"
)
func Test_mySqrt(t *testing.T) {
fmt.Println(mySqrt(9))
}
================================================
FILE: algorithm/leetcode/070/ac070.go
================================================
package problem070
func climbStairs(n int) int {
if 1 == n {
return 1
}
f := make([]int, n+1)
f[1], f[2] = 1, 2
for i := 3; i < len(f); i++ {
f[i] = f[i-1] + f[i-2]
}
return f[n]
}
func climbStairs2(n int) int {
a, b := 1, 1
for ; n > 0; n-- {
a, b = b, a+b
}
return a
}
================================================
FILE: algorithm/leetcode/083/ac083.go
================================================
package problem083
type ListNode struct {
Val int
Next *ListNode
}
func deleteDuplicates(head *ListNode) *ListNode {
if nil == head || nil == head.Next {
return head
}
prePtr := head
currentPtr := head.Next
lastValue := prePtr.Val
for nil != currentPtr {
if currentPtr.Val != lastValue {
lastValue = currentPtr.Val
prePtr = currentPtr
currentPtr = currentPtr.Next
} else {
prePtr.Next = currentPtr.Next
currentPtr = currentPtr.Next
}
}
return head
}
================================================
FILE: algorithm/leetcode/088/ac088.go
================================================
package problem088
func merge(nums1 []int, m int, nums2 []int, n int) {
idm, idn := m-1, n-1
for i := m + n - 1; i >= 0; i-- {
if idm < 0 {
nums1[i] = nums2[idn]
idn--
continue
}
if idn < 0 {
nums1[i] = nums1[idm]
idm--
continue
}
if nums1[idm] >= nums2[idn] {
nums1[i] = nums1[idm]
idm--
continue
}
if nums1[idm] < nums2[idn] {
nums1[i] = nums2[idn]
idn--
continue
}
}
}
================================================
FILE: algorithm/leetcode/100/ac100.go
================================================
package problem100
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isSameTree(p *TreeNode, q *TreeNode) bool {
if nil == p && nil == q {
return true
} else if nil == p || nil == q {
return false
} else if p.Val == q.Val {
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
} else {
return false
}
}
================================================
FILE: algorithm/leetcode/101/ac101.go
================================================
package problem101
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isSymmetric(root *TreeNode) bool {
return nil == root || isSame(root.Left, root.Right)
}
func isSame(left, right *TreeNode) bool {
if nil == left || nil == right {
return left == right
} else if left.Val != right.Val {
return false
} else {
return isSame(left.Left, right.Right) && isSame(left.Right, right.Left)
}
}
================================================
FILE: algorithm/leetcode/104/ac104.go
================================================
package problem104
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func maxDepth(root *TreeNode) int {
if nil == root {
return 0
}
left := maxDepth(root.Left)
right := maxDepth(root.Right)
if left >= right {
return left + 1
} else {
return right + 1
}
}
================================================
FILE: algorithm/leetcode/107/ac107.go
================================================
package problem107
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func levelOrderBottom(root *TreeNode) [][]int {
res := make([][]int, 0)
var dfs func(*TreeNode, int)
dfs = func(root *TreeNode, level int) {
if root == nil {
return
}
// 出现了新的 level
if level >= len(res) {
res = append([][]int{{}}, res...)
}
n := len(res)
res[n-level-1] = append(res[n-level-1], root.Val)
dfs(root.Left, level+1)
dfs(root.Right, level+1)
}
dfs(root, 0)
return res
}
================================================
FILE: algorithm/leetcode/107/ac107_test.go
================================================
package problem107
import (
"testing"
"fmt"
)
func Test_levelOrderBottom(t *testing.T) {
node15 := TreeNode{Val: 15, Left: nil, Right: nil}
node7 := TreeNode{Val: 7, Left: nil, Right: nil}
node9 := TreeNode{Val: 9, Left: nil, Right: nil}
node20 := TreeNode{Val: 20, Left: &node15, Right: &node7}
node3 := TreeNode{Val: 3, Left: &node9, Right: &node20}
result := levelOrderBottom(&node3)
fmt.Println(result)
}
================================================
FILE: algorithm/leetcode/108/ac108.go
================================================
package problem108
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func sortedArrayToBST(nums []int) *TreeNode {
if len(nums) < 1 {
return nil
}
mid := len(nums) / 2
return &TreeNode{
Val: nums[mid],
Left: sortedArrayToBST(nums[:mid]),
Right: sortedArrayToBST(nums[mid+1:]),
}
}
================================================
FILE: algorithm/leetcode/110/ac110.go
================================================
package problem110
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isBalanced(root *TreeNode) bool {
if nil == root {
return true
}
diff := depth(root.Left) - depth(root.Right)
if diff > 1 || diff < -1 {
return false
}
return isBalanced(root.Left) && isBalanced(root.Right)
}
func depth(root *TreeNode) int {
if nil == root {
return 0
}
left := depth(root.Left)
right := depth(root.Right)
if left > right {
return left + 1
} else {
return right + 1
}
}
================================================
FILE: algorithm/leetcode/111/ac111.go
================================================
package problem111
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func minDepth(root *TreeNode) int {
if nil == root {
return 0
}
leftDepth := minDepth(root.Left)
rightDepth := minDepth(root.Right)
if 0 == leftDepth || 0 == rightDepth {
return leftDepth + rightDepth + 1
} else {
if leftDepth < rightDepth {
return leftDepth + 1
} else {
return rightDepth + 1
}
}
}
================================================
FILE: algorithm/leetcode/112/ac112.go
================================================
package problem112
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func hasPathSum(root *TreeNode, sum int) bool {
if nil == root {
return false
}
// 判断叶子节点的值是否与指定值相等
if nil == root.Left && nil == root.Right {
return root.Val == sum
} else {
return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val)
}
}
================================================
FILE: algorithm/leetcode/118/ac118.go
================================================
package problem118
func generate(numRows int) [][]int {
if numRows < 1 {
return nil
}
ans := make([][]int, 0)
for i := 0; i < numRows; i++ {
oneLine := make([]int, i+1)
oneLine[0] = 1
oneLine[i] = 1
for j := 1; j < i; j++ {
oneLine[j] = ans[i-1][j-1] + ans[i-1][j]
}
ans = append(ans, oneLine)
}
return ans
}
================================================
FILE: algorithm/leetcode/119/ac119.go
================================================
package problem119
func getRow(rowIndex int) []int {
if rowIndex < 0 {
return nil
}
vi := make([]int, rowIndex+1)
vi[0] = 1
for i := 0; i <= rowIndex; i++ {
for j := i; j > 0; j-- {
vi[j] = vi[j] + vi[j-1]
}
}
return vi
}
================================================
FILE: algorithm/leetcode/120/ac120.go
================================================
package problem120
// 自下向上推导
// ans数组保存的是:到达下一行某位置的最小路径和
func minimumTotal(triangle [][]int) int {
ans := make([]int, len(triangle)+1)
for i := len(triangle) - 1; i >= 0; i-- {
for j := 0; j < len(triangle[i]); j++ {
ans[j] = myMin(ans[j], ans[j+1]) + triangle[i][j]
}
}
return ans[0]
}
func myMin(a, b int) int {
if a < b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/120/ac120_test.go
================================================
package problem120
import (
"testing"
"fmt"
)
func Test_minimumTotal(t *testing.T) {
data := [][]int{
{2},
{3, 4},
{6, 5, 7},
{4, 1, 8, 3}}
fmt.Println(minimumTotal(data))
}
================================================
FILE: algorithm/leetcode/121/ac121.go
================================================
package problem121
func maxProfit(prices []int) int {
if len(prices) < 1 {
return 0
}
maxProfit := 0
minPrice := prices[0]
for i := 1; i < len(prices); i++ {
if prices[i] < minPrice {
minPrice = prices[i]
continue
}
current := prices[i] - minPrice
if current > maxProfit {
maxProfit = current
}
}
return maxProfit
}
================================================
FILE: algorithm/leetcode/121/ac121_test.go
================================================
package problem121
import (
"testing"
"fmt"
)
func Test_maxProfit(t *testing.T) {
data := []int{7, 1, 5, 3, 6, 4}
fmt.Println(maxProfit(data))
}
================================================
FILE: algorithm/leetcode/122/ac122.go
================================================
package problem122
func maxProfit(prices []int) int {
if len(prices) < 1 {
return 0
}
ans := 0
for i := 1; i < len(prices); i++ {
if prices[i] > prices[i-1] {
ans += prices[i] - prices[i-1]
}
}
return ans
}
================================================
FILE: algorithm/leetcode/125/ac125.go
================================================
package problem125
func isPalindrome(s string) bool {
if len(s) <= 1 {
return true
}
left, right := 0, len(s)-1
for left < right {
for left < right && !isCharacter(s[left]) {
left++
}
for left < right && !isCharacter(s[right]) {
right--
}
if lowerCharacter(s[left]) != lowerCharacter(s[right]) {
return false
}
left++
right--
}
return true
}
func isCharacter(c byte) bool {
if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
return true
} else {
return false
}
}
func lowerCharacter(c byte) byte {
if c >= 'A' && c <= 'Z' {
return c + 'a' - 'A'
}
return c
}
================================================
FILE: algorithm/leetcode/125/ac125_test.go
================================================
package problem125
import (
"testing"
"fmt"
)
func Test_isCharacter(t *testing.T) {
str := "A man, a plan, a canal: Panama"
fmt.Println(isPalindrome(str))
str2 := "0P"
fmt.Println(isPalindrome(str2))
}
================================================
FILE: algorithm/leetcode/136/ac136.go
================================================
package problem136
func singleNumber(nums []int) int {
ans := 0
for _, value := range nums{
ans ^= value
}
return ans
}
================================================
FILE: algorithm/leetcode/137/ac137.go
================================================
package problem137
func singleNumber(nums []int) int {
dict := make(map[int]int, 0)
for _, v := range nums {
temp, ok := dict[v]
if !ok {
dict[v] = 1
} else if 2 == temp {
delete(dict, v)
} else {
dict[v]++
}
}
for k := range dict {
return k
}
return -1
}
func singleNumber2(nums []int) int {
ones, twos := 0, 0
for i := 0; i < len(nums); i++ {
ones = (ones ^ nums[i]) & ^twos
twos = (twos ^ nums[i]) & ^ones
}
return ones
}
func singleNumber3(nums []int) int {
one, two, three := 0, 0, 0
for _, v := range nums {
//当新来的为0时,two = two | 0,two不变,当新来的为1时,(当one此时为0,则two = two | 0,two不变;当one此时为1时,则two = two | 1,two变为1
two |= one & v
//新来的为0,one不变,新来为1时,one是0、1 交替改变
one ^= v
//当one和two为1是,three为1(此时代表要把one和two清零了)
three = one & two
//把one清0
one &= ^three
//把two清0
two &= ^three
}
return one
}
// 遍历32次每次记录某位的出现的次数,如果不能被三整除,说明那个出现一次的就在该位有值,那么ans 或该位一下就可以了
func singleNumber4(nums []int) int {
var ans = 0
var i uint8
for i = 0; i < 32; i++ {
cnt := 0
for _, v := range nums {
if ((v >> i) & 1) == 1 {
cnt++
}
}
ans |= (cnt % 3) << i
}
return int(int32(ans))
}
================================================
FILE: algorithm/leetcode/137/ac137_test.go
================================================
package problem137
import (
"fmt"
"testing"
)
func Test_singleNumber(t *testing.T) {
nums1 := []int{2, 2, 3, 2}
fmt.Println(singleNumber(nums1))
fmt.Println(singleNumber4(nums1))
}
================================================
FILE: algorithm/leetcode/160/ac160.go
================================================
package problem160
type ListNode struct {
Val int
Next *ListNode
}
func getIntersectionNode(headA, headB *ListNode) *ListNode {
if nil == headA || nil == headB {
return nil
}
ptrA, ptrB := headA, headB;
for ptrA != ptrB {
if nil == ptrA {
ptrA = headB
} else {
ptrA = ptrA.Next
}
if nil == ptrB {
ptrB = headA
} else {
ptrB = ptrB.Next
}
}
return ptrA
}
================================================
FILE: algorithm/leetcode/160/ac160_test.go
================================================
package problem160
import (
"testing"
"fmt"
)
// 两个单链表的相交节点为 node4 该节点值为 4
// node11 -> node22
// -> node4 -> node5 -> node6
// node1 -> node2 -> node3
func Test_getIntersectionNode(t *testing.T) {
node6 := ListNode{Val: 6, Next: nil}
node5 := ListNode{Val: 5, Next: &node6}
node4 := ListNode{Val: 4, Next: &node5}
node3 := ListNode{Val: 3, Next: &node4}
node2 := ListNode{Val: 2, Next: &node3}
node1 := ListNode{Val: 1, Next: &node2}
node22 := ListNode{Val: 22, Next: &node4}
node11 := ListNode{Val: 11, Next: &node22}
fmt.Println(getIntersectionNode(&node11, &node1).Val)
}
================================================
FILE: algorithm/leetcode/167/ac167.go
================================================
package problem167
func twoSum(numbers []int, target int) []int {
left, right := 0, len(numbers)-1
for left < right {
sum := numbers[left] + numbers[right]
if sum < target {
left++
} else if sum > target {
right--
} else {
return []int{left + 1, right + 1}
}
}
return nil
}
================================================
FILE: algorithm/leetcode/168/ac141.go
================================================
package problem168
func convertToTitle(n int) string {
ans := make([]byte, 0)
for n > 0 {
n--
ans = append(ans, byte(n%26)+'A')
n /= 26
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return string(ans)
}
================================================
FILE: algorithm/leetcode/168/ac141_test.go
================================================
package problem168
import (
"testing"
"fmt"
)
func Test_convertToTitle(t *testing.T) {
fmt.Println(convertToTitle(27))
}
================================================
FILE: algorithm/leetcode/169/ac169.go
================================================
package problem169
func majorityElement(nums []int) int {
if len(nums) < 1 {
return 0
}
count, flag := 1, nums[0]
for i := 1; i < len(nums); i++ {
if count < 1 {
flag = nums[i]
count = 1
continue
}
if nums[i] == flag {
count++
} else {
count--
}
}
return flag
}
================================================
FILE: algorithm/leetcode/171/ac171.go
================================================
package problem171
func titleToNumber(s string) int {
ans := 0
for _, value := range s {
ans = int(value) - int('A') + 1 + ans*26
}
return ans
}
================================================
FILE: algorithm/leetcode/172/ac172.go
================================================
package problem172
func trailingZeroes(n int) int {
if 0 == n {
return 0
} else {
return n/5 + trailingZeroes(n/5)
}
}
================================================
FILE: algorithm/leetcode/189/ac189.go
================================================
package problem189
func rotate(nums []int, k int) {
if nil == nums {
return
}
length := len(nums)
if k > length {
k %= length
}
if 0 == k || k == length {
return
}
reverse(nums)
reverse(nums[:k])
reverse(nums[k:])
}
func reverse(nums []int) {
for i, j := 0, len(nums)-1; i < j; i, j = i+1, j-1 {
nums[i], nums[j] = nums[j], nums[i]
}
}
================================================
FILE: algorithm/leetcode/189/ac189_test.go
================================================
package problem189
import (
"testing"
"fmt"
)
func Test_rotate(t *testing.T) {
numbers := []int{1, 2, 3, 4, 5, 6, 7}
rotate(numbers, 3)
fmt.Println(numbers)
numbers2 := []int{-1}
rotate(numbers2, 2)
fmt.Println(numbers2)
}
================================================
FILE: algorithm/leetcode/191/ac191.go
================================================
package problem191
func hammingWeight(n uint32) int {
ans := 0
for 0 != n {
n &= n - 1
ans++
}
return ans
}
================================================
FILE: algorithm/leetcode/191/ac191_test.go
================================================
package problem191
import (
"testing"
"fmt"
)
func Test_hammingWeight(t *testing.T) {
fmt.Println(hammingWeight(uint32(11)))
}
================================================
FILE: algorithm/leetcode/198/ac198.go
================================================
package problem198
func rob(nums []int) int {
if nil == nums || len(nums) < 1 {
return 0
}
if 1 == len(nums) {
return nums[0]
} else if 2 == len(nums) {
return maxInt(nums[0], nums[1])
}
f := make([]int, len(nums))
f[0] = nums[0]
f[1] = maxInt(nums[0], nums[1])
for i := 2; i < len(nums); i++ {
f[i] = maxInt(nums[i]+f[i-2], f[i-1])
}
return f[len(nums)-1]
}
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/202/ac202.go
================================================
package problem202
func isHappy(n int) bool {
slow, fast := n, n
for {
slow = digitSquareSum(slow)
fast = digitSquareSum(fast)
fast = digitSquareSum(fast)
if 1 == fast {
return true
}
if slow == fast {
break
}
}
return false
}
func digitSquareSum(n int) int {
sum, tmp := 0, 0
for ; 0 != n; n /= 10 {
tmp = n % 10
sum += tmp * tmp
}
return sum
}
================================================
FILE: algorithm/leetcode/203/ac203.go
================================================
package problem203
type ListNode struct {
Val int
Next *ListNode
}
// 递归写法 效率低
func removeElements(head *ListNode, val int) *ListNode {
if nil == head {
return nil
}
head.Next = removeElements(head.Next, val)
if head.Val == val {
return head.Next
} else {
return head
}
}
// 非递归 效率高
func removeElements2(head *ListNode, val int) *ListNode {
for nil != head && val == head.Val {
head = head.Next
}
currentPtr := head
for nil != currentPtr && nil != currentPtr.Next {
if currentPtr.Next.Val == val {
currentPtr.Next = currentPtr.Next.Next
} else {
currentPtr = currentPtr.Next
}
}
return head
}
================================================
FILE: algorithm/leetcode/204/ac204.go
================================================
package problem204
func countPrimes(n int) int {
notPrime := make([]bool, n)
count := 0
for i := 2; i < n; i++ {
if !notPrime[i] {
count++
if temp := i * i; temp < n {
for j := temp; j < n; j += i {
notPrime[j] = true
}
}
}
}
return count
}
================================================
FILE: algorithm/leetcode/205/ac205.go
================================================
package problem205
func isIsomorphic(s string, t string) bool {
length := len(s)
num1 := make([]int, 256)
num2 := make([]int, 256)
for i := 0; i < length; i++ {
if num1[s[i]] != num2[t[i]] {
return false
}
num1[s[i]] = i + 1
num2[t[i]] = i + 1
}
return true
}
================================================
FILE: algorithm/leetcode/205/ac205_test.go
================================================
package problem205
import (
"testing"
"fmt"
)
func Test_isIsomorphic(t *testing.T) {
str1 := "egg"
str2 := "add"
fmt.Println(isIsomorphic(str1, str2))
}
================================================
FILE: algorithm/leetcode/206/ac206.go
================================================
package problem206
type ListNode struct {
Val int
Next *ListNode
}
func reverseList(head *ListNode) *ListNode {
var newHead *ListNode
for nil != head {
nextNode := head.Next
head.Next = newHead
newHead = head
head = nextNode
}
return newHead
}
func reverseList1(head *ListNode) *ListNode {
return reverseCore(head, nil)
}
func reverseCore(head, newHead *ListNode) *ListNode {
if nil == head {
return newHead
}
nextNode := head.Next
head.Next = newHead
return reverseCore(nextNode, head)
}
================================================
FILE: algorithm/leetcode/217/ac217.go
================================================
package problem217
import "sort"
func containsDuplicate(nums []int) bool {
dict := make(map[int]int, len(nums))
for _, value := range nums {
dict[value]++
}
for _, v := range dict {
if v > 1 {
return true
}
}
return false
}
func containsDuplicate2(nums []int) bool {
sort.Ints(nums)
for i := 1; i < len(nums); i++ {
if nums[i-1] == nums[i] {
return true
}
}
return false
}
================================================
FILE: algorithm/leetcode/219/ac219.go
================================================
package problem219
func containsNearbyDuplicate(nums []int, k int) bool {
dict := make(map[int]int, len(nums))
for i := 0; i < len(nums); i++ {
if value, ok := dict[nums[i]]; ok {
if -k <= value-i || value-i <= k {
return true
}
}
dict[nums[i]] = i
}
return false
}
================================================
FILE: algorithm/leetcode/226/ac226.go
================================================
package problem226
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func invertTree(root *TreeNode) *TreeNode {
if nil == root {
return nil
}
return &TreeNode{Val: root.Val, Left: invertTree(root.Right), Right: invertTree(root.Left)}
}
================================================
FILE: algorithm/leetcode/231/ac231.go
================================================
package problem231
func isPowerOfTwo(n int) bool {
if n <= 0 {
return false
}
return 0 == (n & (n - 1))
}
================================================
FILE: algorithm/leetcode/234/ac234.go
================================================
package problem234
type ListNode struct {
Val int
Next *ListNode
}
var h *ListNode
func isPalindrome(head *ListNode) bool {
if nil == head {
return true
}
if nil == h {
h = head
}
flag := true
if nil != head.Next {
flag = flag && isPalindrome(head.Next)
}
flag = flag && (h.Val == head.Val)
h = h.Next
return flag
}
func isPalindrome2(head *ListNode) bool {
nums := make([]int, 0, 128)
for nil != head {
nums = append(nums, head.Val)
head = head.Next
}
for i, j := 0, len(nums)-1; i < j; {
if nums[i] != nums[j] {
return false
}
i++
j--
}
return true
}
================================================
FILE: algorithm/leetcode/258/ac258.go
================================================
package problem258
func addDigits(num int) int {
return 1 + (num-1)%9
}
================================================
FILE: algorithm/leetcode/263/ac263.go
================================================
package problem263
func isUgly(num int) bool {
for i := 5; i >= 2 && num > 0; i-- {
for 0 == num%i {
num /= i
}
}
return 1 == num
}
================================================
FILE: algorithm/leetcode/268/ac268.go
================================================
package problem268
import "sort"
func missingNumber(nums []int) int {
if nil == nums {
return -1
}
sort.Ints(nums)
left, right := 0, len(nums)-1
for left <= right {
middle := (left + right) / 2
if nums[middle] == middle {
left = middle + 1
} else if nums[middle] > middle {
right = middle - 1
}
}
return left
}
func missingNumber2(nums []int) int {
sum := len(nums)
for i := 0; i < len(nums); i++ {
sum -= i - nums[i]
}
return sum
}
func missingNumber3(nums []int) int {
xor, i := 0, 0
for ; i < len(nums); i++ {
xor ^= i ^ nums[i]
}
return xor ^ i
}
================================================
FILE: algorithm/leetcode/268/ac268_test.go
================================================
package problem268
import (
"testing"
"fmt"
)
func Test_missingNumber(t *testing.T) {
nums := []int{3, 0, 1}
nums2 := []int{9, 6, 4, 2, 3, 5, 7, 0, 1}
fmt.Println(missingNumber(nums))
fmt.Println(missingNumber(nums2))
}
================================================
FILE: algorithm/leetcode/283/ac283.go
================================================
package problem283
func moveZeroes(nums []int) {
if nil == nums || len(nums) == 0 {
return
}
insertPos := 0
for _, values := range nums {
if 0 != values {
nums[insertPos] = values
insertPos++
}
}
for insertPos < len(nums) {
nums[insertPos] = 0
insertPos++
}
}
================================================
FILE: algorithm/leetcode/290/ac290.go
================================================
package problem290
import "strings"
func wordPattern(pattern string, str string) bool {
words := strings.Split(str, " ")
if len(pattern) != len(words) {
return false
}
dictPattern := make(map[byte]int)
dictString := make(map[string]int)
for i := 0; i < len(pattern); i++ {
patternValue := dictPattern[pattern[i]]
dictPattern[pattern[i]] = i+1
stringValue := dictString[words[i]]
dictString[words[i]] = i+1
if patternValue != stringValue {
return false
}
}
return true
}
================================================
FILE: algorithm/leetcode/290/ac290_test.go
================================================
package problem290
import (
"testing"
"fmt"
)
func Test_wordPattern(t *testing.T) {
pattern := "abba"
str := "dog cat cat fish"
fmt.Println(wordPattern(pattern, str))
}
================================================
FILE: algorithm/leetcode/292/ac292.go
================================================
package problem292
// Nim游戏: 每次最多取 1~n 个石头
// 精髓在于: 先手获胜的条件是 总石头数不能为 n+1 的倍数
func canWinNim(n int) bool {
if n < 1 {
return false
}
return 0 != n % 4
}
================================================
FILE: algorithm/leetcode/303/ac303.go
================================================
package problem303
type NumArray struct {
data []int
}
func Constructor(nums []int) NumArray {
for i := 1; i < len(nums); i++ {
nums[i] += nums[i-1]
}
return NumArray{data: nums}
}
func (this *NumArray) SumRange(i int, j int) int {
if 0 == i {
return this.data[j]
}
return this.data[j] - this.data[i-1]
}
================================================
FILE: algorithm/leetcode/303/ac303_test.go
================================================
package problem303
import (
"testing"
"fmt"
)
func TestNumArray_SumRange(t *testing.T) {
nums := []int{-2, 0, 3, -5, 2, -1}
obj := Constructor(nums)
fmt.Println(obj.data)
fmt.Println(obj.SumRange(0, 2))
}
================================================
FILE: algorithm/leetcode/326/ac326.go
================================================
package problem326
// 1162261467 is 3^19, 3^20 is bigger than int
func isPowerOfThree(n int) bool {
return n > 0 && (0 == 1162261467%n)
}
================================================
FILE: algorithm/leetcode/326/ac326_test.go
================================================
package problem326
import "testing"
func Test_isPowerOfThree(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
args args
want bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isPowerOfThree(tt.args.n); got != tt.want {
t.Errorf("isPowerOfThree() = %v, want %v", got, tt.want)
}
})
}
}
================================================
FILE: algorithm/leetcode/342/ac342.go
================================================
package problem342
// 4的幂应该满足的条件:
// 1. 大于0
// 2. 该数字的二进制表示中 仅有1个bit位数字为 1
// 3. 数字为 1 的bit位,应该出现在二进制数位的奇数位置
func isPowerOfFour(num int) bool {
return num > 0 && (0 == num&(num-1)) && (num&0x55555555 != 0)
}
================================================
FILE: algorithm/leetcode/344/ac344.go
================================================
package problem344
func reverseString(s string) string {
char := []rune(s)
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
char[i], char[j] = char[j], char[i]
}
return string(char)
}
================================================
FILE: algorithm/leetcode/344/ac344_test.go
================================================
package problem344
import (
"testing"
"fmt"
)
func Test_reverseString(t *testing.T) {
s := "abcdefg"
fmt.Println(reverseString(s))
}
================================================
FILE: algorithm/leetcode/345/ac345.go
================================================
package problem345
import "strings"
func reverseVowels(s string) string {
if len(s) == 0 {
return s
}
vowels := "aeiouAEIOU"
chars := []rune(s)
for start, end := 0, len(chars)-1; start < end; {
for start < end && !strings.ContainsRune(vowels, chars[start]) {
start++
}
for start < end && !strings.ContainsRune(vowels, chars[end]) {
end--
}
chars[start], chars[end] = chars[end], chars[start]
start++
end--
}
return string(chars)
}
================================================
FILE: algorithm/leetcode/345/ac345_test.go
================================================
package problem345
import (
"testing"
"fmt"
)
func Test_reverseVowels(t *testing.T) {
str := "hello"
str2 := "leetcode"
fmt.Println(reverseVowels(str))
fmt.Println(reverseVowels(str2))
}
================================================
FILE: algorithm/leetcode/349/ac349.go
================================================
package problem349
func intersection(nums1 []int, nums2 []int) []int {
if nil == nums1 || len(nums1) == 0 {
return nums1
}
if nil == nums2 || len(nums2) == 0 {
return nums2
}
dict1 := make(map[int]int, 1)
dict2 := make(map[int]int, 1)
for _, v := range nums1 {
dict1[v]++
}
for _, v := range nums2 {
dict2[v]++
}
ans := make([]int, 0)
for k := range dict1 {
if _, ok := dict2[k]; ok {
ans = append(ans, k)
}
}
return ans
}
func intersection2(nums1 []int, nums2 []int) []int {
dict := make(map[int]int)
for _, v := range nums1 {
dict[v] = 1
}
for _, v := range nums2 {
if 1 == dict[v] {
dict[v] = 2
}
}
ans := make([]int, 0)
for k, v := range dict {
if 2 == v {
ans = append(ans, k)
}
}
return ans
}
================================================
FILE: algorithm/leetcode/349/ac349_test.go
================================================
package problem349
import (
"testing"
"fmt"
)
func Test_intersection(t *testing.T) {
num1 := []int{1, 2, 2, 1}
num2 := []int{2, 2}
fmt.Println(intersection(num1, num2))
}
================================================
FILE: algorithm/leetcode/350/ac350.go
================================================
package problem350
func intersect(nums1 []int, nums2 []int) []int {
if nil == nums1 || len(nums1) == 0 {
return nums1
}
if nil == nums2 || len(nums2) == 0 {
return nums2
}
dict1 := make(map[int]int, 0)
for _, v := range nums1 {
dict1[v]++
}
dict2 := make(map[int]int, 0)
for _, v := range nums2 {
dict2[v]++
}
ans := make([]int, 0)
for k1, v1 := range dict1 {
if v2, ok := dict2[k1]; ok {
for repeat := minInt(v1, v2); repeat > 0; repeat-- {
ans = append(ans, k1)
}
}
}
return ans
}
func minInt(a, b int) int {
if a <= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/350/ac350_test.go
================================================
package problem350
import (
"testing"
"fmt"
)
func Test_intersect(t *testing.T) {
num1 := []int{1, 2, 2, 1}
num2 := []int{2, 2}
fmt.Println(intersect(num1, num2))
}
================================================
FILE: algorithm/leetcode/367/ac367.go
================================================
package problem367
func isPerfectSquare(num int) bool {
if 1 == num {
return true
}
for middle := num / 2; middle >= 2; middle-- {
result := middle * middle
if result == num {
return true
} else if result < num {
return false
}
}
return false
}
func isPerfectSquare2(num int) bool {
low, high := 1, num
for low <= high {
middle := (low + high) >> 1
result := middle * middle
if result == num {
return true
} else if result < num {
low = middle + 1
} else {
high = middle - 1
}
}
return false
}
================================================
FILE: algorithm/leetcode/371/ac371.go
================================================
package problem371
func getSum(a int, b int) int {
if 0 == b {
return a
}
sum := a ^ b
carry := (a & b) << 1
return getSum(sum, carry)
}
================================================
FILE: algorithm/leetcode/383/ac383.go
================================================
package problem383
func canConstruct(ransomNote string, magazine string) bool {
if len(ransomNote) == 0 && len(magazine) == 0 {
return true
}
if len(magazine) == 0 {
return false
}
if len(ransomNote) == 0 {
return true
}
dict1 := make(map[rune]int, 0)
for _, v := range ransomNote {
dict1[v]++
}
dict2 := make(map[rune]int, 0)
for _, v := range magazine {
dict2[v]++
}
for k1, v1 := range dict1 {
if v2, ok := dict2[k1]; !ok || v2 < v1 {
return false
}
}
return true
}
================================================
FILE: algorithm/leetcode/383/ac383_test.go
================================================
package problem383
import (
"testing"
"fmt"
)
func Test_canConstruct(t *testing.T) {
ransom := "aa"
magazine := "ab"
fmt.Println(canConstruct(ransom, magazine))
}
================================================
FILE: algorithm/leetcode/387/ac387.go
================================================
package problem387
func firstUniqChar(s string) int {
if len(s) == 0 {
return -1
}
dict := make(map[rune]int, 0)
for _, v := range s {
dict[v]++
}
ans := -1
for id, v := range s {
if 1 == dict[v] {
return id
}
}
return ans
}
// 效率更高
func firstUniqChar2(s string) int {
position := [26]int{}
for _, v := range s {
position[v-'a']++
}
for id, v := range s {
if 1 == position[v-'a'] {
return id
}
}
return -1
}
================================================
FILE: algorithm/leetcode/389/ac389.go
================================================
package problem389
func findTheDifference(s string, t string) byte {
dict1 := make(map[rune]int, 0)
for _, v := range s {
dict1[v]++
}
dict2 := make(map[rune]int, 0)
for _, v := range t {
dict2[v]++
}
for k2, v2 := range dict2 {
if v1 := dict1[k2]; v1 != v2 {
return byte(k2)
}
}
return byte(0)
}
// 更高效的做法
func findTheDifference2(s string, t string) byte {
ans := byte(0)
for i := 0; i < len(s); i++ {
ans ^= s[i]
}
for i := 0; i < len(t); i++ {
ans ^= t[i]
}
return ans
}
================================================
FILE: algorithm/leetcode/389/ac389_test.go
================================================
package problem389
import (
"testing"
"fmt"
)
func Test_findTheDifference(t *testing.T) {
s := "a"
tt := "aa"
fmt.Printf("%c\n", findTheDifference(s, tt))
}
================================================
FILE: algorithm/leetcode/400/ac400.go
================================================
package problem400
import "strconv"
func findNthDigit(n int) int {
// 初始值定义 1 位数 共有 9 个, 起始数字为 1
len, count, start := 1, 9, 1
for n > len*count {
n -= len * count
len++
count *= 10
start *= 10
}
start += (n - 1) / len
s := strconv.Itoa(start)
return int(s[(n-1)%len] - '0')
}
================================================
FILE: algorithm/leetcode/400/ac400_test.go
================================================
package problem400
import (
"testing"
"fmt"
)
func Test_findNthDigit(t *testing.T) {
fmt.Println(findNthDigit(3))
fmt.Println(findNthDigit(11))
fmt.Println(findNthDigit(31))
}
================================================
FILE: algorithm/leetcode/404/ac404.go
================================================
package problem404
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func sumOfLeftLeaves(root *TreeNode) int {
if nil == root {
return 0
}
sum := 0
if nil != root.Left && nil == root.Left.Left && nil == root.Left.Right {
sum += root.Left.Val
} else {
sum += sumOfLeftLeaves(root.Left)
}
sum += sumOfLeftLeaves(root.Right)
return sum
}
================================================
FILE: algorithm/leetcode/405/ac405.go
================================================
package problem405
func toHex(num int) string {
charMap := []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
if 0 == num {
return "0"
}
numUint32 := uint32(num)
ans := make([]byte, 0)
for 0 != numUint32 {
ans = append(ans, charMap[numUint32&15])
numUint32 >>= 4
}
for i, j := 0, len(ans)-1; i < j; {
ans[i], ans[j] = ans[j], ans[i]
i++
j--
}
return string(ans)
}
================================================
FILE: algorithm/leetcode/405/ac405_test.go
================================================
package problem405
import (
"testing"
"fmt"
)
func Test_toHex(t *testing.T) {
fmt.Println(toHex(14))
fmt.Println(toHex(-1))
}
================================================
FILE: algorithm/leetcode/409/ac409.go
================================================
package problem409
//1、统计所有字母的出现频率(分大小写)
//2、统计只出现奇数次数字母的个数
//3、如果2中结果不为0,字符串的长度减去2中的字母个数+1
//
//其中3的意思是,保留出现次数最多的那个奇数字母,剩下的需要全部减1变成偶数去构造
func longestPalindrome(s string) int {
dict := make(map[byte]int, 0)
for i := 0; i < len(s); i++ {
dict[s[i]]++
}
odd := 0
for _, v := range dict {
if v&1 != 0 {
odd++
}
}
ans := len(s)
if 0 != odd {
ans -= odd - 1
}
return ans
}
================================================
FILE: algorithm/leetcode/409/ac409_test.go
================================================
package problem409
import (
"testing"
"fmt"
)
func Test_longestPalindrome(t *testing.T) {
str := "abccccdd"
fmt.Println(longestPalindrome(str))
}
================================================
FILE: algorithm/leetcode/412/ac412.go
================================================
package problem412
import (
"strconv"
"fmt"
)
func fizzBuzz(n int) []string {
ans := make([]string, 0)
for i := 1; i <= n; i++ {
ans = append(ans, number(i))
}
return ans
}
func number(n int) string {
if 0 == n%3 && 0 == n%5 {
return "FizzBuzz"
} else if 0 == n%5 {
return "Buzz"
} else if 0 == n%3 {
return "Fizz"
} else {
return strconv.Itoa(n)
}
}
func fizzBuzz2(n int) []string {
ans := make([]string, 0)
for i, fizz, buzz := 1, 0, 0; i <= n; i++ {
fizz++
buzz++
if 3 == fizz && 5 == buzz {
ans = append(ans, "FizzBuzz")
fizz = 0
buzz = 0
} else if 3 == fizz {
ans = append(ans, "Fizz")
fizz = 0
} else if 5 == buzz {
ans = append(ans, "Buzz")
buzz = 0
} else {
ans = append(ans, fmt.Sprintf("%d", i))
}
}
return ans
}
================================================
FILE: algorithm/leetcode/412/ac412_test.go
================================================
package problem412
import (
"testing"
"fmt"
)
func Test_fizzBuzz(t *testing.T) {
for _, v := range fizzBuzz2(15) {
fmt.Println(v)
}
}
================================================
FILE: algorithm/leetcode/414/ac414.go
================================================
package problem414
import (
"sort"
)
func thirdMax(nums []int) int {
length := len(nums)
if length < 1 {
return -1
}
sort.Ints(nums)
ans := nums[length-1]
j := 2
for i := length - 2; i >= 0 && j > 0; i-- {
if nums[i] != ans {
ans = nums[i]
j--
}
}
if 0 != j {
ans = nums[length-1]
}
return ans
}
func thirdMax2(nums []int) int {
empty := [4]bool{true, true, true, true}
max := [4]int{}
for _, v := range nums {
if (!empty[1] && v == max[1]) || (!empty[2] && v == max[2]) || (!empty[3] && v == max[3]) {
continue
}
if empty[1] || v > max[1] {
max[3] = max[2]
max[2] = max[1]
max[1] = v
if !empty[2] {
empty[3] = false
}
if !empty[1] {
empty[2] = false
}
empty[1] = false
} else if empty[2] || v > max[2] {
max[3] = max[2]
max[2] = v
if !empty[2] {
empty[3] = false
}
empty[2] = false
} else if empty[3] || v > max[3] {
max[3] = v
empty[3] = false
}
}
if !empty[3] {
return max[3]
} else {
return max[1]
}
}
================================================
FILE: algorithm/leetcode/414/ac414_test.go
================================================
package problem414
import (
"testing"
"fmt"
)
func Test_thirdMax(t *testing.T) {
num1 := []int{3, 2, 1}
num2 := []int{2, 1}
num3 := []int{2, 3, 2, 1}
fmt.Println(thirdMax(num1))
fmt.Println(thirdMax(num2))
fmt.Println(thirdMax(num3))
}
================================================
FILE: algorithm/leetcode/415/ac415.go
================================================
package problem415
func addStrings(num1 string, num2 string) string {
str1 := []rune(num1)
str2 := []rune(num2)
ans := make([]rune, 0)
var carry rune = 0
for i, j := len(str1)-1, len(str2)-1; i >= 0 || j >= 0 || 1 == carry; i, j = i-1, j-1 {
var x, y rune = 0, 0
if i >= 0 {
x = str1[i] - '0'
}
if j >= 0 {
y = str2[j] - '0'
}
bit := (x+y+carry)%rune(10) + '0'
carry = (x + y + carry) / rune(10)
ans = append(ans, bit)
}
for i, j := 0, len(ans)-1; i < j; {
ans[i], ans[j] = ans[j], ans[i]
i++
j--
}
return string(ans)
}
func addStrings2(num1 string, num2 string) string {
ans := make([]byte, 0)
carry := byte(0)
for i, j := len(num1)-1, len(num2)-1; i >= 0 || j >= 0 || byte(1) == carry; i, j = i-1, j-1 {
var x, y byte
if i >= 0 {
x = num1[i] - '0'
}
if j >= 0 {
y = num2[j] - '0'
}
bit := (x+y+carry)%10 + '0'
carry = (x + y + carry) / 10
ans = append(ans, bit)
}
for i, j := 0, len(ans)-1; i < j; {
ans[i], ans[j] = ans[j], ans[i]
i++
j--
}
return string(ans)
}
================================================
FILE: algorithm/leetcode/415/ac415_test.go
================================================
package problem415
import (
"testing"
"fmt"
)
func Test_addStrings(t *testing.T) {
str1 := "123"
str2 := "231"
fmt.Println(addStrings(str1, str2))
}
================================================
FILE: algorithm/leetcode/434/ac434.go
================================================
package problem434
func countSegments(s string) int {
ans := 0
for i := 0; i < len(s); i++ {
if s[i] != ' ' && (0 == i || ' ' == s[i-1]) {
ans++
}
}
return ans
}
================================================
FILE: algorithm/leetcode/437/ac437.go
================================================
package problem437
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func pathSum(root *TreeNode, sum int) int {
if nil == root {
return 0
}
return pathSumFrom(root, sum) + pathSum(root.Left, sum) + pathSum(root.Right, sum)
}
func pathSumFrom(node *TreeNode, sum int) int {
if nil == node {
return 0
}
ans := 0
if node.Val == sum {
ans = 1
}
return ans + pathSumFrom(node.Left, sum-node.Val) + pathSumFrom(node.Right, sum-node.Val)
}
================================================
FILE: algorithm/leetcode/438/ac438.go
================================================
package problem438
func findAnagrams(s string, p string) []int {
dict := make(map[byte]int, 0)
for i := 0; i < len(p); i++ {
dict[p[i]]++
}
ans := make([]int, 0)
//two points, initialize count to p's length
left, right, count := 0, 0, len(p)
for right < len(s) {
//move right everytime, if the character exists in p's hash, decrease the count
//current hash value >= 1 means the character is existing in p
if dict[s[right]] >= 1 {
count--
}
dict[s[right]]--
right++
//when the count is down to 0, means we found the right anagram
//then add window's left to result list
if 0 == count {
ans = append(ans, left)
}
//if we find the window's size equals to p, then we have to move left (narrow the window) to find the new match window
//++ to reset the hash because we kicked out the left
//only increase the count if the character is in p
//the count >= 0 indicate it was original in the hash, cuz it won't go below 0
if right-left == len(p) {
if v, ok := dict[s[left]]; ok && v >= 0 {
count++
}
dict[s[left]]++
left++
}
}
return ans
}
================================================
FILE: algorithm/leetcode/438/ac438_test.go
================================================
package problem438
import (
"testing"
"fmt"
)
func Test_findAnagrams(t *testing.T) {
s := "cbaebabacd"
p := "abc"
fmt.Println(findAnagrams(s, p))
s1 := "abab"
p1 := "ab"
fmt.Println(findAnagrams(s1, p1))
s2 := "ababababab"
p2 := "aab"
fmt.Println(findAnagrams(s2, p2))
}
================================================
FILE: algorithm/leetcode/441/ac441.go
================================================
package problem441
import "math"
// 题目的意思其实就是从1~x层完整楼梯硬币数量加起来,要小于等于n,求最大的x。
// 说到加起来的数量,很容易想到求累加和,我们知道求累加和的公式为:
// sum = (1+x)*x/2
// 这里就是要求 sum <= n 了。我们反过来求层数x。如果直接开方来求会存在错误,必须因式分解求得准确的x值:
//
// (1+x)*x/2 <= n
// x*x + x <= 2*n
// 4*x*x + 4*x <= 8*n
// (2*x + 1)*(2*x + 1) - 1 <= 8*n
// x <= (sqrt(8*n + 1) - 1) / 2
func arrangeCoins(n int) int {
return int((math.Sqrt(float64(8*n+1)) - 1) / 2)
}
================================================
FILE: algorithm/leetcode/441/ac441_test.go
================================================
package problem441
import (
"testing"
"fmt"
)
func Test_arrangeCoins(t *testing.T) {
fmt.Println(arrangeCoins(5))
fmt.Println(arrangeCoins(8))
}
================================================
FILE: algorithm/leetcode/443/ac443.go
================================================
package problem443
import "strconv"
func compress(chars []byte) int {
index, indexAns := 0, 0
for index < len(chars) {
currentChar := chars[index]
count := 0
for index < len(chars) && chars[index] == currentChar {
index++
count++
}
chars[indexAns] = currentChar
indexAns++
if 1 != count {
for _, v := range []byte(strconv.Itoa(count)) {
chars[indexAns] = v
indexAns++
}
}
}
return indexAns
}
================================================
FILE: algorithm/leetcode/443/ac443_test.go
================================================
package problem443
import (
"testing"
"fmt"
)
func Test_compress(t *testing.T) {
data := []byte{'a', 'a', 'b', 'b', 'c', 'c', 'c'}
fmt.Println(compress(data))
}
================================================
FILE: algorithm/leetcode/447/ac447.go
================================================
package problem447
func numberOfBoomerangs(points [][]int) int {
ans := 0
for i := 0; i < len(points); i++ {
dict := make(map[int]int, 0)
for j := 0; j < len(points); j++ {
if i == j {
continue
}
d := getDistance(points[i], points[j])
dict[d]++
}
for _, v := range dict {
ans += v * (v - 1)
}
}
return ans
}
func getDistance(a, b []int) int {
dx := a[0] - b[0]
dy := a[1] - b[1]
return dx*dx + dy*dy
}
================================================
FILE: algorithm/leetcode/448/ac448.go
================================================
package problem448
func findDisappearedNumbers(nums []int) []int {
ans := make([]int, 0)
for i := 0; i < len(nums); i++ {
id := abs(nums[i]) - 1
if nums[id] > 0 {
nums[id] = -nums[id]
}
}
for i := 0; i < len(nums); i++ {
if nums[i] > 0 {
ans = append(ans, i+1)
}
}
return ans
}
func abs(n int) int {
if n < 0 {
n = -n
}
return n
}
================================================
FILE: algorithm/leetcode/453/ac453.go
================================================
package problem453
func minMoves(nums []int) int {
if nil == nums || len(nums) == 0 {
return -1
}
if len(nums) == 1 {
return 0
}
minNumber := nums[0]
sum := nums[0]
for i := 1; i < len(nums); i++ {
sum += nums[i]
if nums[i] < minNumber {
minNumber = nums[i]
}
}
return sum - minNumber*len(nums)
}
================================================
FILE: algorithm/leetcode/453/ac453_test.go
================================================
package problem453
import (
"testing"
"fmt"
)
func Test_minMoves(t *testing.T) {
num := []int{1, 2, 3}
fmt.Println(minMoves(num))
}
================================================
FILE: algorithm/leetcode/455/ac455.go
================================================
package problem455
import "sort"
func findContentChildren(g []int, s []int) int {
sort.Ints(g)
sort.Ints(s)
i := 0
for j := 0; i < len(g) && j < len(s); j++ {
if g[i] <= s[j] {
i++
}
}
return i
}
================================================
FILE: algorithm/leetcode/455/ac455_test.go
================================================
package problem455
import (
"testing"
"fmt"
)
func Test_findContentChildren(t *testing.T) {
g1 := []int{1, 2, 3}
s1 := []int{1, 1}
fmt.Println(findContentChildren(g1, s1))
g2 := []int{1, 2}
s2 := []int{1, 2, 3}
fmt.Println(findContentChildren(g2, s2))
}
================================================
FILE: algorithm/leetcode/458/ac458.go
================================================
package problem458
// 1. 一只猪在一小时内最多能验多少桶?
//
//一次喝一个桶的,15分钟后没挂再喝第二桶,一小时60分钟内可以喝 60/15 = 4 次,如果有5桶水,那个只要喝前4桶就只能第5桶是否有毒。
//
//因此一只小猪在一小时可以验5桶水
//
//
//2. 两只呢?
//
//既然一只能验5桶,那么用二维的思路,2只猪应该可以验5*5桶:
//
//猪A负责行,猪B负责列,每15分钟试喝一行/一列的所有5桶水,通过2只猪上天的时间能推断出毒水在几行几列。
//
//1 2 3 4 5
//
//6 7 8 9 10
//
//11 12 13 14 15
//
//16 17 18 19 20
//
//21 22 23 24 25
//
//
//3. N只
//
//如此类推到N只的情况,使用N维去分区,则5^N >= 1000即为解决本题的公式。
func poorPigs(buckets int, minutesToDie int, minutesToTest int) int {
pigs := 0
for mypow(minutesToTest/minutesToDie+1, pigs) < buckets {
pigs++
}
return pigs
}
func mypow(a, b int) int {
if 0 == b {
return 1
}
if 1 == b {
return a
}
if 0 == b&1 {
return mypow(a*a, b/2)
} else {
return a * mypow(a*a, b/2)
}
}
================================================
FILE: algorithm/leetcode/459/ac459.go
================================================
package problem459
import "strings"
func repeatedSubstringPattern(s string) bool {
if len(s) < 2 {
return false
}
newStr := s + s
return strings.Contains(newStr[1:len(newStr)-1], s)
}
================================================
FILE: algorithm/leetcode/459/ac459_test.go
================================================
package problem459
import (
"testing"
"fmt"
)
func Test_repeatedSubstringPattern(t *testing.T) {
s := "ab"
fmt.Println(repeatedSubstringPattern(s))
s2 := "abab"
fmt.Println(repeatedSubstringPattern(s2))
}
================================================
FILE: algorithm/leetcode/461/ac461.go
================================================
package problem461
func hammingDistance(x int, y int) int {
n := x ^ y
ans := 0
for n != 0 {
n &= n - 1
ans++
}
return ans
}
================================================
FILE: algorithm/leetcode/463/ac463.go
================================================
package problem463
func islandPerimeter(grid [][]int) int {
isLand, neighbours := 0, 0
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[i]); j++ {
if 1 == grid[i][j] {
isLand++
// 统计下一行与该陆地块相邻的数量
if i < len(grid)-1 && 1 == grid[i+1][j] {
neighbours++
}
// 统计右侧与该陆地块相邻的数量
if j < len(grid[i]) - 1 && 1 == grid[i][j+1] {
neighbours++
}
}
}
}
// and the pattern is islands * 4 - neighbours * 2, since every adjacent islands made two sides disappeared
// 4 + 4 - ? = 6 -> ? = 2
return isLand*4 - neighbours*2
}
================================================
FILE: algorithm/leetcode/463/ac463_test.go
================================================
package problem463
import (
"testing"
"fmt"
)
func Test_islandPerimeter(t *testing.T) {
land := [][]int{
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 1, 0, 0},
{1, 1, 0, 0}}
fmt.Println(islandPerimeter(land))
}
================================================
FILE: algorithm/leetcode/475/ac475.go
================================================
package problem475
import "sort"
func findRadius(houses []int, heaters []int) int {
sort.Ints(houses)
sort.Ints(heaters)
i, ans := 0, 0
for _, house := range houses {
for i < len(heaters)-1 && heaters[i]+heaters[i+1] <= house*2 {
i++
}
ans = maxInt(ans, absInt(heaters[i]-house))
}
return ans
}
func maxInt(a, b int) int {
if a >= b {
return a
}
return b
}
func absInt(a int) int {
if a < 0 {
return -a
}
return a
}
================================================
FILE: algorithm/leetcode/475/ac475_test.go
================================================
package problem475
import (
"testing"
"fmt"
)
func Test_findRadius(t *testing.T) {
fmt.Println(findRadius([]int{1, 2, 3}, []int{2}))
fmt.Println(findRadius([]int{1, 2, 3, 4, 99}, []int{1, 4}))
}
================================================
FILE: algorithm/leetcode/476/ac476.go
================================================
package problem476
func findComplement(num int) int {
temp, mask := num, 0
for temp > 0 {
mask = mask*2 + 1
temp /= 2
}
return mask ^ num
}
================================================
FILE: algorithm/leetcode/476/ac476_test.go
================================================
package problem476
import (
"testing"
"fmt"
)
func Test_findComplement(t *testing.T) {
fmt.Println(findComplement(5))
}
================================================
FILE: algorithm/leetcode/479/ac479.go
================================================
package problem479
import (
"strconv"
"math"
)
func largestPalindrome(n int) int {
if 1 == n {
return 9
}
max := int(math.Pow10(n) - 1)
for v := max - 1; v > max/10; v-- {
u, _ := strconv.Atoi(strconv.Itoa(v) + reverseInt(v))
for x := max; x*x >= u; x-- {
if u%x == 0 {
return u % 1337
}
}
}
return 0
}
func reverseInt(a int) string {
temp := []byte(strconv.Itoa(a))
for i, j := 0, len(temp)-1; i < j; i, j = i+1, j-1 {
temp[i], temp[j] = temp[j], temp[i]
}
return string(temp)
}
================================================
FILE: algorithm/leetcode/479/ac479_test.go
================================================
package problem479
import (
"testing"
"fmt"
)
func Test_largestPalindrome(t *testing.T) {
fmt.Println(largestPalindrome(2))
fmt.Println(largestPalindrome(3))
fmt.Println(largestPalindrome(4))
}
================================================
FILE: algorithm/leetcode/485/ac485.go
================================================
package problem485
func findMaxConsecutiveOnes(nums []int) int {
ans, count := 0, 0
for _, v := range nums {
if 1 == v {
count++
if ans < count {
ans = count
}
} else {
count = 0
}
}
return ans
}
================================================
FILE: algorithm/leetcode/485/ac485_test.go
================================================
package problem485
import (
"testing"
"fmt"
)
func Test_findMaxConsecutiveOnes(t *testing.T) {
nums := []int{1, 1, 0, 1, 1, 1}
fmt.Println(findMaxConsecutiveOnes(nums))
}
================================================
FILE: algorithm/leetcode/492/ac492.go
================================================
package problem492
import "math"
func constructRectangle(area int) []int {
ans := make([]int, 0)
num := int(math.Sqrt(float64(area)))
for num > 0 {
if area%num == 0 {
// L W 需要按顺排列并确保 L >= W
if num >= area/num {
ans = append(ans, num, area/num)
} else {
ans = append(ans, area/num, num)
}
break
} else {
num--
}
}
return ans
}
================================================
FILE: algorithm/leetcode/492/ac492_test.go
================================================
package problem492
import (
"testing"
"fmt"
)
func Test_constructRectangle(t *testing.T) {
fmt.Println(constructRectangle(4))
}
================================================
FILE: algorithm/leetcode/496/ac496.go
================================================
package problem496
func nextGreaterElement(findNums []int, nums []int) []int {
ans := make([]int, 0)
for _, v := range findNums {
ans = append(ans, nextMax(v, nums))
}
return ans
}
func nextMax(n int, nums []int) int {
ans := -1
index := 0
// 找到数字 n 在 nums 切片中的位置
for index < len(nums) && nums[index] != n {
index++
}
for index < len(nums) {
if nums[index] > n {
ans = nums[index]
break
}
index++
}
return ans
}
================================================
FILE: algorithm/leetcode/496/ac496_test.go
================================================
package problem496
import (
"testing"
"fmt"
)
func Test_nextGreaterElement(t *testing.T) {
num1 := []int{4, 1, 2}
num2 := []int{1, 3, 4, 2}
fmt.Println(nextGreaterElement(num1, num2))
}
================================================
FILE: algorithm/leetcode/500/ac500.go
================================================
package problem500
import "strings"
func findWords(words []string) []string {
ans := make([]string, 0)
data := []string{"qwertyuiop", "asdfghjkl", "zxcvbnm"}
for _, v := range words {
word := strings.ToLower(v)
if (strings.ContainsAny(data[0], word) && !strings.ContainsAny(data[1], word) && !strings.ContainsAny(data[2], word)) ||
(!strings.ContainsAny(data[0], word) && strings.ContainsAny(data[1], word) && !strings.ContainsAny(data[2], word)) ||
(!strings.ContainsAny(data[0], word) && !strings.ContainsAny(data[1], word) && strings.ContainsAny(data[2], word)) {
ans = append(ans, v)
}
}
return ans
}
================================================
FILE: algorithm/leetcode/500/ac500_test.go
================================================
package problem500
import (
"testing"
"fmt"
)
func Test_findWords(t *testing.T) {
words := []string{"Hello", "Alaska", "Dad", "Peace"}
fmt.Println(findWords(words))
}
================================================
FILE: algorithm/leetcode/501/ac501.go
================================================
package problem501
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func findMode(root *TreeNode) []int {
ans := make([]int, 0)
if nil == root {
return ans
}
max, count, pre := 1, 0, root.Val
traverse(root, &ans, &max, &count, &pre)
return ans
}
func traverse(root *TreeNode, ans *[]int, max, count, pre *int) {
if nil == root {
return
}
traverse(root.Left, ans, max, count, pre)
if root.Val == *pre {
*count++
if *count == *max {
*ans = append(*ans, root.Val)
} else if *count > *max {
*max = *count
*ans = (*ans)[:0]
*ans = append(*ans, root.Val)
}
} else {
*count = 1
if *count == *max {
*ans = append(*ans, root.Val)
}
}
*pre = root.Val
traverse(root.Right, ans, max, count, pre)
}
================================================
FILE: algorithm/leetcode/501/ac501_test.go
================================================
package problem501
import (
"testing"
"fmt"
)
func Test_findMode(t *testing.T) {
node21 := TreeNode{Val: 2, Left: nil, Right: nil}
node2 := TreeNode{Val: 2, Left: &node21, Right: nil}
node1 := TreeNode{Val: 1, Left: nil, Right: &node2}
fmt.Println(findMode(&node1))
}
================================================
FILE: algorithm/leetcode/504/ac504.go
================================================
package problem504
import (
"strconv"
)
func convertToBase7(num int) string {
if 0 == num {
return "0"
}
ans := ""
neg := false
if num < 0 {
num = -num
neg = true
}
for 0 != num {
ans = strconv.Itoa(num%7) + ans
num /= 7
}
if neg {
ans = "-" + ans
}
return ans
}
================================================
FILE: algorithm/leetcode/504/ac504_test.go
================================================
package problem504
import (
"testing"
"fmt"
)
func Test_convertToBase7(t *testing.T) {
fmt.Println(convertToBase7(7))
fmt.Println(convertToBase7(-7))
fmt.Println(convertToBase7(100))
}
================================================
FILE: algorithm/leetcode/506/ac506.go
================================================
package problem506
import (
"sort"
"strconv"
)
type person struct {
score int
index int
}
type persons []person
// Len方法返回集合中的元素个数
func (p persons) Len() int {
return len(p)
}
// Less方法报告索引i的元素是否比索引j的元素小
func (p persons) Less(i, j int) bool {
// 此处实现从大到小排序 (大的数字在前,小的在后)
return p[i].score > p[j].score
}
// Swap方法交换索引i和j的两个元素
func (p persons) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func findRelativeRanks(nums []int) []string {
ans := make([]string, len(nums))
ps := make(persons, len(nums))
for i, v := range nums {
ps[i].score = v
ps[i].index = i
}
sort.Sort(ps)
for i, v := range ps {
switch i {
case 0:
ans[v.index] = "Gold Medal"
case 1:
ans[v.index] = "Silver Medal"
case 2:
ans[v.index] = "Bronze Medal"
default:
ans[v.index] = strconv.Itoa(i + 1)
}
}
return ans
}
================================================
FILE: algorithm/leetcode/506/ac506_test.go
================================================
package problem506
import (
"testing"
"fmt"
)
func Test_findRelativeRanks(t *testing.T) {
nums := []int{5, 4, 3, 2, 1}
fmt.Println(findRelativeRanks(nums))
}
================================================
FILE: algorithm/leetcode/507/ac507.go
================================================
package problem507
import "math"
func checkPerfectNumber(num int) bool {
if 1 == num {
return false
}
// 1 为该数字的正因子,需要计入
sum := 1
for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
if 0 == num%i {
sum += i + num/i
}
}
return sum == num
}
================================================
FILE: algorithm/leetcode/507/ac507_test.go
================================================
package problem507
import (
"testing"
"fmt"
)
func Test_checkPerfectNumber(t *testing.T) {
fmt.Println(checkPerfectNumber(6))
fmt.Println(checkPerfectNumber(28))
fmt.Println(checkPerfectNumber(30))
}
================================================
FILE: algorithm/leetcode/520/ac520.go
================================================
package problem520
func detectCapitalUse(word string) bool {
if len(word) <= 1 {
return true
}
firstChar := word[0]
secondChar := word[1]
if 'a' <= firstChar && firstChar <= 'z' {
for i := 1; i < len(word); i++ {
if word[i] < 'a' || word[i] > 'z' {
return false
}
}
} else if 'a' <= secondChar && secondChar <= 'z' {
for i := 2; i < len(word); i++ {
if word[i] < 'a' || word[i] > 'z' {
return false
}
}
} else {
for i := 1; i < len(word); i++ {
if word[i] < 'A' || word[i] > 'Z' {
return false
}
}
}
return true
}
// 直接检测单词中大写字母的数量
// 符合规范的有下面几种情况:
// 1. 全部都是大写单词 -> 大写单词数目 = 单词长度
// 2. 没有大写单词 -> 大写单词数目 = 0
// 3. 只有头部是大写单词 -> 大写单词数目 = 1 && 仅头部第一个单词是大写
func detectCapitalUse2(word string) bool {
if len(word) <= 1 {
return true
}
count := 0
for i := 0; i < len(word); i++ {
if word[i] < 'a' {
count++
}
}
if 0 == count || len(word) == count || (1 == count && word[0] < 'a') {
return true
}
return false
}
================================================
FILE: algorithm/leetcode/520/ac520_test.go
================================================
package problem520
import (
"testing"
"fmt"
)
func Test_detectCapitalUse(t *testing.T) {
str1 := "USA"
fmt.Println(detectCapitalUse(str1))
str2 := "Leetcode"
fmt.Println(detectCapitalUse(str2))
str3 := "LEETcode"
fmt.Println(detectCapitalUse(str3))
fmt.Println(detectCapitalUse2(str1))
fmt.Println(detectCapitalUse2(str2))
fmt.Println(detectCapitalUse2(str3))
}
================================================
FILE: algorithm/leetcode/521/ac521.go
================================================
package problem521
func findLUSlength(a string, b string) int {
if a == b {
return -1
}
if len(a) > len(b) {
return len(a)
}
return len(b)
}
================================================
FILE: algorithm/leetcode/530/ac530.go
================================================
package problem530
import "math"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func getMinimumDifference(root *TreeNode) int {
numbers := []int{math.MaxInt32, -1}
if nil == root {
return numbers[0]
}
core(root, numbers)
return numbers[0]
}
func core(root *TreeNode, nums []int) int {
if nil == root {
return nums[0]
}
core(root.Left, nums)
if -1 != nums[1] {
nums[0] = myMin(nums[0], root.Val-nums[1])
}
nums[1] = root.Val
core(root.Right, nums)
return nums[0]
}
func myMin(a, b int) int {
if a < b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/530/ac530_test.go
================================================
package problem530
import (
"testing"
"fmt"
)
func Test_getMinimumDifference(t *testing.T) {
node3 := TreeNode{Val: 3, Left: nil, Right: nil}
node5 := TreeNode{Val: 5, Left: &node3, Right: nil}
node1 := TreeNode{Val: 1, Left: nil, Right: &node5}
fmt.Println(getMinimumDifference(&node1))
}
================================================
FILE: algorithm/leetcode/532/ac532.go
================================================
package problem532
func findPairs(nums []int, k int) int {
if nil == nums || len(nums) == 0 || k < 0 {
return 0
}
dict := make(map[int]int, 0)
for _, v := range nums {
dict[v]++
}
ans := 0
for key, value := range dict {
if 0 == k {
if value >= 2 {
ans++
}
} else {
if _, ok := dict[key+k]; ok {
ans++
}
}
}
return ans
}
================================================
FILE: algorithm/leetcode/532/ac532_test.go
================================================
package problem532
import (
"testing"
"fmt"
)
func Test_findPairs(t *testing.T) {
nums := []int{3, 1, 4, 1, 5}
fmt.Println(findPairs(nums, 2))
}
================================================
FILE: algorithm/leetcode/538/ac538.go
================================================
package problem538
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func convertBST(root *TreeNode) *TreeNode {
if nil == root {
return root
}
sums := 0
convertBSTCore(root, &sums)
return root
}
func convertBSTCore(root *TreeNode, sums *int) {
if nil == root {
return
}
convertBSTCore(root.Right, sums)
root.Val += *sums
*sums = root.Val
convertBSTCore(root.Left, sums)
}
================================================
FILE: algorithm/leetcode/541/ac541.go
================================================
package problem541
func reverseStr(s string, k int) string {
chars := []byte(s)
for idx := 0; idx*k < len(chars); idx += 2 {
start, end := idx*k, idx*k+k-1
if end >= len(chars) {
end = len(chars)-1
}
for start < end {
chars[start], chars[end] = chars[end], chars[start]
start++
end--
}
}
return string(chars)
}
================================================
FILE: algorithm/leetcode/541/ac541_test.go
================================================
package problem541
import (
"testing"
"fmt"
)
func Test_reverseStr(t *testing.T) {
s := "abcdefg"
k := 2
fmt.Println(reverseStr(s, k))
s1 := "a"
k1 := 2
fmt.Println(reverseStr(s1, k1))
}
================================================
FILE: algorithm/leetcode/543/ac543.go
================================================
package problem543
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
// 二叉树的直径解题思路:
// 二叉树中的某个节点的左子树深度 与 该节点的右子树深度 之和为树种最大值,则该最大值即为二叉树的直径。
func diameterOfBinaryTree(root *TreeNode) int {
ans := 0
if nil == root {
return ans
}
left := depthOfTree(root.Left, &ans)
right := depthOfTree(root.Right, &ans)
if left+right > ans {
ans = left + right
}
return ans
}
func depthOfTree(node *TreeNode, diameter *int) int {
if nil == node {
return 0
}
leftDepth := depthOfTree(node.Left, diameter)
rightDepth := depthOfTree(node.Right, diameter)
temp := leftDepth + rightDepth
if temp > *diameter {
*diameter = temp
}
return myMax(leftDepth, rightDepth) + 1
}
func myMax(a, b int) int {
if a >= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/551/ac551.go
================================================
package problem551
import "strings"
func checkRecord(s string) bool {
countA := 0
for i := 0; i < len(s); i++ {
if s[i] == 'A' {
countA++
}
if s[i] == 'L' {
if i+2 < len(s) && s[i+1] == 'L' && s[i+2] == 'L' {
return false
}
}
}
if countA > 1 {
return false
}
return true
}
func checkRecord2(s string) bool {
return strings.IndexByte(s, 'A') == strings.LastIndexByte(s, 'A') && !strings.Contains(s, "LLL")
}
================================================
FILE: algorithm/leetcode/551/ac551_test.go
================================================
package problem551
import (
"testing"
"fmt"
)
func Test_checkRecord(t *testing.T) {
str1 := "PPALLP"
fmt.Println(checkRecord(str1))
str2 := "PPALLL"
fmt.Println(checkRecord(str2))
}
================================================
FILE: algorithm/leetcode/557/ac557.go
================================================
package problem557
func reverseWords(s string) string {
strChar := []rune(s)
start, end := 0, 0
for i := 0; i < len(s); i++ {
if s[i] == ' ' {
end = i - 1
reverse(strChar, start, end)
start = i + 1
}
}
end = len(s)-1
reverse(strChar, start, end)
return string(strChar)
}
func reverse(chars []rune, start, end int) {
for start < end {
chars[start], chars[end] = chars[end], chars[start]
start++
end--
}
}
================================================
FILE: algorithm/leetcode/557/ac557_test.go
================================================
package problem557
import (
"testing"
"fmt"
)
func Test_reverseWords(t *testing.T) {
str := "Let's take LeetCode contest"
fmt.Println(reverseWords(str))
}
================================================
FILE: algorithm/leetcode/561/ac561.go
================================================
package problem561
import "sort"
func arrayPairSum(nums []int) int {
ans := 0
sort.Ints(nums)
for i, value := range nums {
if 0 == i%2 {
ans += value
}
}
return ans
}
func arrayPairSum2(nums []int) int {
//给数组排序,然后挨着的两个为一组,也就是取第1,3,....到2n-3,2n-1的和(从1开始计数)
dict := make([]int, 20001)
//考虑负数
for _, v := range nums {
dict[v+10000]++
}
id, count := 0, 0
for k, v := range dict {
//考虑有相同数据
for v > 0 {
id++
if 1 == id%2 {
count += k - 10000
}
v--
}
}
return count
}
================================================
FILE: algorithm/leetcode/561/ac561_test.go
================================================
package problem561
import (
"testing"
"fmt"
)
func Test_arrayPairSum(t *testing.T) {
nums := []int{1, 4, 3, 2}
fmt.Println(arrayPairSum(nums))
}
================================================
FILE: algorithm/leetcode/563/ac563.go
================================================
package problem563
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func findTilt(root *TreeNode) int {
ret := make([]int, 1)
helper(root, ret)
return ret[0]
}
func helper(root *TreeNode, ret []int) int {
if nil == root {
return 0
}
lSum := helper(root.Left, ret)
rSum := helper(root.Right, ret)
ret[0] += myAbs(lSum, rSum)
return lSum + rSum + root.Val
}
func myAbs(a, b int) int {
if a < b {
return b - a
}
return a - b
}
================================================
FILE: algorithm/leetcode/566/ac566.go
================================================
package problem566
func matrixReshape(nums [][]int, r int, c int) [][]int {
if nil == nums || 0 == len(nums) || len(nums)*len(nums[0]) != r*c {
return nums
}
temp := make([]int, 0)
for row := 0; row < len(nums); row++ {
for col := 0; col < len(nums[0]); col++ {
temp = append(temp, nums[row][col])
}
}
ans := make([][]int, r)
for row := 0; row < r; row++ {
tmp := make([]int, c)
for col := 0; col < c; col++ {
tmp[col] = temp[row*c+col]
}
ans[row] = tmp
}
return ans
}
func matrixReshape2(nums [][]int, r int, c int) [][]int {
if nil == nums || 0 == len(nums) || len(nums)*len(nums[0]) != r*c {
return nums
}
originCol := len(nums[0])
ans := make([][]int, r)
for row := 0; row < r; row++ {
temp := make([]int, c)
for col := 0; col < c; col++ {
idx := row*c + col
currentRow := idx / originCol
currentCol := idx % originCol
temp[col] = nums[currentRow][currentCol]
}
ans[row] = temp
}
return ans
}
================================================
FILE: algorithm/leetcode/566/ac566_test.go
================================================
package problem566
import (
"testing"
"fmt"
)
func Test_matrixReshape(t *testing.T) {
nums := [][]int{
{1, 2},
{3, 4}}
fmt.Println(matrixReshape(nums, 1, 4))
fmt.Println(matrixReshape(nums, 2, 4))
fmt.Println(matrixReshape2(nums, 1, 4))
fmt.Println(matrixReshape2(nums, 2, 4))
}
================================================
FILE: algorithm/leetcode/572/ac572.go
================================================
package problem572
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isSubtree(s *TreeNode, t *TreeNode) bool {
if nil == s || nil == t {
return s == t || nil == t
}
return isSame(s, t) || isSubtree(s.Left, t) || isSubtree(s.Right, t)
}
// 从该节点起的 2 个子树是否完全相同
func isSame(t1, t2 *TreeNode) bool {
if nil == t1 || nil == t2 {
return t1 == t2
}
return t1.Val == t2.Val && isSame(t1.Left, t2.Left) && isSame(t1.Right, t2.Right)
}
================================================
FILE: algorithm/leetcode/575/ac575.go
================================================
package problem575
func distributeCandies(candies []int) int {
mySet := make(map[int]int, 0)
for _, v := range candies {
mySet[v]++
}
if len(mySet) >= len(candies)/2 {
return len(candies) / 2
}
return len(mySet)
}
================================================
FILE: algorithm/leetcode/575/ac575_test.go
================================================
package problem575
import (
"testing"
"fmt"
)
func Test_distributeCandies(t *testing.T) {
nums := []int{1, 1, 2, 2, 3, 3}
fmt.Println(distributeCandies(nums))
nums2 := []int{1, 1, 2, 3}
fmt.Println(distributeCandies(nums2))
}
================================================
FILE: algorithm/leetcode/581/ac581.go
================================================
package problem581
import "sort"
func findUnsortedSubarray(nums []int) int {
if nil == nums || len(nums) == 0 {
return 0
}
numsBackup := make([]int, len(nums))
copy(numsBackup, nums)
sort.Ints(nums)
left, right := 0, len(nums)-1
for ; left < len(nums) && nums[left] == numsBackup[left]; {
left++
}
// 该数组是有序数组
if left == len(nums) {
return 0
}
for ; right >= 0 && nums[right] == numsBackup[right]; {
right--
}
return right - left + 1
}
func findUnsortedSubarray2(nums []int) int {
if nil == nums || len(nums) == 0 {
return 0
}
n := len(nums)
begin, end := -1, -2
min, max := nums[n-1], nums[0]
for i := 1; i < n; i++ {
max = myMax(max, nums[i])
min = myMin(min, nums[n-1-i])
if nums[i] < max {
end = i
}
if nums[n-1-i] > min {
begin = n - 1 - i
}
}
return end - begin + 1
}
func myMax(a, b int) int {
if a >= b {
return a
}
return b
}
func myMin(a, b int) int {
if a <= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/581/ac581_test.go
================================================
package problem581
import (
"testing"
"fmt"
)
func Test_findUnsortedSubarray(t *testing.T) {
num := []int{2, 6, 4, 8, 10, 9, 15}
num2 := []int{1, 2, 3, 4}
fmt.Println(findUnsortedSubarray(num))
fmt.Println(findUnsortedSubarray(num2))
}
================================================
FILE: algorithm/leetcode/594/ac594.go
================================================
package problem594
// 和谐数组为 原数组的子数组即可,不需要是连续的子数组...
func findLHS(nums []int) int {
dict := make(map[int]int, 0)
for _, v := range nums {
dict[v]++
}
ans := 0
for k, v := range dict {
if _, ok := dict[k+1]; ok {
ans = myMax(ans, v+dict[k+1])
}
}
return ans
}
func myMax(a, b int) int {
if a >= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/594/ac594_test.go
================================================
package problem594
import (
"fmt"
"testing"
)
func Test_findLHS(t *testing.T) {
nums := []int{1, 3, 2, 2, 5, 2, 3, 7}
fmt.Println(findLHS(nums))
nums2 := []int{2, 2, 2, 9, 3, 3, 3}
fmt.Println(findLHS(nums2))
}
================================================
FILE: algorithm/leetcode/598/ac598.go
================================================
package problem598
func maxCount(m int, n int, ops [][]int) int {
if nil == ops || len(ops) == 0 {
return m * n
}
row, col := ops[0][0], ops[0][1]
for _, v := range ops {
row = myMin(row, v[0])
col = myMin(col, v[1])
}
return row * col
}
func myMin(a, b int) int {
if a <= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/598/ac598_test.go
================================================
package problem598
import (
"testing"
"fmt"
)
func Test_maxCount(t *testing.T) {
ope := [][]int{
{2, 2},
{3, 3}}
fmt.Println(maxCount(3, 3, ope))
}
================================================
FILE: algorithm/leetcode/599/ac599.go
================================================
package problem599
func findRestaurant(list1 []string, list2 []string) []string {
dict1 := make(map[string]int, len(list1))
dict2 := make(map[string]int, len(list2))
for id, v := range list1 {
dict1[v] = id + 1
}
for id, v := range list2 {
dict2[v] = id + 1
}
ans := make([]string, 0)
min := 2001
for k, v := range dict1 {
if v2, ok := dict2[k]; ok && v+v2 <= min {
if v+v2 < min {
min = v + v2
ans = ans[:0]
}
ans = append(ans, k)
}
}
return ans
}
================================================
FILE: algorithm/leetcode/599/ac599_test.go
================================================
package problem599
import (
"testing"
"fmt"
)
func Test_findRestaurant(t *testing.T) {
list1 := []string{"Shogun", "Tapioca Express", "Burger King", "KFC"}
list2 := []string{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}
fmt.Println(findRestaurant(list1, list2))
}
================================================
FILE: algorithm/leetcode/605/ac605.go
================================================
package problem605
func canPlaceFlowers(flowerbed []int, n int) bool {
count := 0
for i := 0; i < len(flowerbed); i++ {
if 0 == flowerbed[i] {
prev, next := 0, 0
if i == len(flowerbed)-1 {
next = 0
} else {
next = flowerbed[i+1]
}
if 0 == i {
prev = 0
} else {
prev = flowerbed[i-1]
}
if 0 == prev && next == 0 {
flowerbed[i] = 1
count++
}
}
}
// 花圃能够继续种植的花卉数目大于给的花卉数目,则成立
return count >= n
}
================================================
FILE: algorithm/leetcode/605/ac605_test.go
================================================
package problem605
import (
"testing"
"fmt"
)
func Test_canPlaceFlowers(t *testing.T) {
flowerbed := []int{1, 0, 0, 0, 1}
fmt.Println(canPlaceFlowers(flowerbed, 1))
fmt.Println(canPlaceFlowers(flowerbed, 2))
}
================================================
FILE: algorithm/leetcode/606/ac606.go
================================================
package problem606
import "strconv"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func tree2str(t *TreeNode) string {
if nil == t {
return ""
}
ans := strconv.Itoa(t.Val)
strLeft := tree2str(t.Left)
strRight := tree2str(t.Right)
if "" == strLeft && "" == strRight {
return ans
}
if "" == strLeft {
return ans + "()" + "(" + strRight + ")"
}
if "" == strRight {
return ans + "(" + strLeft + ")"
}
return ans + "(" + strLeft + ")" + "(" + strRight + ")"
}
================================================
FILE: algorithm/leetcode/617/ac617.go
================================================
package problem617
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
if nil == t1 {
return t2
}
if nil == t2 {
return t1
}
return &TreeNode{Val: t1.Val + t2.Val, Left: mergeTrees(t1.Left, t2.Left), Right: mergeTrees(t1.Right, t2.Right)}
}
================================================
FILE: algorithm/leetcode/628/ac628.go
================================================
package problem628
import (
"sort"
"math"
)
func maximumProduct(nums []int) int {
if len(nums) < 3 {
return 0
}
sort.Ints(nums)
ans1, ans2 := 1, 1
for i := len(nums) - 1; i >= len(nums)-3; i-- {
ans1 *= nums[i]
}
for i := 0; i < 2; i++ {
ans2 *= nums[i]
}
ans2 *= nums[len(nums)-1]
if ans1 > ans2 {
return ans1
}
return ans2
}
func maximumProduct2(nums []int) int {
if len(nums) < 3 {
return 0
}
max1, max2, max3 := math.MinInt32, math.MinInt32, math.MinInt32
min1, min2 := math.MaxInt32, math.MaxInt32
for _, v := range nums {
if v > max1 {
max3 = max2
max2 = max1
max1 = v
} else if v > max2 {
max3 = max2
max2 = v
} else if v > max3 {
max3 = v
}
if v < min1 {
min2 = min1
min1 = v
} else if v < min2 {
min2 = v
}
}
return myMax(max1*max2*max3, min1*min2*max1)
}
func myMax(a, b int) int {
if a >= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/633/ac633.go
================================================
package problem633
import "math"
func judgeSquareSum(c int) bool {
if c < 0 {
return false
}
left, right := 0, int(math.Sqrt(float64(c)))
for left <= right {
current := left*left + right*right
if current < c {
left++
} else if current > c {
right--
} else {
return true
}
}
return false
}
================================================
FILE: algorithm/leetcode/637/ac637.go
================================================
package problem637
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func averageOfLevels(root *TreeNode) []float64 {
if nil == root {
return nil
}
ans := make([]float64, 0)
// 模拟队列
queue := make([]*TreeNode, 0)
queue = append(queue, root)
for len(queue) != 0 {
n := len(queue)
sum := 0.0
for i := 0; i < n; i++ {
node := queue[0]
sum += float64(node.Val)
if nil != node.Left {
queue = append(queue, node.Left)
}
if nil != node.Right {
queue = append(queue, node.Right)
}
// 从队列中删除头部元素,相当于头部元素出队
queue = queue[1:]
}
ans = append(ans, sum/float64(n))
}
return ans
}
================================================
FILE: algorithm/leetcode/643/ac643.go
================================================
package problem643
import "math"
func findMaxAverage(nums []int, k int) float64 {
if nil == nums || len(nums) == 0 {
return 0
}
maxSum := math.MinInt32
for i := 0; i <= len(nums)-k; i++ {
temp := 0
for j := 0; j < k; j++ {
temp += nums[i+j]
}
if maxSum < temp {
maxSum = temp
}
}
return float64(maxSum) / float64(k)
}
================================================
FILE: algorithm/leetcode/643/ac643_test.go
================================================
package problem643
import (
"testing"
"fmt"
)
func Test_findMaxAverage(t *testing.T) {
nums := []int{1, 12, -5, -6, 50, 3}
fmt.Println(findMaxAverage(nums, 4))
nums2 := []int{-1}
fmt.Println(findMaxAverage(nums2, 1))
}
================================================
FILE: algorithm/leetcode/645/ac645.go
================================================
package problem645
func findErrorNums(nums []int) []int {
if nil == nums || len(nums) < 2 {
return nil
}
ans := make([]int, 0)
for _, v := range nums {
if 0 > nums[myAbs(v)-1] {
// 找到重复的数字
ans = append(ans, myAbs(v))
} else {
nums[myAbs(v)-1] *= -1
}
}
for i := 0; i < len(nums); i++ {
// 找到缺失元素的位置
if nums[i] > 0 {
ans = append(ans, i+1)
}
}
return ans
}
func myAbs(a int) int {
if a < 0 {
return -a
}
return a
}
================================================
FILE: algorithm/leetcode/645/ac645_test.go
================================================
package problem645
import (
"testing"
"fmt"
)
func Test_findErrorNums(t *testing.T) {
nums := []int{4, 2, 1, 2}
fmt.Println(findErrorNums(nums))
}
================================================
FILE: algorithm/leetcode/653/ac653.go
================================================
package problem653
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func findTarget(root *TreeNode, k int) bool {
if nil == root {
return false
}
dict := make(map[int]bool, 0)
return dfs(root, k, dict)
}
func dfs(node *TreeNode, k int, dict map[int]bool) bool {
if nil == node {
return false
}
if dict[k-node.Val] {
return true
}
dict[node.Val] = true
return dfs(node.Left, k, dict) || dfs(node.Right, k, dict)
}
================================================
FILE: algorithm/leetcode/653/ac653_test.go
================================================
package problem653
import (
"testing"
"fmt"
)
func Test_findTarget(t *testing.T) {
node2 := TreeNode{Val: 2, Left: nil, Right: nil}
node4 := TreeNode{Val: 4, Left: nil, Right: nil}
node7 := TreeNode{Val: 7, Left: nil, Right: nil}
node3 := TreeNode{Val: 3, Left: &node2, Right: &node4}
node6 := TreeNode{Val: 2, Left: nil, Right: &node7}
node5 := TreeNode{Val: 5, Left: &node3, Right: &node6}
fmt.Println(findTarget(&node5, 9))
}
================================================
FILE: algorithm/leetcode/657/ac657.go
================================================
package problem657
import "strings"
type Position struct {
x, y int
}
func judgeCircle(moves string) bool {
pst := Position{x: 0, y: 0}
for _, v := range moves {
if 'R' == v {
pst.x++
} else if 'L' == v {
pst.x--
} else if 'U' == v {
pst.y++
} else if 'D' == v {
pst.y--
} else {
continue
}
}
if 0 == pst.x && pst.x == pst.y {
return true
}
return false
}
func judgeCircle2(moves string) bool {
r := strings.Count(moves, "R")
l := strings.Count(moves, "L")
u := strings.Count(moves, "U")
d := strings.Count(moves, "D")
if l == r && u == d {
return true
}
return false
}
================================================
FILE: algorithm/leetcode/657/ac657_test.go
================================================
package problem657
import (
"testing"
"fmt"
)
func Test_judgeCircle(t *testing.T) {
fmt.Println(judgeCircle("UD"))
fmt.Println(judgeCircle("LL"))
fmt.Println(judgeCircle2("UD"))
fmt.Println(judgeCircle2("LL"))
}
================================================
FILE: algorithm/leetcode/661/ac661.go
================================================
package problem661
func imageSmoother(M [][]int) [][]int {
if nil == M || len(M) <= 0 || len(M[0]) <= 0 {
return nil
}
// 行数、列数
nx, ny := len(M), len(M[0])
ans := make([][]int, nx)
for i := 0; i < nx; i++ {
ans[i] = make([]int, ny)
for j := 0; j < ny; j++ {
ans[i][j] = smooth(M, i, j)
}
}
return ans
}
func smooth(M [][]int, x, y int) int {
nx, ny := len(M), len(M[0])
sum, count := 0, 0
// i: -1 -> 上一行, 0 -> 本行, 1 -> 下一行
// j: -1 -> 左侧列, 0 -> 本列, 1 -> 右侧列
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if x+i < 0 || x+i >= nx || y+j < 0 || y+j >= ny {
continue
}
count++
sum += M[x+i][y+j]
}
}
return sum / count
}
================================================
FILE: algorithm/leetcode/661/ac661_test.go
================================================
package problem661
import (
"testing"
"fmt"
)
func Test_imageSmoother(t *testing.T) {
M := [][]int{
{0, 1, 2, 3},
{2, 3, 1, 1},
}
fmt.Println(imageSmoother(M))
}
================================================
FILE: algorithm/leetcode/665/ac665.go
================================================
package problem665
func checkPossibility(nums []int) bool {
if nil == nums || len(nums) <= 0 {
return false
}
if len(nums) == 1 {
return true
}
count := 0
for i := 1; i < len(nums) && count <= 1; i++ {
if nums[i-1] > nums[i] {
count++
if i-2 < 0 || nums[i-2] <= nums[i] {
nums[i-1] = nums[i]
} else {
nums[i] = nums[i-1]
}
}
}
return count <= 1
}
================================================
FILE: algorithm/leetcode/665/ac665_test.go
================================================
package problem665
import (
"testing"
"fmt"
)
func Test_checkPossibility(t *testing.T) {
nums := []int{4, 2, 1}
fmt.Println(checkPossibility(nums))
nums2 := []int{4, 2, 3}
fmt.Println(checkPossibility(nums2))
nums3 := []int{3, 4, 2, 3}
fmt.Println(checkPossibility(nums3))
}
================================================
FILE: algorithm/leetcode/669/ac669.go
================================================
package problem669
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func trimBST(root *TreeNode, L int, R int) *TreeNode {
if nil == root {
return nil
}
if root.Val < L {
return trimBST(root.Right, L, R)
}
if root.Val > R {
return trimBST(root.Left, L, R)
}
root.Left = trimBST(root.Left, L, R)
root.Right = trimBST(root.Right, L, R)
return root
}
================================================
FILE: algorithm/leetcode/671/ac671.go
================================================
package problem671
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func findSecondMinimumValue(root *TreeNode) int {
if nil == root {
return -1
}
return findCore(root, root.Val)
}
func findCore(node *TreeNode, min int) int {
if nil == node {
return -1
}
if node.Val > min {
return node.Val
}
leftMin := findCore(node.Left, min)
rightMin := findCore(node.Right, min)
if -1 == leftMin || -1 == rightMin {
return myMax(leftMin, rightMin)
} else {
return myMin(leftMin, rightMin)
}
}
func myMin(a, b int) int {
if a <= b {
return a
}
return b
}
func myMax(a, b int) int {
if a >= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/671/ac671_test.go
================================================
package problem671
import (
"testing"
"fmt"
)
func Test_findSecondMinimumValue(t *testing.T) {
node35 := TreeNode{Val: 5}
node37 := TreeNode{Val: 7}
node22 := TreeNode{Val: 2}
node25 := TreeNode{Val: 5, Left: &node35, Right: &node37}
node12 := TreeNode{Val: 2, Left: &node22, Right: &node25}
fmt.Println(findSecondMinimumValue(&node12))
}
================================================
FILE: algorithm/leetcode/674/ac674.go
================================================
package problem674
func findLengthOfLCIS(nums []int) int {
if nil == nums {
return 0
}
ans, cnt := 0, 0
for i := 0; i < len(nums); i++ {
if 0 == i || nums[i-1] < nums[i] {
cnt++
if cnt > ans {
ans = cnt
}
} else {
cnt = 1
}
}
return ans
}
================================================
FILE: algorithm/leetcode/674/ac674_test.go
================================================
package problem674
import (
"testing"
"fmt"
)
func Test_findLengthOfLCIS(t *testing.T) {
nums := []int{1, 3, 5, 4, 7}
fmt.Println(findLengthOfLCIS(nums))
}
================================================
FILE: algorithm/leetcode/680/ac680.go
================================================
package problem680
func validPalindrome(s string) bool {
for l, r := 0, len(s)-1; l < r; {
if s[l] != s[r] {
return isPalindromic(s, l+1, r) || isPalindromic(s, l, r-1)
} else {
l++
r--
}
}
return true
}
func isPalindromic(s string, l, r int) bool {
for l < r {
if s[l] != s[r] {
return false
} else {
l++
r--
}
}
return true
}
================================================
FILE: algorithm/leetcode/682/ac682.go
================================================
package problem682
import "strconv"
func calPoints(ops []string) int {
if nil == ops {
return 0
}
sum := 0
stack := make([]int, 0)
for _, op := range ops {
switch op {
case "C":
sum -= stack[len(stack)-1]
stack = stack[:len(stack)-1]
case "D":
val := stack[len(stack)-1] * 2
sum += val
stack = append(stack, val)
case "+":
val := stack[len(stack)-1] + stack[len(stack)-2]
sum += val
stack = append(stack, val)
default:
val, _ := strconv.Atoi(op)
stack = append(stack, val)
sum += val
}
}
return sum
}
================================================
FILE: algorithm/leetcode/682/ac682_test.go
================================================
package problem682
import (
"testing"
"fmt"
)
func Test_calPoints(t *testing.T) {
ops := []string{"5", "-2", "4", "C", "D", "9", "+", "+"}
fmt.Println(calPoints(ops))
}
================================================
FILE: algorithm/leetcode/686/ac686.go
================================================
package problem686
import "strings"
func repeatedStringMatch(A string, B string) int {
if strings.Contains(A, B) {
return 1
}
tempA := A
for i := 0; len(tempA) <= 10000; i++ {
tempA += A
if strings.Contains(tempA, B) {
return i + 2
}
}
return -1
}
func repeatedStringMatch2(A string, B string) int {
count := 0
var strb string
for len(strb) < len(B) {
strb += A
count++
}
if strings.Contains(strb, B) {
return count
}
if strb += A; strings.Contains(strb, B) {
count++
return count
}
return -1
}
================================================
FILE: algorithm/leetcode/686/ac686_test.go
================================================
package problem686
import (
"testing"
"fmt"
)
func Test_repeatedStringMatch(t *testing.T) {
a := "abababaaba"
b := "aabaaba"
fmt.Println(repeatedStringMatch(a, b))
aa := "bb"
bb := "bbbbbbb"
fmt.Println(repeatedStringMatch(aa, bb))
fmt.Println(repeatedStringMatch2(a, b))
fmt.Println(repeatedStringMatch2(aa, bb))
}
================================================
FILE: algorithm/leetcode/687/ac687.go
================================================
package problem687
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func longestUnivaluePath(root *TreeNode) int {
ans := make([]int, 1)
if nil != root {
dfs(root, ans)
}
return ans[0]
}
func dfs(node *TreeNode, res []int) int {
l := 0
if nil != node.Left {
l = dfs(node.Left, res)
}
r := 0
if nil != node.Right {
r = dfs(node.Right, res)
}
resl := 0
if nil != node.Left && node.Left.Val == node.Val {
resl = l + 1
}
resr := 0
if nil != node.Right && node.Right.Val == node.Val {
resr = r + 1
}
// 当前节点的左右子树连接,成为最长同值路径
if res[0] < resl+resr {
res[0] = resl + resr
}
// 最长同值路径仅出现在当前节点的左子树 或者 右子树
if resl >= resr {
return resl
} else {
return resr
}
}
================================================
FILE: algorithm/leetcode/693/ac693.go
================================================
package problem693
import (
"strings"
"fmt"
)
func hasAlternatingBits(n int) bool {
res := decimalToBinary(n)
for i, temp := 1, res[0]; i < len(res); i++ {
if temp == res[i] {
return false
} else {
temp = res[i]
}
}
return true
}
func decimalToBinary(n int) []int {
if 0 == n {
return []int{0}
}
ans := make([]int, 0)
for 0 != n {
ans = append(ans, n%2)
n /= 2
}
return ans
}
func hasAlternatingBits2(n int) bool {
strN := fmt.Sprintf("%b", n)
return !(strings.Contains(strN, "00") || strings.Contains(strN, "11"))
}
================================================
FILE: algorithm/leetcode/693/ac693_test.go
================================================
package problem693
import (
"testing"
"fmt"
)
func Test_hasAlternatingBits2(t *testing.T) {
fmt.Println(hasAlternatingBits2(5))
fmt.Println(hasAlternatingBits2(7))
}
================================================
FILE: algorithm/leetcode/695/ac695.go
================================================
package problem695
func maxAreaOfIsland(grid [][]int) int {
maxArea := 0
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if 1 == grid[i][j] {
current := AreaOfIsland(grid, i, j)
if maxArea < current {
maxArea = current
}
}
}
}
return maxArea
}
func AreaOfIsland(grid [][]int, i, j int) int {
if 0 <= i && i < len(grid) && 0 <= j && j < len(grid[0]) && 1 == grid[i][j] {
// 当前元素置为 0 ,避免重复计算
grid[i][j] = 0
return 1 + AreaOfIsland(grid, i-1, j) + AreaOfIsland(grid, i+1, j) + AreaOfIsland(grid, i, j-1) + AreaOfIsland(grid, i, j+1)
}
return 0
}
================================================
FILE: algorithm/leetcode/696/ac696.go
================================================
package problem696
//preRun count the same item happend before (let say you have 0011, preRun = 2 when you hit the first 1, means there are two zeros before first '1')
//curRun count the current number of items (let say you have 0011, curRun = 2 when you hit the second 1, means there are two 1s so far)
//Whenever item change (from 0 to 1 or from 1 to 0), preRun change to curRun, reset curRun to 1 (store the curRun number into PreRun, reset curRun)
//Every time preRun >= curRun means there are more 0s before 1s, so could do count++ .
// (This was the tricky one, ex. 0011 when you hit the first '1', curRun = 1, preRun = 2, means 0s number is larger than 1s number,
// so we could form "01" at this time, count++ . When you hit the second '1', curRun = 2, preRun = 2, means 0s' number equals to 1s' number,
// so we could form "0011" at this time, that is why count++)
func countBinarySubstrings(s string) int {
prevRun, curRun, ans := 0, 1, 0
for i := 1; i < len(s); i++ {
if s[i] == s[i-1] {
curRun++
} else {
prevRun = curRun
curRun = 1
}
if prevRun >= curRun {
ans++
}
}
return ans
}
================================================
FILE: algorithm/leetcode/697/ac697.go
================================================
package problem697
func findShortestSubArray(nums []int) int {
size := len(nums)
if 2 > size {
return size
}
first := make(map[int]int, size)
count := make(map[int]int, size)
maxCount := 1
minLen := size
for i, v := range nums {
count[v]++
if 1 == count[v] {
first[v] = i
} else {
l := i - first[v] + 1
if maxCount < count[v] || (maxCount == count[v] && l < minLen) {
maxCount = count[v]
minLen = l
}
}
}
if len(count) == size {
return 1
}
return minLen
}
================================================
FILE: algorithm/leetcode/717/ac717.go
================================================
package problem717
//if there is only one symbol in array the answer is always true (as last element is 0)
//if there are two 0s at the end again the answer is true no matter what the rest symbols are( ...1100, ...1000,)
//if there is 1 right before the last element(...10), the outcome depends on the count of sequential 1, i.e.
//a) if there is odd amount of 1(10, ...01110, etc) the answer is false as there is a single 1 without pair
//b) if it's even (110, ...011110, etc) the answer is true, as 0 at the end doesn't have anything to pair with
func isOneBitCharacter(bits []int) bool {
ones := 0
for i := len(bits) - 2; i >= 0 && 1 == bits[i]; i-- {
ones++
}
if ones%2 != 0 {
return false
}
return true
}
================================================
FILE: algorithm/leetcode/724/ac724.go
================================================
package problem724
func pivotIndex(nums []int) int {
if nil == nums || len(nums) == 0 {
return -1
}
preSum := make([]int, len(nums))
preSum[0] = nums[0]
for i := 1; i < len(nums); i++ {
preSum[i] = preSum[i-1] + nums[i]
}
for i := 0; i < len(nums); i++ {
// 左边和 == 右边和,则返回当前的下标
if preSum[i]-nums[i] == preSum[len(nums)-1]-preSum[i] {
return i
}
}
return -1
}
================================================
FILE: algorithm/leetcode/728/ac728.go
================================================
package problem728
func selfDividingNumbers(left int, right int) []int {
ans := make([]int, 0)
for i := left; i <= right; i++ {
value := i
for ; value > 0; value /= 10 {
if 0 == value%10 || 0 != (i%(value%10)) {
break
}
}
if 0 == value {
ans = append(ans, i)
}
}
return ans
}
================================================
FILE: algorithm/leetcode/744/ac744.go
================================================
package problem744
func nextGreatestLetter(letters []byte, target byte) byte {
idx := 0
for i := 0; i < len(letters); i++ {
if letters[i] > target {
idx = i
break
}
}
return letters[idx]
}
================================================
FILE: algorithm/leetcode/746/ac746.go
================================================
package problem746
func minCostClimbingStairs(cost []int) int {
for i := len(cost) - 3; i >= 0; i-- {
cost[i] += myMin(cost[i+1], cost[i+2])
}
return myMin(cost[0], cost[1])
}
func myMin(a, b int) int {
if a <= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/747/ac747.go
================================================
package problem747
func dominantIndex(nums []int) int {
if len(nums) < 1 {
return -1
}
if len(nums) == 1 {
return 0
}
id1, id2 := -1, -1
for i := 0; i < len(nums); i++ {
if -1 == id1 || nums[i] > nums[id1] {
id2 = id1
id1 = i
} else if -1 == id2 || nums[i] > nums[id2] {
id2 = i
}
}
if nums[id2]*2 <= nums[id1] {
return id1
}
return -1
}
================================================
FILE: algorithm/leetcode/747/ac747_test.go
================================================
package problem747
import (
"testing"
"fmt"
)
func Test_dominantIndex(t *testing.T) {
nums := []int{0, 0, 1, 2}
fmt.Println(dominantIndex(nums))
}
================================================
FILE: algorithm/leetcode/762/ac762.go
================================================
package problem762
func countPrimeSetBits(L, R int) int {
primes := make(map[int]bool, 0)
primes[2] = true
primes[3] = true
primes[5] = true
primes[7] = true
primes[11] = true
primes[13] = true
primes[17] = true
primes[19] = true
cnt := 0
for i := L; i <= R; i++ {
bits := 0
for n := i; n > 0; n /= 2 {
bits += n & 1
}
if primes[bits] {
cnt++
}
}
return cnt
}
================================================
FILE: algorithm/leetcode/766/ac766.go
================================================
package problem766
// 无需考虑复杂,直接判断是否符合要求即可
func isToeplitzMatrix(matrix [][]int) bool {
for i := 0; i < len(matrix)-1; i++ {
for j := 0; j < len(matrix[0])-1; j++ {
if matrix[i][j] != matrix[i+1][j+1] {
return false
}
}
}
return true
}
================================================
FILE: algorithm/leetcode/766/ac766_test.go
================================================
package problem766
import (
"testing"
"fmt"
)
func Test_isToeplitzMatrix(t *testing.T) {
data := [][]int{{1, 2, 3, 4}, {5, 1, 2, 3}, {9, 5, 1, 2}}
fmt.Println(isToeplitzMatrix(data))
}
================================================
FILE: algorithm/leetcode/771/ac771.go
================================================
package problem771
func numJewelsInStones(J string, S string) int {
ans := 0
dict := make(map[uint8]bool, len(J))
for i := 0; i < len(J); i++ {
dict[J[i]] = true
}
for i := 0; i < len(S); i++ {
if dict[S[i]] {
ans++
}
}
return ans
}
================================================
FILE: algorithm/leetcode/771/ac771_test.go
================================================
package problem771
import (
"testing"
"fmt"
)
func Test_numJewelsInStones(t *testing.T) {
j1 := "aA"
s1 := "aAAbbbb"
fmt.Println(numJewelsInStones(j1, s1))
j2 := "z"
s2 := "ZZ"
fmt.Println(numJewelsInStones(j2, s2))
}
================================================
FILE: algorithm/leetcode/783/ac783.go
================================================
package problem783
import "math"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
// 因为给定的是二叉搜索树,故可以采用中序遍历 data[1]代表pre
func minDiffInBST(root *TreeNode) int {
data := make([]int, 2)
data[0] = math.MaxInt32
data[1] = -1
check(root, data)
return data[0]
}
func check(node *TreeNode, nums []int) {
if nil == node {
return
}
check(node.Left, nums)
if -1 != nums[1] {
nums[0] = myMin(nums[0], node.Val-nums[1])
}
nums[1] = node.Val
check(node.Right, nums)
}
func myMin(a, b int) int {
if a <= b {
return a
}
return b
}
================================================
FILE: algorithm/leetcode/783/ac783_test.go
================================================
package problem783
import (
"testing"
"fmt"
)
func Test_minDiffInBST(t *testing.T) {
node44 := TreeNode{Val: 44}
node50 := TreeNode{Val: 50, Left: &node44}
node58 := TreeNode{Val: 58, Left: &node50}
node34 := TreeNode{Val: 34, Right: &node58}
node27 := TreeNode{Val: 27, Right: &node34}
fmt.Println(minDiffInBST(&node27))
}
================================================
FILE: algorithm/leetcode/784/ac784.go
================================================
package problem784
// 递归解法
// 每当遇到一个字符时,分别统计 1.该字符为小写时的所有情况 2.该字符为大写时的所有情况
func letterCasePermutation(S string) []string {
ans := make([]string, 0)
if len(S) < 1 {
return []string{""}
}
return core(ans, []byte(S), 0)
}
func core(ans []string, chars []byte, index int) []string {
if index == len(chars) {
ans = append(ans, string(chars))
} else {
if isLetter(chars[index]) {
chars[index] = toLower(chars[index])
ans = core(ans, chars, index+1)
chars[index] = toUpper(chars[index])
}
ans = core(ans, chars, index+1)
}
return ans
}
func isLetter(c byte) bool {
if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
return true
}
return false
}
func toLower(c byte) byte {
if c >= 'A' && c <= 'Z' {
dis := byte('a' - 'A')
return c + dis
}
return c
}
func toUpper(c byte) byte {
if c >= 'a' && c <= 'z' {
dis := byte('a' - 'A')
return c - dis
}
return c
}
================================================
FILE: algorithm/leetcode/784/ac784_test.go
================================================
package problem784
import (
"testing"
"fmt"
)
func Test_letterCasePermutation(t *testing.T) {
s := "a1b2"
fmt.Println(letterCasePermutation(s))
s1 := ""
fmt.Println(letterCasePermutation(s1))
}
================================================
FILE: algorithm/leetcode/788/ac788.go
================================================
package problem788
func rotatedDigits(N int) int {
count := 0
for i := 1; i <= N; i++ {
if isValid(i) {
count++
}
}
return count
}
func isValid(n int) bool {
ans := false
for 0 < n {
if 2 == n%10 {
ans = true
}
if 5 == n%10 {
ans = true
}
if 6 == n%10 {
ans = true
}
if 9 == n%10 {
ans = true
}
if 3 == n%10 {
return false
}
if 4 == n%10 {
return false
}
if 7 == n%10 {
return false
}
n /= 10
}
return ans
}
================================================
FILE: algorithm/leetcode/796/ac796.go
================================================
package problem706
import "strings"
// 拼接2个字符串,正好满足了循环条件
func rotateString(A string, B string) bool {
if len(A) != len(B){
return false
}
return strings.Contains(A+A, B)
}
================================================
FILE: algorithm/leetcode/804/ac804.go
================================================
package problem804
func uniqueMorseRepresentations(words []string) int {
d := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
ans := make(map[string]int, 0)
for _, word := range words {
ecd := ""
for i := 0; i < len(word); i++ {
ecd += d[word[i]-'a']
}
ans[ecd]++
}
return len(ans)
}
================================================
FILE: algorithm/leetcode/804/ac804_test.go
================================================
package problem804
import (
"testing"
"fmt"
)
func Test_uniqueMorseRepresentations(t *testing.T) {
strList := []string{"abc", "bcd"}
fmt.Println(uniqueMorseRepresentations(strList))
}
================================================
FILE: algorithm/leetcode/811/ac811.go
================================================
package problem811
import (
"strings"
"strconv"
)
func subdomainVisits(cpdomains []string) []string {
ans := make(map[string]int, 0)
for _, item := range cpdomains {
parts := strings.Split(item, " ")
domain := parts[1]
count, _ := strconv.Atoi(parts[0])
ans[domain] += count
idx := strings.LastIndex(domain, ".")
for i := 0; i <= idx; i++ {
if domain[i] == '.' {
temp := domain[i+1:]
ans[temp] += count
}
}
}
ansList := make([]string, 0)
for k, v := range ans {
ansList = append(ansList, strconv.Itoa(v)+" "+k)
}
return ansList
}
================================================
FILE: algorithm/leetcode/811/ac811_test.go
================================================
package problem811
import (
"testing"
"fmt"
)
func Test_subdomainVisits(t *testing.T) {
test1 := []string{"9001 discuss.leetcode.com"}
fmt.Println(subdomainVisits(test1))
test2 := []string{"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"}
fmt.Println(subdomainVisits(test2))
}
================================================
FILE: algorithm/leetcode/812/ac812.go
================================================
package problem812
import "math"
// 从坐标计算三角形的面积公式为:
// area = |xa*yb + xb*yc + xc*ya - xa*yc - xb*ya - xc*yb| / 2
func largestTriangleArea(points [][]int) float64 {
ans := 0.0
for _, i := range points {
for _, j := range points {
for _, k := range points {
ans = math.Max(ans, 0.5*math.Abs(float64(i[0]*j[1]+j[0]*k[1]+k[0]*i[1]-i[0]*k[1]-j[0]*i[1]-k[0]*j[1])))
}
}
}
return ans
}
================================================
FILE: algorithm/leetcode/812/ac812_test.go
================================================
package problem812
import (
"testing"
"fmt"
)
func Test_largestTriangleArea(t *testing.T) {
nums := [][]int{{0, 0}, {0, 1}, {1, 0}, {0, 2}, {2, 0}}
fmt.Println(largestTriangleArea(nums))
}
================================================
FILE: algorithm/leetcode/819/ac819.go
================================================
package problem819
import (
"strings"
"regexp"
)
func mostCommonWord(paragraph string, banned []string) string {
text := strings.ToLower(paragraph)
reg := regexp.MustCompile("[a-z]+")
result := reg.FindAllString(text, -1)
ans := make(map[string]int, 0)
for _, word := range result {
if !isContain(banned, word) {
ans[word]++
}
}
maxTime := 0
var ansString string
for k, v := range ans {
if v > maxTime {
maxTime = v
ansString = k
}
}
return ansString
}
func isContain(words []string, word string) bool {
for _, w := range words {
if w == word {
return true
}
}
return false
}
================================================
FILE: algorithm/leetcode/819/ac819_test.go
================================================
package problem819
import (
"testing"
"fmt"
)
func Test_mostCommonWord(t *testing.T) {
para := "Bob hit a ball, the hit BALL flew far after it was hit."
banned := []string{"hit"}
fmt.Println(mostCommonWord(para, banned))
}
================================================
FILE: algorithm/leetcode/821/ac821.go
================================================
package problem821
import "math"
func shortestToChar(S string, C byte) []int {
// 先获得 C 的各个坐标
cId := make([]int, 0)
for i := 0; i < len(S); i++ {
if S[i] == C {
cId = append(cId, i)
}
}
ans := make([]int, 0)
for i := 0; i < len(S); i++ {
if S[i] == C {
ans = append(ans, 0)
} else {
minDistance := getMinDistance(cId, i)
ans = append(ans, minDistance)
}
}
return ans
}
func getMinDistance(position []int, p int) int {
d := math.MaxInt32
for _, item := range position {
temp := myAbs(p - item)
if temp < d {
d = temp
}
}
return d
}
func myAbs(a int) int {
if a < 0 {
return -a
}
return a
}
================================================
FILE: algorithm/leetcode/821/ac821_test.go
================================================
package problem821
import (
"testing"
"fmt"
)
func Test_shortestToChar(t *testing.T) {
S := "loveleetcode"
C := byte('e')
fmt.Println(shortestToChar(S, C))
}
================================================
FILE: algorithm/leetcode/824/ac824.go
================================================
package problem824
import (
"strings"
"bytes"
)
func toGoatLatin(S string) string {
words := strings.Fields(S)
var buffer bytes.Buffer
vowels := map[string]int{
"a": 1,
"e": 1,
"i": 1,
"o": 1,
"u": 1,
"A": 1,
"E": 1,
"I": 1,
"O": 1,
"U": 1,
}
for i := 0; i < len(words); i++ {
first := string(words[i][0])
if vowels[first] == 1 {
buffer.WriteString(words[i])
buffer.WriteString("ma")
} else {
buffer.WriteString(string(words[i][1:]))
buffer.WriteString(first)
buffer.WriteString("ma")
}
for j := 0; j <= i; j++ {
buffer.WriteString("a")
}
if i != len(words)-1 {
buffer.WriteString(" ")
}
}
return buffer.String()
}
================================================
FILE: algorithm/leetcode/824/ac824_test.go
================================================
package problem824
import (
"testing"
"fmt"
)
func Test_toGoatLatin(t *testing.T) {
S := "I speak Goat Latin"
fmt.Println(toGoatLatin(S))
}
================================================
FILE: algorithm/leetcode/829/ac829.go
================================================
package problem829
// 任何一个连续的序列可以写作-> (base+1,base+2,...,base+m)
// 对于本题,我们需要检查 (1+2+...+m)+ m*base==N 是否成立
// 即检查 sum=(1+2+...+m) N-sum % m == 0
func consecutiveNumbersSum(N int) int {
count := 0
sum := 1
m := 1
for sum <= N {
if 0 == (N-sum)%m {
count++
}
m++
sum = (1 + m) * m / 2
}
return count
}
================================================
FILE: algorithm/leetcode/829/ac829_test.go
================================================
package problem829
import (
"testing"
"fmt"
)
func Test_consecutiveNumbersSum(t *testing.T) {
fmt.Println(consecutiveNumbersSum(1))
fmt.Println(consecutiveNumbersSum(9))
fmt.Println(consecutiveNumbersSum(15))
}
================================================
FILE: algorithm/leetcode/830/ac830.go
================================================
package problem830
func largeGroupPositions(S string) [][]int {
if len(S) < 3 {
return [][]int{}
}
ans := make([][]int, 0)
start, count := 0, 0
for i := 0; i < len(S); i++ {
if S[i] == S[start] {
count++
} else {
if 3 <= count {
ans = append(ans, []int{start, i - 1})
}
start = i
count = 1
}
}
// large group 出现在字符串末尾位置
if 3 <= count && S[start] == S[len(S)-1] {
ans = append(ans, []int{start, len(S) - 1})
}
return ans
}
================================================
FILE: algorithm/leetcode/830/ac830_test.go
================================================
package problem830
import (
"testing"
"fmt"
)
func Test_largeGroupPositions(t *testing.T) {
str1 := "abbxxxxzzy"
fmt.Println(largeGroupPositions(str1))
str2 := "abc"
fmt.Println(largeGroupPositions(str2))
str3 := "abcdddeeeeaabbbcd"
fmt.Println(largeGroupPositions(str3))
str4 := "aaa"
fmt.Println(largeGroupPositions(str4))
}
================================================
FILE: algorithm/leetcode/832/ac832.go
================================================
package problem832
func flipAndInvertImage(A [][]int) [][]int {
for i := 0; i < len(A); i++ {
reverse(A[i])
invert(A[i])
}
return A
}
func reverse(array []int) {
for left, right := 0, len(array)-1; left < right; {
array[left], array[right] = array[right], array[left]
left++
right--
}
}
func invert(array []int) {
for i := 0; i < len(array); i++ {
if 1 == array[i] {
array[i] = 0
} else if 0 == array[i] {
array[i] = 1
}
}
}
================================================
FILE: algorithm/leetcode/836/ac836.go
================================================
package problem836
// 两个矩形 A B 有重叠部分,当且仅当
// A.bottom < B.top && A.left < B.right and B.bottom < A.top && B.left < A.right
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
if rec1[1] < rec2[3] && rec1[0] < rec2[2] && rec2[1] < rec1[3] && rec2[0] < rec1[2] {
return true
}
return false
}
================================================
FILE: algorithm/leetcode/840/ac840.go
================================================
package problem840
func numMagicSquaresInside(grid [][]int) int {
if len(grid) < 3 { return 0 }
res := 0
for i:=1; i<len(grid)-1; i++ {
for j:=1; j<len(grid[0])-1; j++ {
if grid[i][j] == 5 {
is_ok := true
m := make([]bool, 9)
m[4] = true
for _, offset := range [][]int{[]int{-1, -1}, []int{-1, 0},
[]int{-1, 1}, []int{0, -1}} {
v1,v2 := grid[i+offset[0]][j+offset[1]], grid[i-offset[0]][j-offset[1]]
if v1<1 || v1>9 || v2<1 || v2>9 ||
v1+v2 != 10 || m[v1-1] == true || m[v2-1] == true {
is_ok = false
break
}
m[v1-1], m[v2-1] = true, true
}
if is_ok == true { res++ }
}
}
}
return res
}
================================================
FILE: algorithm/leetcode/844/ac844.go
================================================
package problem844
func backspaceCompare(S string, T string) bool {
newS := core(S)
newT := core(T)
if newS == newT {
return true
}
return false
}
func core(s string) string {
ans := make([]byte, 0)
for i := 0; i < len(s); i++ {
if '#' == s[i] {
if 0 < len(ans) {
ans = ans[0 : len(ans)-1]
}
} else {
ans = append(ans, s[i])
}
}
return string(ans)
}
================================================
FILE: algorithm/sfo/30/ac30.go
================================================
package problem30
import (
"github.com/emirpasic/gods/stacks/arraystack"
"errors"
)
var (
dataStack = arraystack.New()
minStack = arraystack.New()
)
func push(number int) {
dataStack.Push(number)
if 0 == minStack.Size() {
minStack.Push(number)
} else {
topNumber, _ := minStack.Peek()
topN := topNumber.(int)
if number < topN {
minStack.Push(number)
} else {
minStack.Push(topN)
}
}
}
func pop() {
if 0 < dataStack.Size() && 0 < minStack.Size() {
dataStack.Pop()
minStack.Pop()
}
}
func min() (int, error) {
if 0 < dataStack.Size() && 0 < minStack.Size() {
topNumber, _ := minStack.Pop()
topN := topNumber.(int)
return topN, nil
}
return 0, errors.New("栈为空")
}
================================================
FILE: algorithm/sfo/30/ac30_test.go
================================================
package problem30
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestOk(t *testing.T) {
ast := assert.New(t)
push(3)
push(4)
push(2)
push(6)
push(5)
pop()
minNumber, ok := min()
if nil != ok {
ast.Error(ok)
}
ast.Equal(2, minNumber, "栈中最小数")
}
================================================
FILE: algorithm/sfo/31/ac31.go
================================================
package problem31
import "github.com/emirpasic/gods/stacks/arraystack"
func isPopOrder(pushStack, popStack []int) bool {
length := len(pushStack)
if 0 == length || length != len(popStack) {
return false
}
stack := arraystack.New()
nextPush, nextPop := 0, 0
for nextPop < length {
for topNumber, _ := stack.Peek(); nil == topNumber || topNumber != popStack[nextPop]; {
if nextPush == length {
break
}
stack.Push(pushStack[nextPush])
nextPush++
topNumber, _ = stack.Peek()
}
if topNumber, _ := stack.Peek(); topNumber != popStack[nextPop] {
break
}
stack.Pop()
nextPop++
}
if stack.Empty() && nextPop == length {
return true
}
return false
}
func Ma(){
return
}
================================================
FILE: algorithm/sfo/31/ac31_test.go
================================================
package problem31
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_isPopOrder(t *testing.T) {
type argument struct {
pushStack []int
popStack []int
}
type answer struct {
value bool
}
type example struct {
arg argument
ans answer
}
examples := []example{
{
arg: argument{
pushStack: []int{1, 2, 3, 4, 5},
popStack: []int{4, 5, 3, 2, 1},
},
ans: answer{
value: true,
},
}, {
arg: argument{
pushStack: []int{1, 2, 3, 4, 5},
popStack: []int{4, 3, 5, 1, 2},
},
ans: answer{
value: false,
},
},
}
ast := assert.New(t)
for _, exam := range examples {
ast.Equal(exam.ans.value, isPopOrder(exam.arg.pushStack, exam.arg.popStack),
"压入栈:%v 弹出栈:%v", exam.arg.pushStack, exam.arg.popStack)
}
}
================================================
FILE: algorithm/sfo/32/ac32.go
================================================
package problem32
import (
"gopkg.in/oleiade/lane.v1"
"fmt"
)
type binaryTreeNode struct {
value int
left *binaryTreeNode
right *binaryTreeNode
}
func breadthFirstSearch(root *binaryTreeNode) {
if nil == root {
return
}
l := list.New()
l.PushBack(root)
for l.Len() != 0 {
e := l.Front()
l.Remove(e)
t := e.Value.(*binaryTreeNode)
fmt.Print(t.value, " ")
if t.left != nil {
l.PushBack(t.left)
}
if t.right != nil {
l.PushBack(t.right)
}
}
}
// 分行打印二叉树
func breadthFirstSearch2(root *binaryTreeNode) {
if nil == root {
return
}
l := list.New()
l.PushBack(root)
toBePrint := 1
nextLevel := 0
for l.Len() != 0 {
e := l.Front()
l.Remove(e)
t := e.Value.(*binaryTreeNode)
fmt.Printf("%d ",t.value)
toBePrint--
if t.left != nil {
l.PushBack(t.left)
nextLevel++
}
if t.right != nil {
l.PushBack(t.right)
nextLevel++
}
if toBePrint == 0 {
fmt.Printf("\n")
toBePrint = nextLevel
nextLevel = 0
}
}
}
================================================
FILE: algorithm/sfo/32/ac32_test.go
================================================
package problem32
import "testing"
func Test_breadthFirstSearch(t *testing.T) {
var node6 = binaryTreeNode{value: 11, left: nil, right: nil}
var node5 = binaryTreeNode{value: 9, left: nil, right: nil}
var node4 = binaryTreeNode{value: 7, left: nil, right: nil}
var node3 = binaryTreeNode{value: 5, left: nil, right: nil}
var node2 = binaryTreeNode{value: 10, left: &node5, right: &node6}
var node1 = binaryTreeNode{value: 6, left: &node3, right: &node4}
var node0 = binaryTreeNode{value: 8, left: &node1, right: &node2}
breadthFirstSearch(&node0)
breadthFirstSearch2(&node0)
}
================================================
FILE: algorithm/sfo/33/ac33.go
================================================
package problem33
// begin 开始字符的序号, end 结尾字符的序号
func verifySquenceOfBST(sequence []int, begin, end int) bool {
if nil == sequence || begin >= end {
return false
}
root := sequence[end]
// 二叉搜索树中,左子树节点的值小于根节点的值
i := begin
for ; i < end; i++ {
if sequence[i] > root {
break
}
}
// 二叉搜索树中,右子树节点的值大于根节点的值
j := i
for ; j < end; j++ {
if sequence[j] < root {
return false
}
}
// 判断左子树是不是二叉搜索树
left := true
if 1 < i-begin {
left = verifySquenceOfBST(sequence, begin, i-1)
}
// 判断右子树是不是二叉搜索树
right := true
if 1 < end-i {
right = verifySquenceOfBST(sequence, i, end-1)
}
return left && right
}
================================================
FILE: algorithm/sfo/33/ac33_test.go
================================================
package problem33
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_verifySquenceOfBST(t *testing.T) {
ast := assert.New(t)
ast.Equal(true, verifySquenceOfBST([]int{5, 7, 6, 9, 11, 10, 8}, 0, 6),
"是二叉搜索树的后序遍历序列")
ast.Equal(false, verifySquenceOfBST([]int{7, 4, 6, 5}, 0, 3),
"不是二叉搜索树的后序遍历序列")
}
================================================
FILE: algorithm/sfo/34/ac34.go
================================================
package problem34
import (
"github.com/emirpasic/gods/stacks/arraystack"
"fmt"
)
type binaryTreeNode struct {
value int
left *binaryTreeNode
right *binaryTreeNode
}
func findPath(root *binaryTreeNode, expectedSum int) {
if nil == root {
return
}
stack := arraystack.New()
currentSum := 0
findPathCore(root, stack, expectedSum, currentSum)
}
func findPathCore(root *binaryTreeNode, stack *arraystack.Stack, expectedSum, currentSum int) {
currentSum += root.value
stack.Push(root.value)
// 如果是叶节点,并且路径节点值的和等于输入的期望值则打印路径
isLeaf := root.left == nil && root.right == nil
if isLeaf && currentSum == expectedSum {
it := stack.Iterator()
for it.End(); it.Prev(); {
fmt.Print(it.Value(), " ")
}
fmt.Printf("\n")
}
// 如果不是叶节点,则遍历它的子节点
if root.left != nil {
findPathCore(root.left, stack, expectedSum, currentSum)
}
if root.right != nil {
findPathCore(root.right, stack, expectedSum, currentSum)
}
stack.Pop()
}
================================================
FILE: algorithm/sfo/34/ac34_test.go
================================================
package problem34
import (
"testing"
)
func Test_findPath(t *testing.T) {
node4 := binaryTreeNode{value: 4, left: nil, right: nil}
node7 := binaryTreeNode{value: 7, left: nil, right: nil}
node5 := binaryTreeNode{value: 5, left: &node4, right: &node7}
node12 := binaryTreeNode{value: 12, left: nil, right: nil}
node10 := binaryTreeNode{value: 10, left: &node5, right: &node12}
findPath(&node10, 22)
}
================================================
FILE: algorithm/sfo/35/ac35.go
================================================
package problem35
type complexListNode struct {
value int
next *complexListNode
sibling *complexListNode
}
func cloneNodes(head *complexListNode) {
pNode := head
for nil != pNode {
pCloned := new(complexListNode)
pCloned.value = pNode.value
pCloned.next = pNode.next
pNode.next = pCloned
pNode = pCloned.next
}
}
func connectSiblingNodes(head *complexListNode) {
pNode := head
for nil != pNode {
pCloned := pNode.next
if nil != pNode.sibling {
pCloned.sibling = pNode.sibling.next
}
pNode = pCloned.next
}
}
func reconnectNodes(head *complexListNode) *complexListNode {
pNode := head
var pClonedHead, pClonedNode *complexListNode
if nil != pNode {
pClonedHead = pNode.next
pClonedNode = pClonedHead
pNode.next = pClonedNode.next
pNode = pNode.next
}
for nil != pNode {
pClonedNode.next = pNode.next
pClonedNode = pClonedNode.next
pNode.next = pClonedNode.next
pNode = pNode.next
}
return pClonedHead
}
func clone(pHead *complexListNode) *complexListNode {
cloneNodes(pHead)
connectSiblingNodes(pHead)
return reconnectNodes(pHead)
}
================================================
FILE: algorithm/sfo/36/ac36.go
================================================
package problem36
type binaryTreeNode struct {
value int
left, right *binaryTreeNode
}
func conver(root *binaryTreeNode) *binaryTreeNode {
var lastNodeInList *binaryTreeNode
convertNode(root, &lastNodeInList)
// lastNodeInList指向双向链表的尾节点,但是需要返回链表的头结点,所以进行下面操作。
headOfList := lastNodeInList
for nil != headOfList && nil != headOfList.left {
headOfList = headOfList.left
}
return headOfList
}
func convertNode(node *binaryTreeNode, lastNodeInList **binaryTreeNode) {
if nil == node {
return
}
current := node
if nil != current.left {
convertNode(current.left, lastNodeInList)
}
current.left = *lastNodeInList
if nil != *lastNodeInList {
(*lastNodeInList).right = current
}
*lastNodeInList = current
if nil != current.right {
convertNode(current.right, lastNodeInList)
}
}
================================================
FILE: algorithm/sfo/36/ac36_test.go
================================================
package problem36
import (
"testing"
"fmt"
)
func Test_conver(t *testing.T) {
node16 := binaryTreeNode{value: 16, left: nil, right: nil}
node12 := binaryTreeNode{value: 12, left: nil, right: nil}
node8 := binaryTreeNode{value: 8, left: nil, right: nil}
node4 := binaryTreeNode{value: 4, left: nil, right: nil}
node14 := binaryTreeNode{value: 14, left: &node12, right: &node16}
node6 := binaryTreeNode{value: 6, left: &node4, right: &node8}
node10 := binaryTreeNode{value: 10, left: &node6, right: &node14}
ptr := conver(&node10)
fmt.Println(ptr.value)
}
================================================
FILE: algorithm/sfo/38/ac38.go
================================================
package problem38
import "fmt"
func permutation(arrayChar []byte, start int) {
if 1 >= len(arrayChar) {
return
}
if start == len(arrayChar)-1 {
for i := 0; i < len(arrayChar); i++ {
fmt.Printf("%c", arrayChar[i])
}
fmt.Println()
} else {
for i := start; i < len(arrayChar); i++ {
swap(arrayChar, start, i)
permutation(arrayChar, start+1)
swap(arrayChar, start, i)
}
}
}
func swap(array []byte, m, n int) {
temp := array[m]
array[m] = array[n]
array[n] = temp
}
================================================
FILE: algorithm/sfo/38/ac38_test.go
================================================
package problem38
import "testing"
func Test_permutation(t *testing.T) {
arrayChar := []byte("abc")
permutation(arrayChar, 0)
}
================================================
FILE: algorithm/sfo/39/ac39.go
================================================
package problem39
func moreThanHalfNum(numbers []int) int {
if nil == numbers || 0 >= len(numbers) {
return 0
}
ans := numbers[0]
times := 1
for i := 1; i < len(numbers); i++ {
if 0 == times {
ans = numbers[i]
times = 1
} else if ans == numbers[i] {
times++
} else {
times--
}
}
return ans
}
================================================
FILE: algorithm/sfo/39/ac39_test.go
================================================
package problem39
import (
"testing"
"fmt"
)
func Test_moreThanHalfNum(t *testing.T) {
numbers := []int{0, 0, 0, 1, 1, 1, 2, 3, 1, 1, 1}
fmt.Println(moreThanHalfNum(numbers))
}
================================================
FILE: algorithm/sfo/40/ac40.go
================================================
package problem40
import "sort"
func getLeastNumbers(data []int, k int) []int {
if nil == data || k > len(data) {
return nil
}
leastNumbers := make([]int, k)
for i := 0; i < k; i++ {
leastNumbers[i] = data[i]
}
sort.Ints(leastNumbers)
for i:=k; i<len(data); i++ {
if data[i] < leastNumbers[k-1] {
leastNumbers[k-1] = data[i]
sort.Ints(leastNumbers)
}
}
return leastNumbers
}
================================================
FILE: algorithm/sfo/40/ac40_test.go
================================================
package problem40
import (
"testing"
"fmt"
)
func Test_getLeastNumbers(t *testing.T) {
numbers := []int{10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
ans := getLeastNumbers(numbers, 3)
for _, value := range ans {
fmt.Println(value)
}
}
================================================
FILE: algorithm/sfo/42/ac42.go
================================================
package problem42
func findGreatestSumOfSubArray(data []int) (int, bool) {
if nil == data || len(data) <= 0 {
return 0, false
}
var currentSum, greatestSum int
for i := 0; i < len(data); i++ {
if currentSum <= 0 {
currentSum = data[i]
} else {
currentSum += data[i]
}
if currentSum > greatestSum {
greatestSum = currentSum
}
}
return greatestSum, true
}
================================================
FILE: algorithm/sfo/42/ac42_test.go
================================================
package problem42
import (
"testing"
"fmt"
)
func Test_findGreatestSumOfSubArray(t *testing.T) {
data := []int{1, -2, 3, 10, -4, 7, 2, -5}
fmt.Println(findGreatestSumOfSubArray(data))
}
================================================
FILE: algorithm/sfo/43/ac43.go
================================================
package problem43
// 1~n 整数中 1 出现的次数 http://blog.csdn.net/yi_afly/article/details/52012593
func numberOf1Between1AndN(n int) int {
ans := 0
for i := 1; i <= n; i *= 10 {
a := n / i
b := n % i
if 0 == a%10 {
ans += i * a / 10
} else if 1 == a%10 {
ans += i*a/10 + b + 1
} else {
ans += (a/10 + 1) * i
}
}
return ans
}
================================================
FILE: algorithm/sfo/43/ac43_test.go
================================================
package problem43
import (
"testing"
"fmt"
)
func Test_numberOf1Between1AndN(t *testing.T) {
fmt.Println(numberOf1Between1AndN(12))
}
================================================
FILE: algorithm/sfo/44/ac44.go
================================================
package problem44
import "math"
func countOfIntegers(digits int) int {
if 1 == digits {
return 10
}
return int(9 * math.Pow10(digits-1))
}
func beginNumber(digits int) int {
if 1 == digits {
return 0
}
return int(math.Pow10(digits - 1))
}
func digitAtIndexCore(index, digits int) int {
number := beginNumber(digits) + index/digits
indexFromRight := digits - index%digits
for i := 1; i < indexFromRight; i++ {
number /= 10
}
return number % 10
}
func digitAtIndex(index int) int {
if 0 > index {
return -1
}
digit := 1
for {
numbers := countOfIntegers(digit)
if index < numbers*digit {
return digitAtIndexCore(index, digit)
}
index -= digit * numbers
digit++
}
return -1
}
================================================
FILE: algorithm/sfo/44/ac44_test.go
================================================
package problem44
import (
"testing"
"fmt"
)
func Test_digitAtIndex(t *testing.T) {
fmt.Println(digitAtIndex(19))
}
================================================
FILE: algorithm/sfo/45/ac45.go
================================================
package problem45
import (
"sort"
"strconv"
)
type intSlice []int
func (p intSlice) Len() int {
return len(p)
}
func (p intSlice) Less(i, j int) bool {
return strconv.Itoa(p[i])+strconv.Itoa(p[j]) < strconv.Itoa(p[j])+strconv.Itoa(p[i])
}
func (p intSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func printMinNumber(numbers []int) string {
if nil == numbers {
return ""
}
sort.Sort(intSlice(numbers))
var ans string
for _, value := range numbers {
ans += strconv.Itoa(value)
}
return ans
}
================================================
FILE: algorithm/sfo/45/ac45_test.go
================================================
package problem45
import (
"testing"
"fmt"
)
func Test_printMinNumber(t *testing.T) {
numbers := []int{3, 32, 321}
fmt.Println(printMinNumber(numbers))
}
================================================
FILE: algorithm/sfo/46/ac46.go
================================================
package problem46
import "strconv"
func getTranslation(number int) int {
if 0 > number {
return 0
}
return getTranslationCore(strconv.Itoa(number))
}
func getTranslationCore(str string) int {
length := len(str)
counts := make([]int, length)
num := 0
for i := length - 1; i >= 0; i-- {
num = 0
if i < length-1 {
num = counts[i+1]
} else {
num = 1
}
if i < length-1 {
digit1 := str[i] - '0'
digit2 := str[i+1] - '0'
number := digit1*10 + digit2
if 10 <= number && 25 >= number {
if i < length-2 {
num += counts[i+2]
} else {
num += 1
}
}
}
counts[i] = num
}
return counts[0]
}
================================================
FILE: algorithm/sfo/46/ac46_test.go
================================================
package problem46
import (
"testing"
"fmt"
)
func Test_getTranslation(t *testing.T) {
fmt.Println(getTranslation(12258))
}
================================================
FILE: algorithm/sfo/47/ac47.go
================================================
package problem47
func getMaxValue(matrix [][]int) int {
if nil == matrix {
return 0
}
rows := len(matrix)
columns := len(matrix[0])
dp := make([][]int, rows)
for i := 0; i < rows; i++ {
dp[i] = make([]int, columns)
for j := 0; j < columns; j++ {
if 0 == i && 0 == j {
dp[i][j] = matrix[i][j]
}
if 0 == i && 0 != j {
dp[i][j] = dp[i][j-1] + matrix[i][j]
}
if 0 != i && 0 == j {
dp[i][j] = dp[i-1][j] + matrix[i][j]
}
if 0 != i && 0 != j {
if dp[i-1][j] >= dp[i][j-1] {
dp[i][j] = dp[i-1][j] + matrix[i][j]
} else {
dp[i][j] = dp[i][j-1] + matrix[i][j]
}
}
}
}
return dp[rows-1][columns-1]
}
func getMaxValue2(matrix [][]int) int {
if nil == matrix {
return 0
}
rows := len(matrix)
columns := len(matrix[0])
dp := make([]int, columns)
for i := 0; i < rows; i++ {
for j := 0; j < columns; j++ {
left, up := 0, 0
if 0 < i {
up = dp[j]
}
if 0 < j {
left = dp[j-1]
}
if left >= up {
dp[j] = left + matrix[i][j]
} else {
dp[j] = up + matrix[i][j]
}
}
}
return dp[columns-1]
}
================================================
FILE: algorithm/sfo/47/ac47_test.go
================================================
package problem47
import (
"testing"
"fmt"
)
func Test_getMaxValue(t *testing.T) {
matrix := [][]int{
{1, 3, 3},
{2, 1, 3},
{2, 2, 1},
}
fmt.Println(getMaxValue(matrix))
fmt.Println(getMaxValue2(matrix))
}
================================================
FILE: algorithm/sfo/48/ac48.go
================================================
package problem48
func longestSubstring(str string) int {
curLength, maxLength := 0, 0
position := make([]int, 26)
for i := 0; i < 26; i++ {
position[i] = -1
}
for i := 0; i < len(str); i++ {
preIndex := position[str[i]-'a']
if 0 > preIndex || i-preIndex > curLength {
curLength++
} else {
if curLength > maxLength {
maxLength = curLength
}
curLength = i - preIndex
}
position[str[i]-'a'] = i
if curLength > maxLength {
maxLength = curLength
}
}
return maxLength
}
================================================
FILE: algorithm/sfo/48/ac48_test.go
================================================
package problem48
import (
"testing"
"fmt"
)
func Test_longestSubstring(t *testing.T) {
fmt.Println(longestSubstring("arabcacfr"))
}
================================================
FILE: algorithm/sfo/49/ac49.go
================================================
package problem49
func getUglyNumber(index int) int {
if 0 >= index {
return 0
}
uglyNumbers := make([]int, index)
uglyNumbers[0] = 1
nextUglyIndex := 1
var pMultiply2, pMultiply3, pMultiply5 int
for nextUglyIndex < index {
uglyNumbers[nextUglyIndex] = min3(uglyNumbers[pMultiply2]*2, uglyNumbers[pMultiply3]*3, uglyNumbers[pMultiply5]*5)
for uglyNumbers[pMultiply2]*2 <= uglyNumbers[nextUglyIndex] {
pMultiply2++
}
for uglyNumbers[pMultiply3]*3 <= uglyNumbers[nextUglyIndex] {
pMultiply3++
}
for uglyNumbers[pMultiply5]*5 <= uglyNumbers[nextUglyIndex] {
pMultiply5++
}
nextUglyIndex++
}
return uglyNumbers[index-1]
}
func min3(num1, num2, num3 int) int {
number := min(num1, num2)
return min(num3, number)
}
func min(num1, num2 int) int {
if num1 < num2 {
return num1
}
return num2
}
================================================
FILE: algorithm/sfo/49/ac49_test.go
================================================
package problem49
import (
"testing"
"fmt"
)
func Test_getUglyNumber(t *testing.T) {
fmt.Println(getUglyNumber(1500))
}
================================================
FILE: algorithm/sfo/50/ac50.go
================================================
package problem50
func firstNoRepeatingChar(str string) byte {
if 0 >= len(str) {
return 0
}
table := make([]int, 256)
for i := 0; i < len(str); i++ {
table[str[i]]++
}
for id, value := range table {
if 1 == value {
return byte(id)
}
}
return 0
}
================================================
FILE: algorithm/sfo/50/ac50_test.go
================================================
package problem50
import (
"testing"
"fmt"
)
func Test_firstNoRepeatingChar(t *testing.T) {
str := "abaccdeff"
ans := firstNoRepeatingChar(str)
fmt.Printf("%c %v", ans, ans)
}
================================================
FILE: algorithm/sfo/51/ac51.go
================================================
package problem51
func inversePairs(data []int) int {
if nil == data || 0 > len(data) {
return 0
}
copy := make([]int, len(data))
for id, value := range data {
copy[id] = value
}
count := inversePairsCore(data, copy, 0, len(data)-1)
return count
}
func inversePairsCore(data, copy []int, start, end int) int {
if start == end {
copy[start] = data[start]
return 0
}
length := (end - start) / 2
left := inversePairsCore(copy, data, start, start+length)
right := inversePairsCore(copy, data, start+length+1, end)
i := start + length
j := end
indexCopy := end
count := 0
for i >= start && j >= start+length+1 {
if data[i] > data[j] {
copy[indexCopy] = data[i]
indexCopy--
i--
count += j - start - length
} else {
copy[indexCopy] = data[j]
indexCopy--
j--
}
}
for ; i >= start; i-- {
copy[indexCopy] = data[i]
indexCopy--
}
for ; j >= start+length+1; j-- {
copy[indexCopy] = data[j]
indexCopy--
}
return left + right + count
}
================================================
FILE: algorithm/sfo/51/ac51_test.go
================================================
package problem51
import (
"testing"
"fmt"
)
func Test_inversePairs(t *testing.T) {
data := []int{7, 5, 6, 4}
fmt.Println(inversePairs(data))
}
================================================
FILE: algorithm/sfo/52/ac52.go
================================================
package problem52
type ListNode struct {
value int
next *ListNode
}
func findFirstCommonNode(head1, head2 *ListNode) *ListNode {
length1 := getListLength(head1)
length2 := getListLength(head2)
var lengthDif int
var listHeadLong, listHeadShort, firstCommonNode *ListNode
if length1 >= length2 {
lengthDif = length1 - length2
listHeadLong = head1
listHeadShort = head2
} else {
lengthDif = length2 - length1
listHeadLong = head2
listHeadShort = head1
}
for i := 0; i < lengthDif; i++ {
listHeadLong = listHeadLong.next
}
for listHeadLong != listHeadShort && nil != listHeadLong && nil != listHeadShort {
listHeadLong = listHeadLong.next
listHeadShort = listHeadShort.next
}
firstCommonNode = listHeadLong
return firstCommonNode
}
func getListLength(head *ListNode) int {
var length int
node := head
for nil != node {
length++
node = node.next
}
return length
}
================================================
FILE: algorithm/sfo/52/ac52_test.go
================================================
package problem52
import (
"testing"
"fmt"
)
func Test_findFirstCommonNode(t *testing.T) {
node7 := ListNode{value: 7, next: nil}
node6 := ListNode{value: 6, next: &node7}
node5 := ListNode{value: 5, next: &node6}
node4 := ListNode{value: 4, next: &node5}
node3 := ListNode{value: 3, next: &node6}
node2 := ListNode{value: 2, next: &node3}
node1 := ListNode{value: 1, next: &node2}
fmt.Println(findFirstCommonNode(&node1, &node4).value)
}
================================================
FILE: algorithm/sfo/53/ac53.go
================================================
package problem53
func getFirstK(numbers []int, k, start, end int) int {
if start > end {
return -1
}
middleIndex := (start + end) / 2
middleData := numbers[middleIndex]
if middleData == k {
if (0 < middleIndex && numbers[middleIndex-1] != k) || 0 == middleIndex {
return middleIndex
} else {
end = middleIndex - 1
}
} else if middleData > k {
end = middleIndex - 1
} else {
start = middleIndex + 1
}
return getFirstK(numbers, k, start, end)
}
func getLastK(numbers []int, k, start, end int) int {
if start > end {
return -1
}
middleIndex := (start + end) / 2
middleData := numbers[middleIndex]
if middleData == k {
if (len(numbers)-1 > middleIndex && numbers[middleIndex+1] != k) || len(numbers)-1 == middleIndex {
return middleIndex
} else {
start = middleIndex + 1
}
} else if middleData < k {
start = middleIndex + 1
} else {
end = middleIndex - 1
}
return getLastK(numbers, k, start, end)
}
func getNumberOfK(numbers []int, k int) int {
if nil == numbers {
return 0
}
first := getFirstK(numbers, k, 0, len(numbers)-1)
last := getLastK(numbers, k, 0, len(numbers)-1)
if first > -1 && last > -1 {
return last - first + 1
}
return 0
}
func getMissingNumber(numbers []int) int {
if nil == numbers || len(numbers) <= 0 {
return -1
}
left, right := 0, len(numbers)-1
for left <= right {
middle := (left + right) >> 1
if numbers[middle] != middle {
if 0 == middle || numbers[middle-1] == middle-1 {
return middle
}
right = middle - 1
} else {
left = middle + 1
}
}
if left == len(numbers) {
return len(numbers)
}
return -1
}
func getNumberSameAsIndex(numbers []int) int {
if nil == numbers {
return -1
}
left, right := 0, len(numbers)-1
for left <= right {
middle := (left + right) / 2
if numbers[middle] == middle {
return middle
} else if numbers[middle] > middle {
right = middle - 1
} else {
left = middle + 1
}
}
return -1
}
================================================
FILE: algorithm/sfo/53/ac53_test.go
================================================
package problem53
import (
"testing"
"fmt"
)
func Test_getNumberOfK(t *testing.T) {
numbers := []int{1, 2, 3, 3, 3, 3, 4, 5}
k := 3
fmt.Println(getNumberOfK(numbers, k))
}
================================================
FILE: algorithm/sfo/54/ac54.go
================================================
package problem54
type BinaryTreeNode struct {
value int
left, right *BinaryTreeNode
}
func kthNode(root *BinaryTreeNode, k int) *BinaryTreeNode {
if nil == root || k <= 0 {
return nil
}
var target *BinaryTreeNode
if root.left != nil {
target = kthNode(root.left, k)
}
if target == nil {
if 1 == k {
target = root
} else {
k--
}
}
if target == nil && root.right != nil {
target = kthNode(root.right, k)
}
return target
}
================================================
FILE: algorithm/sfo/55/ac55.go
================================================
package problem55
type BinaryTreeNode struct {
value int
left, right *BinaryTreeNode
}
func treeDepth(root *BinaryTreeNode) int {
if nil == root {
return 0
}
left := treeDepth(root.left)
right := treeDepth(root.right)
if left >= right {
return left + 1
} else {
return right + 1
}
}
func isBalanced(root *BinaryTreeNode, depth *int) bool {
if nil == root {
*depth = 0
return true
}
var left, right int
if isBalanced(root.left, &left) && isBalanced(root.right, &right) {
diff := left - right
if -1 <= diff && diff <= 1 {
if left > right {
*depth = 1 + left
} else {
*depth = 1 + right
}
return true
}
}
return false
}
func isBalance(root *BinaryTreeNode) bool {
var depth int
return isBalanced(root, &depth)
}
================================================
FILE: algorithm/sfo/55/ac55_test.go
================================================
package problem55
import (
"testing"
"fmt"
)
func Test_treeDepth(t *testing.T) {
node7 := BinaryTreeNode{value: 7, left: nil, right: nil}
node6 := BinaryTreeNode{value: 6, left: nil, right: nil}
node5 := BinaryTreeNode{value: 5, left: &node7, right: nil}
node4 := BinaryTreeNode{value: 4, left: nil, right: nil}
node3 := BinaryTreeNode{value: 3, left: nil, right: &node6}
node2 := BinaryTreeNode{value: 2, left: &node4, right: &node5}
node1 := BinaryTreeNode{value: 1, left: &node2, right: &node3}
fmt.Println(treeDepth(&node1))
fmt.Println(isBalance(&node1))
}
================================================
FILE: algorithm/sfo/56/ac56.go
================================================
package problem56
import (
"unsafe"
"errors"
)
func findNumbersAppearOnce(data []int) (int, int) {
if nil == data || len(data) < 2 {
return 0, 0
}
var result int
for i := 0; i < len(data); i++ {
result ^= data[i]
}
indexOf1 := findFirstBitIs1(result)
var num1, num2 int
for j := 0; j < len(data); j++ {
if isBit1(data[j], indexOf1) {
num1 ^= data[j]
} else {
num2 ^= data[j]
}
}
return num1, num2
}
func findFirstBitIs1(num int) uint {
var indexBit uint
for (0 == num&1) && (indexBit < uint(unsafe.Sizeof(int(0))*8)) {
num = num >> 1
indexBit++
}
return indexBit
}
func isBit1(num int, indexBit uint) bool {
num = num >> indexBit
if 1 == num&1 {
return true
}
return false
}
func findNumberAppearOnce2(numbers []int) int {
if nil == numbers || 0 >= len(numbers) {
panic(errors.New("invalid input"))
}
bitSum := make([]int, 32)
for i := 0; i < len(numbers); i++ {
bitMask := 1
for j := 31; j >= 0; j-- {
bit := numbers[i] & bitMask
if 0 != bit {
bitSum[j] += 1
}
bitMask = bitMask << 1
}
}
var result int
for i := 0; i < 32; i++ {
result = result << 1
result += bitSum[i] % 3
}
return result
}
================================================
FILE: algorithm/sfo/56/ac56_test.go
================================================
package problem56
import (
"testing"
"fmt"
)
func Test_findNumbersAppearOnce(t *testing.T) {
data := []int{2, 4, 3, 6, 3, 2, 5, 5}
fmt.Println(findNumbersAppearOnce(data))
data2 := []int{2, 2, 2, 3, 3, 3, 4}
fmt.Println(findNumberAppearOnce2(data2))
}
================================================
FILE: algorithm/sfo/57/ac57.go
================================================
package problem57
import "fmt"
func findNumbers(data []int, sum int) (int, int, bool) {
var found bool
if nil == data || 1 > len(data) {
return 0, 0, found
}
ahead, behind := 0, len(data)-1
for ahead < behind {
currentSum := data[ahead] + data[behind]
if currentSum == sum {
found = true
return data[ahead], data[behind], found
} else if currentSum < sum {
ahead++
} else {
behind--
}
}
return 0, 0, found
}
func findContinuousSequence(sum int) {
if 3 > sum {
return
}
middle := (sum + 1) / 2
small, big := 1, 2
currentSum := small + big
for small < middle {
if currentSum == sum {
printContinuousSequence(small, big)
}
for currentSum > sum && small < middle {
currentSum -= small
small++
if currentSum == sum {
printContinuousSequence(small, big)
}
}
big++
currentSum += big
}
}
func printContinuousSequence(small, big int) {
for i := small; i <= big; i++ {
fmt.Printf("%v ", i)
}
fmt.Println()
}
================================================
FILE: algorithm/sfo/57/ac57_test.go
================================================
package problem57
import (
"testing"
"fmt"
)
func Test_findNumbers(t *testing.T) {
data := []int{1, 2, 4, 7, 11, 15}
sum := 15
fmt.Println(findNumbers(data, sum))
findContinuousSequence(15)
}
================================================
FILE: algorithm/sfo/58/ac58.go
================================================
package problem58
func reverse(str []rune) {
copyStr := str
begin, end := 0, len(copyStr)-1
for begin < end {
copyStr[begin], copyStr[end] = copyStr[end], copyStr[begin]
begin++
end--
}
}
func reverseSentence(str string) string {
if len(str) < 1 {
return ""
}
copyCharArray := []rune(str)
reverse(copyCharArray)
var begin, end int
for end <= len(copyCharArray) {
if end == len(copyCharArray) {
reverse(copyCharArray[begin:end])
break
}
if copyCharArray[end] != ' ' {
end++
} else {
reverse(copyCharArray[begin:end])
end++
begin = end
}
}
return string(copyCharArray)
}
func leftRotateString(str string, n int) string {
if len(str) < n {
return ""
}
copyCharArray := []rune(str)
reverse(copyCharArray[0:n])
reverse(copyCharArray[n:])
reverse(copyCharArray[:])
return string(copyCharArray)
}
================================================
FILE: algorithm/sfo/58/ac58_test.go
================================================
package problem58
import (
"fmt"
"testing"
)
func Test_reverseSentence(t *testing.T) {
text := "I am a student."
fmt.Println(reverseSentence(text))
}
func Test_leftRotateString(t *testing.T) {
text := "abcdefg"
fmt.Println(leftRotateString(text, 2))
}
================================================
FILE: algorithm/sfo/59/ac59.go
================================================
package problem59
import (
"gopkg.in/oleiade/lane.v1"
)
func maxInWindows(num []int, size int) []int {
maxInWindow := lane.NewDeque()
if len(num) >= size && size >= 1 {
index := lane.NewDeque()
for i := 0; i < size; i++ {
for !index.Empty() && num[i] >= num[index.Last().(int)] {
index.Pop()
}
index.Append(i)
}
for i := size; i < len(num); i++ {
maxInWindow.Append(num[index.First().(int)])
for !index.Empty() && num[i] >= num[index.Last().(int)] {
index.Pop()
}
if !index.Empty() && index.First().(int) <= i-size {
index.Shift()
}
index.Append(i)
}
maxInWindow.Append(num[index.First().(int)])
}
var ans []int
for !maxInWindow.Empty() {
ans = append(ans, maxInWindow.Shift().(int))
}
return ans
}
================================================
FILE: algorithm/sfo/59/ac59_test.go
================================================
package problem59
import (
"testing"
"fmt"
)
func Test_maxInWindows(t *testing.T) {
data := []int{2, 3, 4, 2, 6, 2, 5, 1}
size := 3
fmt.Println(maxInWindows(data, size))
}
================================================
FILE: algorithm/sfo/61/ac60.go
================================================
package problem61
import "sort"
func isContinuous(numbers []int) bool {
if nil == numbers || len(numbers) < 5 {
return false
}
sort.Ints(numbers)
numberOfZero, numberOfGap := 0, 0
for _, value := range numbers {
if value == 0 {
numberOfZero++
}
}
small, big := 0, 1
for big < len(numbers) {
if numbers[small] == numbers[big] {
return false
}
numberOfGap += numbers[big] - numbers[small] - 1
small = big
big++
}
if numberOfGap > numberOfZero {
return false
}
return true
}
================================================
FILE: algorithm/sfo/61/ac60_test.go
================================================
package problem61
import (
"testing"
"fmt"
)
func Test_isContinuous(t *testing.T) {
data := []int{3, 4, 0, 5, 1}
fmt.Println(isContinuous(data))
}
================================================
FILE: algorithm/sfo/62/ac62.go
================================================
package problem62
func lastRemaining(n, m int) int {
if n < 1 || m < 1 {
return -1
}
last := 0
for i := 2; i <= n; i++ {
last = (last + m) % i
}
return last
}
================================================
FILE: algorithm/sfo/62/ac62_test.go
================================================
package problem62
import (
"testing"
"fmt"
)
func Test_lastRemaining(t *testing.T) {
n, m := 5, 3
fmt.Println(lastRemaining(n, m))
}
================================================
FILE: algorithm/sfo/63/ac63.go
================================================
package problem63
func maxDiff(numbers []int) int {
if nil == numbers || len(numbers) < 2 {
return -1
}
min := numbers[0]
max := numbers[1] - min
for i := 2; i < len(numbers); i++ {
if numbers[i-1] < min {
min = numbers[i-1]
}
currentDiff := numbers[i] - min
if currentDiff > max {
max = currentDiff
}
}
return max
}
================================================
FILE: algorithm/sfo/63/ac63_test.go
================================================
package problem63
import (
"testing"
"fmt"
)
func Test_maxDiff(t *testing.T) {
data := []int{9, 11, 8, 5, 7, 12, 16, 14}
fmt.Println(maxDiff(data))
}
================================================
FILE: algorithm/sfo/65/ac64.go
================================================
package problem65
func Add(num1, num2 int) int {
var sum, carry int
for {
sum = num1 ^ num2
carry = (num1 & num2) << 1
num1 = sum
num2 = carry
if 0 == num2 {
break
}
}
return num1
}
================================================
FILE: algorithm/sfo/65/ac64_test.go
================================================
package problem65
import (
"testing"
"fmt"
)
func TestAdd(t *testing.T) {
fmt.Println(Add(9, 11))
}
================================================
FILE: lib/BinaryTreeNode.go
================================================
package lib
type BinaryTreeNode struct {
value int
left, right *BinaryTreeNode
}
================================================
FILE: lib/GetLine.go
================================================
package lib
import (
"bufio"
"strings"
"os"
)
func GetLine() string {
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
return strings.TrimSpace(line)
}
================================================
FILE: lib/Reverse.go
================================================
package lib
func Reverse(data []interface{}) {
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
data[i], data[j] = data[j], data[i]
}
}
================================================
FILE: lib/grpool/Pool.go
================================================
package grpool
import (
"sync"
)
type Pool struct {
queue chan int
wg *sync.WaitGroup
}
func New(size int) *Pool {
if size <= 0 {
size = 1
}
return &Pool{
queue: make(chan int, size),
wg: &sync.WaitGroup{},
}
}
func (p *Pool) Add(delta int) {
for i := 0; i < delta; i++ {
p.queue <- 1
}
for i := 0; i > delta; i-- {
<-p.queue
}
p.wg.Add(delta)
}
func (p *Pool) Done() {
<-p.queue
p.wg.Done()
}
func (p *Pool) WaitAll() {
p.wg.Wait()
}
================================================
FILE: notes/defer/main.go
================================================
package main
import (
"fmt"
)
// defer 语句会将函数推迟到外层函数返回之后执行。
// 推迟调用的函数其参数会立即求值,但直到外层函数返回前该函数都不会被调用。
// 推迟的函数调用会被压入一个栈中,当外层函数返回时,被推迟的函数会按照后进先出的顺序调用。
func main() {
fmt.Println("counting")
for i := 0; i < 10; i++ {
defer fmt.Println(i)
}
fmt.Println("done.")
}
================================================
FILE: notes/learn/main.go
================================================
package main
import (
"fmt"
"time"
"errors"
)
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(time.Second * 1)
c1 <- "one"
}()
go func() {
time.Sleep(time.Second * 2)
c2 <- "two"
}()
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received", msg1)
case msg2 := <-c2:
fmt.Println("received", msg2)
}
}
}
func f2(text string) {
for i := 0; i < 5; i++ {
fmt.Println(text, ":", i)
time.Sleep(1 * time.Second)
}
}
func f1(arg int) (int, error) {
if 42 == arg {
return -1, errors.New("can't work with 42")
}
return arg + 3, nil
}
func modify(slice []int) {
fmt.Printf("%p\n", &slice)
slice[1] = 10
}
func test(a, b int, c float64) {
fmt.Println(a + b + int(c))
}
func testMap() {
m := make(map[string]int)
m["a"] = 90
m["b"] = 60
fmt.Println(m)
fmt.Println(len(m))
value, status := m["a"]
fmt.Println(value, status)
value, status = m["aa"]
fmt.Println(value, status)
}
func testArray() {
//var a [5]int
//fmt.Println(a[3])
//fmt.Println(len(a))
//fmt.Println(cap(a))
b := [5]int{0, 1, 2, 3, 4}
fmt.Println(len(b))
s := make([]string, 5)
fmt.Println("emp:", s)
}
func IsEven(number int) bool {
if number&1 == 0 {
return true
} else {
return false
}
}
func IsEven2(number int) bool {
if number%2 == 0 {
return true
} else {
return false
}
}
func testEven() {
LOOP := 100000
num := 9
start1 := time.Now()
fmt.Println(start1)
for i := 0; i < LOOP; i++ {
IsEven(num)
}
end1 := time.Now()
fmt.Println(end1)
fmt.Println("位运算:", end1.Sub(start1))
start2 := time.Now()
fmt.Println(start2)
for i := 0; i < LOOP; i++ {
IsEven2(num)
}
end2 := time.Now()
fmt.Println(end2)
fmt.Println("求余运算:", end2.Sub(start2))
}
================================================
FILE: notes/main/main.go
================================================
package main
import (
"GoNotes/src/lib"
)
func main() {
lib.SayHello()
}
================================================
FILE: notes/nil/main.go
================================================
package main
import (
"fmt"
)
func main() {
// nil 是不能比较的, == 对于 nil 来说是一种未定义的操作。
// 运行下面这行将报错: invalid operation: nil == nil (operator == not defined on nil)
// fmt.Println(nil == nil)
// nil没有type
fmt.Printf("%T\n", nil)
fmt.Println("**************************")
// 不同类型 nil 的 address 是一样的
var number *int
var dict map[string]int
fmt.Printf("%p\n", number)
fmt.Printf("%p\n", dict)
fmt.Println("**************************")
// nil 是 map,slice,pointer,channel,func,interface 的零值
var m map[int]string
var ptr *int
var c chan int
var sl []int
var f func()
var i interface{}
fmt.Printf("%#v\n", m)
fmt.Printf("%#v\n", ptr)
fmt.Printf("%#v\n", c)
fmt.Printf("%#v\n", sl)
fmt.Printf("%#v\n", f)
fmt.Printf("%#v\n", i)
fmt.Println("**************************")
}
================================================
FILE: notes/switch/main.go
================================================
package main
import (
"fmt"
"runtime"
"time"
)
// switch 的 case 语句从上到下顺序执行,直到匹配成功时停止。
// switch 只运行选定的 case, 除非以 fallthrough 结束,否则分支自动终止。
// switch 的 case 无需为常数,且取值不必为整数。
func main() {
fmt.Print("Go runs on ")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux")
default:
fmt.Printf("%s.", os)
}
test(3.1415926)
test(1)
test(1.0)
test(2.71828)
test2()
}
func test(x float64) {
switch x {
case 3.1415926:
fmt.Println("PI")
case 1.0:
fmt.Println("1 or 1.0")
default:
fmt.Println("no match", x)
}
}
// 没有条件的 switch 同 switch true 一样。这种形式能将一长串 if-then-else 写得更加清晰。
func test2() {
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning.")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
================================================
FILE: notes/time/main.go
================================================
package main
import (
"time"
"fmt"
)
func main() {
fmt.Println("现在时间是: ", time.Now())
t := time.Now()
fmt.Println(t.Year())
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Hour())
fmt.Println(t.Minute())
fmt.Println(t.Second())
fmt.Println(t.Date())
fmt.Println(t.Clock())
fmt.Println(t.Format("19:21:01.123"))
}
================================================
FILE: notes/函数/main.go
================================================
package main
import "fmt"
// 函数可以接受0个或多个参数
func main() {
fmt.Println("this is main function")
fmt.Println(Add(9, 12))
fmt.Println(add(9, 12, 1))
fmt.Println(add2(9, 12, 1))
fmt.Println(add3(false, 12, 1))
fmt.Println(swap("time", "golang"))
fmt.Println(split(20))
}
func Add(x int, y int) int {
return x + y
}
func add(x int, y int, z int) int {
return x + y + z
}
func add2(x, y, z int) int {
return x + y + z
}
func add3(x bool, y, z int) (bool, int) {
return x, y + z
}
func swap(x, y string) (string, string) {
a, b := x, y
fmt.Print("swap->", a, " ",b, "->")
return y, x
}
// 命名返回值
// Go 的返回值可以被命名,它们会被视作定义在函数顶部的变量。
// 没有参数的 return 语句返回已命名的返回值,也就是 “直接” 返回。
func split(sum int) (x, y int) {
x = sum * 2 - 1
y = x - sum
return
}
================================================
FILE: notes/判断/main.go
================================================
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(myPow(2, 3, 99))
fmt.Println(myPow(2, 3, 5))
fmt.Printf("sqrt %v is %v", 2, Sqrt(2))
}
// if 语句可以在表达式前执行一个简单的语句,该语句声明的变量作用域仅在 if 之内。
// 在 if 的简短语句中声明的变量同样可以在任何对应的 else 块中使用。
func myPow(x, y, ans float64) float64 {
if z := math.Pow(x, y); z < ans {
return z
} else {
fmt.Printf("z type:
gitextract_5aw899i7/
├── .gitignore
├── LICENSE
├── README.md
├── algorithm/
│ ├── leetcode/
│ │ ├── 001/
│ │ │ ├── ac001.go
│ │ │ └── ac001_test.go
│ │ ├── 007/
│ │ │ ├── ac007.go
│ │ │ └── ac007_test.go
│ │ ├── 009/
│ │ │ ├── ac009.go
│ │ │ └── ac009_test.go
│ │ ├── 013/
│ │ │ ├── ac013.go
│ │ │ └── ac013_test.go
│ │ ├── 014/
│ │ │ ├── ac014.go
│ │ │ └── ac014_test.go
│ │ ├── 020/
│ │ │ ├── ac020.go
│ │ │ └── ac020_test.go
│ │ ├── 021/
│ │ │ └── ac021.go
│ │ ├── 026/
│ │ │ └── ac026.go
│ │ ├── 027/
│ │ │ └── ac027.go
│ │ ├── 028/
│ │ │ └── ac028.go
│ │ ├── 035/
│ │ │ ├── ac035.go
│ │ │ └── ac035_test.go
│ │ ├── 038/
│ │ │ └── ac038.go
│ │ ├── 053/
│ │ │ └── ac053.go
│ │ ├── 058/
│ │ │ ├── ac058.go
│ │ │ └── ac058_test.go
│ │ ├── 066/
│ │ │ └── ac066.go
│ │ ├── 067/
│ │ │ ├── ac067.go
│ │ │ └── ac067_test.go
│ │ ├── 069/
│ │ │ ├── ac069.go
│ │ │ └── ac069_test.go
│ │ ├── 070/
│ │ │ └── ac070.go
│ │ ├── 083/
│ │ │ └── ac083.go
│ │ ├── 088/
│ │ │ └── ac088.go
│ │ ├── 100/
│ │ │ └── ac100.go
│ │ ├── 101/
│ │ │ └── ac101.go
│ │ ├── 104/
│ │ │ └── ac104.go
│ │ ├── 107/
│ │ │ ├── ac107.go
│ │ │ └── ac107_test.go
│ │ ├── 108/
│ │ │ └── ac108.go
│ │ ├── 110/
│ │ │ └── ac110.go
│ │ ├── 111/
│ │ │ └── ac111.go
│ │ ├── 112/
│ │ │ └── ac112.go
│ │ ├── 118/
│ │ │ └── ac118.go
│ │ ├── 119/
│ │ │ └── ac119.go
│ │ ├── 120/
│ │ │ ├── ac120.go
│ │ │ └── ac120_test.go
│ │ ├── 121/
│ │ │ ├── ac121.go
│ │ │ └── ac121_test.go
│ │ ├── 122/
│ │ │ └── ac122.go
│ │ ├── 125/
│ │ │ ├── ac125.go
│ │ │ └── ac125_test.go
│ │ ├── 136/
│ │ │ └── ac136.go
│ │ ├── 137/
│ │ │ ├── ac137.go
│ │ │ └── ac137_test.go
│ │ ├── 160/
│ │ │ ├── ac160.go
│ │ │ └── ac160_test.go
│ │ ├── 167/
│ │ │ └── ac167.go
│ │ ├── 168/
│ │ │ ├── ac141.go
│ │ │ └── ac141_test.go
│ │ ├── 169/
│ │ │ └── ac169.go
│ │ ├── 171/
│ │ │ └── ac171.go
│ │ ├── 172/
│ │ │ └── ac172.go
│ │ ├── 189/
│ │ │ ├── ac189.go
│ │ │ └── ac189_test.go
│ │ ├── 191/
│ │ │ ├── ac191.go
│ │ │ └── ac191_test.go
│ │ ├── 198/
│ │ │ └── ac198.go
│ │ ├── 202/
│ │ │ └── ac202.go
│ │ ├── 203/
│ │ │ └── ac203.go
│ │ ├── 204/
│ │ │ └── ac204.go
│ │ ├── 205/
│ │ │ ├── ac205.go
│ │ │ └── ac205_test.go
│ │ ├── 206/
│ │ │ └── ac206.go
│ │ ├── 217/
│ │ │ └── ac217.go
│ │ ├── 219/
│ │ │ └── ac219.go
│ │ ├── 226/
│ │ │ └── ac226.go
│ │ ├── 231/
│ │ │ └── ac231.go
│ │ ├── 234/
│ │ │ └── ac234.go
│ │ ├── 258/
│ │ │ └── ac258.go
│ │ ├── 263/
│ │ │ └── ac263.go
│ │ ├── 268/
│ │ │ ├── ac268.go
│ │ │ └── ac268_test.go
│ │ ├── 283/
│ │ │ └── ac283.go
│ │ ├── 290/
│ │ │ ├── ac290.go
│ │ │ └── ac290_test.go
│ │ ├── 292/
│ │ │ └── ac292.go
│ │ ├── 303/
│ │ │ ├── ac303.go
│ │ │ └── ac303_test.go
│ │ ├── 326/
│ │ │ ├── ac326.go
│ │ │ └── ac326_test.go
│ │ ├── 342/
│ │ │ └── ac342.go
│ │ ├── 344/
│ │ │ ├── ac344.go
│ │ │ └── ac344_test.go
│ │ ├── 345/
│ │ │ ├── ac345.go
│ │ │ └── ac345_test.go
│ │ ├── 349/
│ │ │ ├── ac349.go
│ │ │ └── ac349_test.go
│ │ ├── 350/
│ │ │ ├── ac350.go
│ │ │ └── ac350_test.go
│ │ ├── 367/
│ │ │ └── ac367.go
│ │ ├── 371/
│ │ │ └── ac371.go
│ │ ├── 383/
│ │ │ ├── ac383.go
│ │ │ └── ac383_test.go
│ │ ├── 387/
│ │ │ └── ac387.go
│ │ ├── 389/
│ │ │ ├── ac389.go
│ │ │ └── ac389_test.go
│ │ ├── 400/
│ │ │ ├── ac400.go
│ │ │ └── ac400_test.go
│ │ ├── 404/
│ │ │ └── ac404.go
│ │ ├── 405/
│ │ │ ├── ac405.go
│ │ │ └── ac405_test.go
│ │ ├── 409/
│ │ │ ├── ac409.go
│ │ │ └── ac409_test.go
│ │ ├── 412/
│ │ │ ├── ac412.go
│ │ │ └── ac412_test.go
│ │ ├── 414/
│ │ │ ├── ac414.go
│ │ │ └── ac414_test.go
│ │ ├── 415/
│ │ │ ├── ac415.go
│ │ │ └── ac415_test.go
│ │ ├── 434/
│ │ │ └── ac434.go
│ │ ├── 437/
│ │ │ └── ac437.go
│ │ ├── 438/
│ │ │ ├── ac438.go
│ │ │ └── ac438_test.go
│ │ ├── 441/
│ │ │ ├── ac441.go
│ │ │ └── ac441_test.go
│ │ ├── 443/
│ │ │ ├── ac443.go
│ │ │ └── ac443_test.go
│ │ ├── 447/
│ │ │ └── ac447.go
│ │ ├── 448/
│ │ │ └── ac448.go
│ │ ├── 453/
│ │ │ ├── ac453.go
│ │ │ └── ac453_test.go
│ │ ├── 455/
│ │ │ ├── ac455.go
│ │ │ └── ac455_test.go
│ │ ├── 458/
│ │ │ └── ac458.go
│ │ ├── 459/
│ │ │ ├── ac459.go
│ │ │ └── ac459_test.go
│ │ ├── 461/
│ │ │ └── ac461.go
│ │ ├── 463/
│ │ │ ├── ac463.go
│ │ │ └── ac463_test.go
│ │ ├── 475/
│ │ │ ├── ac475.go
│ │ │ └── ac475_test.go
│ │ ├── 476/
│ │ │ ├── ac476.go
│ │ │ └── ac476_test.go
│ │ ├── 479/
│ │ │ ├── ac479.go
│ │ │ └── ac479_test.go
│ │ ├── 485/
│ │ │ ├── ac485.go
│ │ │ └── ac485_test.go
│ │ ├── 492/
│ │ │ ├── ac492.go
│ │ │ └── ac492_test.go
│ │ ├── 496/
│ │ │ ├── ac496.go
│ │ │ └── ac496_test.go
│ │ ├── 500/
│ │ │ ├── ac500.go
│ │ │ └── ac500_test.go
│ │ ├── 501/
│ │ │ ├── ac501.go
│ │ │ └── ac501_test.go
│ │ ├── 504/
│ │ │ ├── ac504.go
│ │ │ └── ac504_test.go
│ │ ├── 506/
│ │ │ ├── ac506.go
│ │ │ └── ac506_test.go
│ │ ├── 507/
│ │ │ ├── ac507.go
│ │ │ └── ac507_test.go
│ │ ├── 520/
│ │ │ ├── ac520.go
│ │ │ └── ac520_test.go
│ │ ├── 521/
│ │ │ └── ac521.go
│ │ ├── 530/
│ │ │ ├── ac530.go
│ │ │ └── ac530_test.go
│ │ ├── 532/
│ │ │ ├── ac532.go
│ │ │ └── ac532_test.go
│ │ ├── 538/
│ │ │ └── ac538.go
│ │ ├── 541/
│ │ │ ├── ac541.go
│ │ │ └── ac541_test.go
│ │ ├── 543/
│ │ │ └── ac543.go
│ │ ├── 551/
│ │ │ ├── ac551.go
│ │ │ └── ac551_test.go
│ │ ├── 557/
│ │ │ ├── ac557.go
│ │ │ └── ac557_test.go
│ │ ├── 561/
│ │ │ ├── ac561.go
│ │ │ └── ac561_test.go
│ │ ├── 563/
│ │ │ └── ac563.go
│ │ ├── 566/
│ │ │ ├── ac566.go
│ │ │ └── ac566_test.go
│ │ ├── 572/
│ │ │ └── ac572.go
│ │ ├── 575/
│ │ │ ├── ac575.go
│ │ │ └── ac575_test.go
│ │ ├── 581/
│ │ │ ├── ac581.go
│ │ │ └── ac581_test.go
│ │ ├── 594/
│ │ │ ├── ac594.go
│ │ │ └── ac594_test.go
│ │ ├── 598/
│ │ │ ├── ac598.go
│ │ │ └── ac598_test.go
│ │ ├── 599/
│ │ │ ├── ac599.go
│ │ │ └── ac599_test.go
│ │ ├── 605/
│ │ │ ├── ac605.go
│ │ │ └── ac605_test.go
│ │ ├── 606/
│ │ │ └── ac606.go
│ │ ├── 617/
│ │ │ └── ac617.go
│ │ ├── 628/
│ │ │ └── ac628.go
│ │ ├── 633/
│ │ │ └── ac633.go
│ │ ├── 637/
│ │ │ └── ac637.go
│ │ ├── 643/
│ │ │ ├── ac643.go
│ │ │ └── ac643_test.go
│ │ ├── 645/
│ │ │ ├── ac645.go
│ │ │ └── ac645_test.go
│ │ ├── 653/
│ │ │ ├── ac653.go
│ │ │ └── ac653_test.go
│ │ ├── 657/
│ │ │ ├── ac657.go
│ │ │ └── ac657_test.go
│ │ ├── 661/
│ │ │ ├── ac661.go
│ │ │ └── ac661_test.go
│ │ ├── 665/
│ │ │ ├── ac665.go
│ │ │ └── ac665_test.go
│ │ ├── 669/
│ │ │ └── ac669.go
│ │ ├── 671/
│ │ │ ├── ac671.go
│ │ │ └── ac671_test.go
│ │ ├── 674/
│ │ │ ├── ac674.go
│ │ │ └── ac674_test.go
│ │ ├── 680/
│ │ │ └── ac680.go
│ │ ├── 682/
│ │ │ ├── ac682.go
│ │ │ └── ac682_test.go
│ │ ├── 686/
│ │ │ ├── ac686.go
│ │ │ └── ac686_test.go
│ │ ├── 687/
│ │ │ └── ac687.go
│ │ ├── 693/
│ │ │ ├── ac693.go
│ │ │ └── ac693_test.go
│ │ ├── 695/
│ │ │ └── ac695.go
│ │ ├── 696/
│ │ │ └── ac696.go
│ │ ├── 697/
│ │ │ └── ac697.go
│ │ ├── 717/
│ │ │ └── ac717.go
│ │ ├── 724/
│ │ │ └── ac724.go
│ │ ├── 728/
│ │ │ └── ac728.go
│ │ ├── 744/
│ │ │ └── ac744.go
│ │ ├── 746/
│ │ │ └── ac746.go
│ │ ├── 747/
│ │ │ ├── ac747.go
│ │ │ └── ac747_test.go
│ │ ├── 762/
│ │ │ └── ac762.go
│ │ ├── 766/
│ │ │ ├── ac766.go
│ │ │ └── ac766_test.go
│ │ ├── 771/
│ │ │ ├── ac771.go
│ │ │ └── ac771_test.go
│ │ ├── 783/
│ │ │ ├── ac783.go
│ │ │ └── ac783_test.go
│ │ ├── 784/
│ │ │ ├── ac784.go
│ │ │ └── ac784_test.go
│ │ ├── 788/
│ │ │ └── ac788.go
│ │ ├── 796/
│ │ │ └── ac796.go
│ │ ├── 804/
│ │ │ ├── ac804.go
│ │ │ └── ac804_test.go
│ │ ├── 811/
│ │ │ ├── ac811.go
│ │ │ └── ac811_test.go
│ │ ├── 812/
│ │ │ ├── ac812.go
│ │ │ └── ac812_test.go
│ │ ├── 819/
│ │ │ ├── ac819.go
│ │ │ └── ac819_test.go
│ │ ├── 821/
│ │ │ ├── ac821.go
│ │ │ └── ac821_test.go
│ │ ├── 824/
│ │ │ ├── ac824.go
│ │ │ └── ac824_test.go
│ │ ├── 829/
│ │ │ ├── ac829.go
│ │ │ └── ac829_test.go
│ │ ├── 830/
│ │ │ ├── ac830.go
│ │ │ └── ac830_test.go
│ │ ├── 832/
│ │ │ └── ac832.go
│ │ ├── 836/
│ │ │ └── ac836.go
│ │ ├── 840/
│ │ │ └── ac840.go
│ │ └── 844/
│ │ └── ac844.go
│ └── sfo/
│ ├── 30/
│ │ ├── ac30.go
│ │ └── ac30_test.go
│ ├── 31/
│ │ ├── ac31.go
│ │ └── ac31_test.go
│ ├── 32/
│ │ ├── ac32.go
│ │ └── ac32_test.go
│ ├── 33/
│ │ ├── ac33.go
│ │ └── ac33_test.go
│ ├── 34/
│ │ ├── ac34.go
│ │ └── ac34_test.go
│ ├── 35/
│ │ └── ac35.go
│ ├── 36/
│ │ ├── ac36.go
│ │ └── ac36_test.go
│ ├── 38/
│ │ ├── ac38.go
│ │ └── ac38_test.go
│ ├── 39/
│ │ ├── ac39.go
│ │ └── ac39_test.go
│ ├── 40/
│ │ ├── ac40.go
│ │ └── ac40_test.go
│ ├── 42/
│ │ ├── ac42.go
│ │ └── ac42_test.go
│ ├── 43/
│ │ ├── ac43.go
│ │ └── ac43_test.go
│ ├── 44/
│ │ ├── ac44.go
│ │ └── ac44_test.go
│ ├── 45/
│ │ ├── ac45.go
│ │ └── ac45_test.go
│ ├── 46/
│ │ ├── ac46.go
│ │ └── ac46_test.go
│ ├── 47/
│ │ ├── ac47.go
│ │ └── ac47_test.go
│ ├── 48/
│ │ ├── ac48.go
│ │ └── ac48_test.go
│ ├── 49/
│ │ ├── ac49.go
│ │ └── ac49_test.go
│ ├── 50/
│ │ ├── ac50.go
│ │ └── ac50_test.go
│ ├── 51/
│ │ ├── ac51.go
│ │ └── ac51_test.go
│ ├── 52/
│ │ ├── ac52.go
│ │ └── ac52_test.go
│ ├── 53/
│ │ ├── ac53.go
│ │ └── ac53_test.go
│ ├── 54/
│ │ └── ac54.go
│ ├── 55/
│ │ ├── ac55.go
│ │ └── ac55_test.go
│ ├── 56/
│ │ ├── ac56.go
│ │ └── ac56_test.go
│ ├── 57/
│ │ ├── ac57.go
│ │ └── ac57_test.go
│ ├── 58/
│ │ ├── ac58.go
│ │ └── ac58_test.go
│ ├── 59/
│ │ ├── ac59.go
│ │ └── ac59_test.go
│ ├── 61/
│ │ ├── ac60.go
│ │ └── ac60_test.go
│ ├── 62/
│ │ ├── ac62.go
│ │ └── ac62_test.go
│ ├── 63/
│ │ ├── ac63.go
│ │ └── ac63_test.go
│ └── 65/
│ ├── ac64.go
│ └── ac64_test.go
├── lib/
│ ├── BinaryTreeNode.go
│ ├── GetLine.go
│ ├── Reverse.go
│ └── grpool/
│ └── Pool.go
└── notes/
├── defer/
│ └── main.go
├── learn/
│ └── main.go
├── main/
│ └── main.go
├── nil/
│ └── main.go
├── switch/
│ └── main.go
├── time/
│ └── main.go
├── 函数/
│ └── main.go
├── 判断/
│ └── main.go
├── 协程/
│ └── main.go
├── 变量/
│ └── main.go
├── 命令行参数/
│ └── iie.go
├── 导入导出/
│ └── main.go
├── 常量/
│ └── main.go
├── 循环/
│ └── main.go
├── 数组/
│ └── main.go
├── 输入/
│ └── main.go
└── 输出/
└── main.go
SYMBOL INDEX (554 symbols across 345 files)
FILE: algorithm/leetcode/001/ac001.go
function twoSum (line 3) | func twoSum(nums []int, target int) []int {
FILE: algorithm/leetcode/001/ac001_test.go
type argument (line 8) | type argument struct
type answer (line 13) | type answer struct
type example (line 17) | type example struct
function TestOk (line 22) | func TestOk(t *testing.T) {
FILE: algorithm/leetcode/007/ac007.go
function reverse (line 5) | func reverse(x int) int {
FILE: algorithm/leetcode/007/ac007_test.go
type argument (line 8) | type argument struct
type answer (line 12) | type answer struct
type example (line 16) | type example struct
function Test_reverse (line 21) | func Test_reverse(t *testing.T) {
FILE: algorithm/leetcode/009/ac009.go
function isPalindrome (line 4) | func isPalindrome(x int) bool {
FILE: algorithm/leetcode/009/ac009_test.go
function Test_isPalindrome (line 8) | func Test_isPalindrome(t *testing.T) {
FILE: algorithm/leetcode/013/ac013.go
function romanToInt (line 3) | func romanToInt(s string) int {
FILE: algorithm/leetcode/013/ac013_test.go
function Test_romanToInt (line 8) | func Test_romanToInt(t *testing.T) {
FILE: algorithm/leetcode/014/ac014.go
function longestCommonPrefix (line 7) | func longestCommonPrefix(strs []string) string {
FILE: algorithm/leetcode/014/ac014_test.go
function Test_longestCommonPrefix (line 8) | func Test_longestCommonPrefix(t *testing.T) {
FILE: algorithm/leetcode/020/ac020.go
function isValid (line 5) | func isValid(s string) bool {
FILE: algorithm/leetcode/020/ac020_test.go
function Test_isValid (line 8) | func Test_isValid(t *testing.T) {
FILE: algorithm/leetcode/021/ac021.go
type ListNode (line 3) | type ListNode struct
function mergeTwoLists (line 8) | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
FILE: algorithm/leetcode/026/ac026.go
function removeDuplicates (line 3) | func removeDuplicates(nums []int) int {
FILE: algorithm/leetcode/027/ac027.go
function removeElement (line 3) | func removeElement(nums []int, val int) int {
FILE: algorithm/leetcode/028/ac028.go
function strStr (line 3) | func strStr(haystack string, needle string) int {
FILE: algorithm/leetcode/035/ac035.go
function searchInsert (line 3) | func searchInsert(nums []int, target int) int {
FILE: algorithm/leetcode/035/ac035_test.go
function Test_searchInsert (line 8) | func Test_searchInsert(t *testing.T) {
FILE: algorithm/leetcode/038/ac038.go
function countAndSay (line 5) | func countAndSay(n int) string {
FILE: algorithm/leetcode/053/ac053.go
function maxSubArray (line 3) | func maxSubArray(nums []int) int {
FILE: algorithm/leetcode/058/ac058.go
function lengthOfLastWord (line 7) | func lengthOfLastWord(s string) int {
function lengthOfLastWord2 (line 18) | func lengthOfLastWord2(s string) int {
FILE: algorithm/leetcode/058/ac058_test.go
function Test_lengthOfLastWord (line 8) | func Test_lengthOfLastWord(t *testing.T) {
FILE: algorithm/leetcode/066/ac066.go
function plusOne (line 3) | func plusOne(digits []int) []int {
FILE: algorithm/leetcode/067/ac067.go
function addBinary (line 3) | func addBinary(a string, b string) string {
function addBinary2 (line 29) | func addBinary2(a string, b string) string {
FILE: algorithm/leetcode/067/ac067_test.go
function Test_addBinary (line 8) | func Test_addBinary(t *testing.T) {
FILE: algorithm/leetcode/069/ac069.go
function mySqrt (line 3) | func mySqrt(x int) int {
FILE: algorithm/leetcode/069/ac069_test.go
function Test_mySqrt (line 8) | func Test_mySqrt(t *testing.T) {
FILE: algorithm/leetcode/070/ac070.go
function climbStairs (line 3) | func climbStairs(n int) int {
function climbStairs2 (line 17) | func climbStairs2(n int) int {
FILE: algorithm/leetcode/083/ac083.go
type ListNode (line 3) | type ListNode struct
function deleteDuplicates (line 8) | func deleteDuplicates(head *ListNode) *ListNode {
FILE: algorithm/leetcode/088/ac088.go
function merge (line 3) | func merge(nums1 []int, m int, nums2 []int, n int) {
FILE: algorithm/leetcode/100/ac100.go
type TreeNode (line 3) | type TreeNode struct
function isSameTree (line 9) | func isSameTree(p *TreeNode, q *TreeNode) bool {
FILE: algorithm/leetcode/101/ac101.go
type TreeNode (line 3) | type TreeNode struct
function isSymmetric (line 9) | func isSymmetric(root *TreeNode) bool {
function isSame (line 13) | func isSame(left, right *TreeNode) bool {
FILE: algorithm/leetcode/104/ac104.go
type TreeNode (line 3) | type TreeNode struct
function maxDepth (line 9) | func maxDepth(root *TreeNode) int {
FILE: algorithm/leetcode/107/ac107.go
type TreeNode (line 3) | type TreeNode struct
function levelOrderBottom (line 9) | func levelOrderBottom(root *TreeNode) [][]int {
FILE: algorithm/leetcode/107/ac107_test.go
function Test_levelOrderBottom (line 8) | func Test_levelOrderBottom(t *testing.T) {
FILE: algorithm/leetcode/108/ac108.go
type TreeNode (line 3) | type TreeNode struct
function sortedArrayToBST (line 9) | func sortedArrayToBST(nums []int) *TreeNode {
FILE: algorithm/leetcode/110/ac110.go
type TreeNode (line 3) | type TreeNode struct
function isBalanced (line 9) | func isBalanced(root *TreeNode) bool {
function depth (line 20) | func depth(root *TreeNode) int {
FILE: algorithm/leetcode/111/ac111.go
type TreeNode (line 3) | type TreeNode struct
function minDepth (line 9) | func minDepth(root *TreeNode) int {
FILE: algorithm/leetcode/112/ac112.go
type TreeNode (line 3) | type TreeNode struct
function hasPathSum (line 9) | func hasPathSum(root *TreeNode, sum int) bool {
FILE: algorithm/leetcode/118/ac118.go
function generate (line 3) | func generate(numRows int) [][]int {
FILE: algorithm/leetcode/119/ac119.go
function getRow (line 3) | func getRow(rowIndex int) []int {
FILE: algorithm/leetcode/120/ac120.go
function minimumTotal (line 5) | func minimumTotal(triangle [][]int) int {
function myMin (line 17) | func myMin(a, b int) int {
FILE: algorithm/leetcode/120/ac120_test.go
function Test_minimumTotal (line 8) | func Test_minimumTotal(t *testing.T) {
FILE: algorithm/leetcode/121/ac121.go
function maxProfit (line 3) | func maxProfit(prices []int) int {
FILE: algorithm/leetcode/121/ac121_test.go
function Test_maxProfit (line 8) | func Test_maxProfit(t *testing.T) {
FILE: algorithm/leetcode/122/ac122.go
function maxProfit (line 3) | func maxProfit(prices []int) int {
FILE: algorithm/leetcode/125/ac125.go
function isPalindrome (line 3) | func isPalindrome(s string) bool {
function isCharacter (line 26) | func isCharacter(c byte) bool {
function lowerCharacter (line 34) | func lowerCharacter(c byte) byte {
FILE: algorithm/leetcode/125/ac125_test.go
function Test_isCharacter (line 8) | func Test_isCharacter(t *testing.T) {
FILE: algorithm/leetcode/136/ac136.go
function singleNumber (line 3) | func singleNumber(nums []int) int {
FILE: algorithm/leetcode/137/ac137.go
function singleNumber (line 3) | func singleNumber(nums []int) int {
function singleNumber2 (line 23) | func singleNumber2(nums []int) int {
function singleNumber3 (line 32) | func singleNumber3(nums []int) int {
function singleNumber4 (line 50) | func singleNumber4(nums []int) int {
FILE: algorithm/leetcode/137/ac137_test.go
function Test_singleNumber (line 8) | func Test_singleNumber(t *testing.T) {
FILE: algorithm/leetcode/160/ac160.go
type ListNode (line 3) | type ListNode struct
function getIntersectionNode (line 8) | func getIntersectionNode(headA, headB *ListNode) *ListNode {
FILE: algorithm/leetcode/160/ac160_test.go
function Test_getIntersectionNode (line 12) | func Test_getIntersectionNode(t *testing.T) {
FILE: algorithm/leetcode/167/ac167.go
function twoSum (line 3) | func twoSum(numbers []int, target int) []int {
FILE: algorithm/leetcode/168/ac141.go
function convertToTitle (line 3) | func convertToTitle(n int) string {
FILE: algorithm/leetcode/168/ac141_test.go
function Test_convertToTitle (line 8) | func Test_convertToTitle(t *testing.T) {
FILE: algorithm/leetcode/169/ac169.go
function majorityElement (line 3) | func majorityElement(nums []int) int {
FILE: algorithm/leetcode/171/ac171.go
function titleToNumber (line 3) | func titleToNumber(s string) int {
FILE: algorithm/leetcode/172/ac172.go
function trailingZeroes (line 3) | func trailingZeroes(n int) int {
FILE: algorithm/leetcode/189/ac189.go
function rotate (line 3) | func rotate(nums []int, k int) {
function reverse (line 21) | func reverse(nums []int) {
FILE: algorithm/leetcode/189/ac189_test.go
function Test_rotate (line 8) | func Test_rotate(t *testing.T) {
FILE: algorithm/leetcode/191/ac191.go
function hammingWeight (line 3) | func hammingWeight(n uint32) int {
FILE: algorithm/leetcode/191/ac191_test.go
function Test_hammingWeight (line 8) | func Test_hammingWeight(t *testing.T) {
FILE: algorithm/leetcode/198/ac198.go
function rob (line 3) | func rob(nums []int) int {
function maxInt (line 24) | func maxInt(a, b int) int {
FILE: algorithm/leetcode/202/ac202.go
function isHappy (line 3) | func isHappy(n int) bool {
function digitSquareSum (line 20) | func digitSquareSum(n int) int {
FILE: algorithm/leetcode/203/ac203.go
type ListNode (line 3) | type ListNode struct
function removeElements (line 9) | func removeElements(head *ListNode, val int) *ListNode {
function removeElements2 (line 24) | func removeElements2(head *ListNode, val int) *ListNode {
FILE: algorithm/leetcode/204/ac204.go
function countPrimes (line 3) | func countPrimes(n int) int {
FILE: algorithm/leetcode/205/ac205.go
function isIsomorphic (line 3) | func isIsomorphic(s string, t string) bool {
FILE: algorithm/leetcode/205/ac205_test.go
function Test_isIsomorphic (line 8) | func Test_isIsomorphic(t *testing.T) {
FILE: algorithm/leetcode/206/ac206.go
type ListNode (line 3) | type ListNode struct
function reverseList (line 8) | func reverseList(head *ListNode) *ListNode {
function reverseList1 (line 19) | func reverseList1(head *ListNode) *ListNode {
function reverseCore (line 23) | func reverseCore(head, newHead *ListNode) *ListNode {
FILE: algorithm/leetcode/217/ac217.go
function containsDuplicate (line 5) | func containsDuplicate(nums []int) bool {
function containsDuplicate2 (line 20) | func containsDuplicate2(nums []int) bool {
FILE: algorithm/leetcode/219/ac219.go
function containsNearbyDuplicate (line 3) | func containsNearbyDuplicate(nums []int, k int) bool {
FILE: algorithm/leetcode/226/ac226.go
type TreeNode (line 3) | type TreeNode struct
function invertTree (line 9) | func invertTree(root *TreeNode) *TreeNode {
FILE: algorithm/leetcode/231/ac231.go
function isPowerOfTwo (line 3) | func isPowerOfTwo(n int) bool {
FILE: algorithm/leetcode/234/ac234.go
type ListNode (line 3) | type ListNode struct
function isPalindrome (line 10) | func isPalindrome(head *ListNode) bool {
function isPalindrome2 (line 28) | func isPalindrome2(head *ListNode) bool {
FILE: algorithm/leetcode/258/ac258.go
function addDigits (line 3) | func addDigits(num int) int {
FILE: algorithm/leetcode/263/ac263.go
function isUgly (line 3) | func isUgly(num int) bool {
FILE: algorithm/leetcode/268/ac268.go
function missingNumber (line 5) | func missingNumber(nums []int) int {
function missingNumber2 (line 23) | func missingNumber2(nums []int) int {
function missingNumber3 (line 31) | func missingNumber3(nums []int) int {
FILE: algorithm/leetcode/268/ac268_test.go
function Test_missingNumber (line 8) | func Test_missingNumber(t *testing.T) {
FILE: algorithm/leetcode/283/ac283.go
function moveZeroes (line 3) | func moveZeroes(nums []int) {
FILE: algorithm/leetcode/290/ac290.go
function wordPattern (line 5) | func wordPattern(pattern string, str string) bool {
FILE: algorithm/leetcode/290/ac290_test.go
function Test_wordPattern (line 8) | func Test_wordPattern(t *testing.T) {
FILE: algorithm/leetcode/292/ac292.go
function canWinNim (line 5) | func canWinNim(n int) bool {
FILE: algorithm/leetcode/303/ac303.go
type NumArray (line 3) | type NumArray struct
method SumRange (line 15) | func (this *NumArray) SumRange(i int, j int) int {
function Constructor (line 7) | func Constructor(nums []int) NumArray {
FILE: algorithm/leetcode/303/ac303_test.go
function TestNumArray_SumRange (line 8) | func TestNumArray_SumRange(t *testing.T) {
FILE: algorithm/leetcode/326/ac326.go
function isPowerOfThree (line 4) | func isPowerOfThree(n int) bool {
FILE: algorithm/leetcode/326/ac326_test.go
function Test_isPowerOfThree (line 5) | func Test_isPowerOfThree(t *testing.T) {
FILE: algorithm/leetcode/342/ac342.go
function isPowerOfFour (line 7) | func isPowerOfFour(num int) bool {
FILE: algorithm/leetcode/344/ac344.go
function reverseString (line 3) | func reverseString(s string) string {
FILE: algorithm/leetcode/344/ac344_test.go
function Test_reverseString (line 8) | func Test_reverseString(t *testing.T) {
FILE: algorithm/leetcode/345/ac345.go
function reverseVowels (line 5) | func reverseVowels(s string) string {
FILE: algorithm/leetcode/345/ac345_test.go
function Test_reverseVowels (line 8) | func Test_reverseVowels(t *testing.T) {
FILE: algorithm/leetcode/349/ac349.go
function intersection (line 3) | func intersection(nums1 []int, nums2 []int) []int {
function intersection2 (line 29) | func intersection2(nums1 []int, nums2 []int) []int {
FILE: algorithm/leetcode/349/ac349_test.go
function Test_intersection (line 8) | func Test_intersection(t *testing.T) {
FILE: algorithm/leetcode/350/ac350.go
function intersect (line 3) | func intersect(nums1 []int, nums2 []int) []int {
function minInt (line 32) | func minInt(a, b int) int {
FILE: algorithm/leetcode/350/ac350_test.go
function Test_intersect (line 8) | func Test_intersect(t *testing.T) {
FILE: algorithm/leetcode/367/ac367.go
function isPerfectSquare (line 3) | func isPerfectSquare(num int) bool {
function isPerfectSquare2 (line 20) | func isPerfectSquare2(num int) bool {
FILE: algorithm/leetcode/371/ac371.go
function getSum (line 3) | func getSum(a int, b int) int {
FILE: algorithm/leetcode/383/ac383.go
function canConstruct (line 3) | func canConstruct(ransomNote string, magazine string) bool {
FILE: algorithm/leetcode/383/ac383_test.go
function Test_canConstruct (line 8) | func Test_canConstruct(t *testing.T) {
FILE: algorithm/leetcode/387/ac387.go
function firstUniqChar (line 3) | func firstUniqChar(s string) int {
function firstUniqChar2 (line 23) | func firstUniqChar2(s string) int {
FILE: algorithm/leetcode/389/ac389.go
function findTheDifference (line 3) | func findTheDifference(s string, t string) byte {
function findTheDifference2 (line 23) | func findTheDifference2(s string, t string) byte {
FILE: algorithm/leetcode/389/ac389_test.go
function Test_findTheDifference (line 8) | func Test_findTheDifference(t *testing.T) {
FILE: algorithm/leetcode/400/ac400.go
function findNthDigit (line 5) | func findNthDigit(n int) int {
FILE: algorithm/leetcode/400/ac400_test.go
function Test_findNthDigit (line 8) | func Test_findNthDigit(t *testing.T) {
FILE: algorithm/leetcode/404/ac404.go
type TreeNode (line 3) | type TreeNode struct
function sumOfLeftLeaves (line 9) | func sumOfLeftLeaves(root *TreeNode) int {
FILE: algorithm/leetcode/405/ac405.go
function toHex (line 3) | func toHex(num int) string {
FILE: algorithm/leetcode/405/ac405_test.go
function Test_toHex (line 8) | func Test_toHex(t *testing.T) {
FILE: algorithm/leetcode/409/ac409.go
function longestPalindrome (line 8) | func longestPalindrome(s string) int {
FILE: algorithm/leetcode/409/ac409_test.go
function Test_longestPalindrome (line 8) | func Test_longestPalindrome(t *testing.T) {
FILE: algorithm/leetcode/412/ac412.go
function fizzBuzz (line 8) | func fizzBuzz(n int) []string {
function number (line 17) | func number(n int) string {
function fizzBuzz2 (line 29) | func fizzBuzz2(n int) []string {
FILE: algorithm/leetcode/412/ac412_test.go
function Test_fizzBuzz (line 8) | func Test_fizzBuzz(t *testing.T) {
FILE: algorithm/leetcode/414/ac414.go
function thirdMax (line 7) | func thirdMax(nums []int) int {
function thirdMax2 (line 30) | func thirdMax2(nums []int) int {
FILE: algorithm/leetcode/414/ac414_test.go
function Test_thirdMax (line 8) | func Test_thirdMax(t *testing.T) {
FILE: algorithm/leetcode/415/ac415.go
function addStrings (line 3) | func addStrings(num1 string, num2 string) string {
function addStrings2 (line 29) | func addStrings2(num1 string, num2 string) string {
FILE: algorithm/leetcode/415/ac415_test.go
function Test_addStrings (line 8) | func Test_addStrings(t *testing.T) {
FILE: algorithm/leetcode/434/ac434.go
function countSegments (line 3) | func countSegments(s string) int {
FILE: algorithm/leetcode/437/ac437.go
type TreeNode (line 3) | type TreeNode struct
function pathSum (line 9) | func pathSum(root *TreeNode, sum int) int {
function pathSumFrom (line 16) | func pathSumFrom(node *TreeNode, sum int) int {
FILE: algorithm/leetcode/438/ac438.go
function findAnagrams (line 3) | func findAnagrams(s string, p string) []int {
FILE: algorithm/leetcode/438/ac438_test.go
function Test_findAnagrams (line 8) | func Test_findAnagrams(t *testing.T) {
FILE: algorithm/leetcode/441/ac441.go
function arrangeCoins (line 16) | func arrangeCoins(n int) int {
FILE: algorithm/leetcode/441/ac441_test.go
function Test_arrangeCoins (line 8) | func Test_arrangeCoins(t *testing.T) {
FILE: algorithm/leetcode/443/ac443.go
function compress (line 5) | func compress(chars []byte) int {
FILE: algorithm/leetcode/443/ac443_test.go
function Test_compress (line 8) | func Test_compress(t *testing.T) {
FILE: algorithm/leetcode/447/ac447.go
function numberOfBoomerangs (line 3) | func numberOfBoomerangs(points [][]int) int {
function getDistance (line 25) | func getDistance(a, b []int) int {
FILE: algorithm/leetcode/448/ac448.go
function findDisappearedNumbers (line 3) | func findDisappearedNumbers(nums []int) []int {
function abs (line 21) | func abs(n int) int {
FILE: algorithm/leetcode/453/ac453.go
function minMoves (line 3) | func minMoves(nums []int) int {
FILE: algorithm/leetcode/453/ac453_test.go
function Test_minMoves (line 8) | func Test_minMoves(t *testing.T) {
FILE: algorithm/leetcode/455/ac455.go
function findContentChildren (line 5) | func findContentChildren(g []int, s []int) int {
FILE: algorithm/leetcode/455/ac455_test.go
function Test_findContentChildren (line 8) | func Test_findContentChildren(t *testing.T) {
FILE: algorithm/leetcode/458/ac458.go
function poorPigs (line 31) | func poorPigs(buckets int, minutesToDie int, minutesToTest int) int {
function mypow (line 39) | func mypow(a, b int) int {
FILE: algorithm/leetcode/459/ac459.go
function repeatedSubstringPattern (line 5) | func repeatedSubstringPattern(s string) bool {
FILE: algorithm/leetcode/459/ac459_test.go
function Test_repeatedSubstringPattern (line 8) | func Test_repeatedSubstringPattern(t *testing.T) {
FILE: algorithm/leetcode/461/ac461.go
function hammingDistance (line 3) | func hammingDistance(x int, y int) int {
FILE: algorithm/leetcode/463/ac463.go
function islandPerimeter (line 3) | func islandPerimeter(grid [][]int) int {
FILE: algorithm/leetcode/463/ac463_test.go
function Test_islandPerimeter (line 8) | func Test_islandPerimeter(t *testing.T) {
FILE: algorithm/leetcode/475/ac475.go
function findRadius (line 5) | func findRadius(houses []int, heaters []int) int {
function maxInt (line 19) | func maxInt(a, b int) int {
function absInt (line 26) | func absInt(a int) int {
FILE: algorithm/leetcode/475/ac475_test.go
function Test_findRadius (line 8) | func Test_findRadius(t *testing.T) {
FILE: algorithm/leetcode/476/ac476.go
function findComplement (line 3) | func findComplement(num int) int {
FILE: algorithm/leetcode/476/ac476_test.go
function Test_findComplement (line 8) | func Test_findComplement(t *testing.T) {
FILE: algorithm/leetcode/479/ac479.go
function largestPalindrome (line 8) | func largestPalindrome(n int) int {
function reverseInt (line 25) | func reverseInt(a int) string {
FILE: algorithm/leetcode/479/ac479_test.go
function Test_largestPalindrome (line 8) | func Test_largestPalindrome(t *testing.T) {
FILE: algorithm/leetcode/485/ac485.go
function findMaxConsecutiveOnes (line 3) | func findMaxConsecutiveOnes(nums []int) int {
FILE: algorithm/leetcode/485/ac485_test.go
function Test_findMaxConsecutiveOnes (line 8) | func Test_findMaxConsecutiveOnes(t *testing.T) {
FILE: algorithm/leetcode/492/ac492.go
function constructRectangle (line 5) | func constructRectangle(area int) []int {
FILE: algorithm/leetcode/492/ac492_test.go
function Test_constructRectangle (line 8) | func Test_constructRectangle(t *testing.T) {
FILE: algorithm/leetcode/496/ac496.go
function nextGreaterElement (line 3) | func nextGreaterElement(findNums []int, nums []int) []int {
function nextMax (line 11) | func nextMax(n int, nums []int) int {
FILE: algorithm/leetcode/496/ac496_test.go
function Test_nextGreaterElement (line 8) | func Test_nextGreaterElement(t *testing.T) {
FILE: algorithm/leetcode/500/ac500.go
function findWords (line 5) | func findWords(words []string) []string {
FILE: algorithm/leetcode/500/ac500_test.go
function Test_findWords (line 8) | func Test_findWords(t *testing.T) {
FILE: algorithm/leetcode/501/ac501.go
type TreeNode (line 3) | type TreeNode struct
function findMode (line 9) | func findMode(root *TreeNode) []int {
function traverse (line 20) | func traverse(root *TreeNode, ans *[]int, max, count, pre *int) {
FILE: algorithm/leetcode/501/ac501_test.go
function Test_findMode (line 8) | func Test_findMode(t *testing.T) {
FILE: algorithm/leetcode/504/ac504.go
function convertToBase7 (line 7) | func convertToBase7(num int) string {
FILE: algorithm/leetcode/504/ac504_test.go
function Test_convertToBase7 (line 8) | func Test_convertToBase7(t *testing.T) {
FILE: algorithm/leetcode/506/ac506.go
type person (line 8) | type person struct
type persons (line 13) | type persons
method Len (line 16) | func (p persons) Len() int {
method Less (line 21) | func (p persons) Less(i, j int) bool {
method Swap (line 27) | func (p persons) Swap(i, j int) {
function findRelativeRanks (line 31) | func findRelativeRanks(nums []int) []string {
FILE: algorithm/leetcode/506/ac506_test.go
function Test_findRelativeRanks (line 8) | func Test_findRelativeRanks(t *testing.T) {
FILE: algorithm/leetcode/507/ac507.go
function checkPerfectNumber (line 5) | func checkPerfectNumber(num int) bool {
FILE: algorithm/leetcode/507/ac507_test.go
function Test_checkPerfectNumber (line 8) | func Test_checkPerfectNumber(t *testing.T) {
FILE: algorithm/leetcode/520/ac520.go
function detectCapitalUse (line 3) | func detectCapitalUse(word string) bool {
function detectCapitalUse2 (line 37) | func detectCapitalUse2(word string) bool {
FILE: algorithm/leetcode/520/ac520_test.go
function Test_detectCapitalUse (line 8) | func Test_detectCapitalUse(t *testing.T) {
FILE: algorithm/leetcode/521/ac521.go
function findLUSlength (line 3) | func findLUSlength(a string, b string) int {
FILE: algorithm/leetcode/530/ac530.go
type TreeNode (line 5) | type TreeNode struct
function getMinimumDifference (line 11) | func getMinimumDifference(root *TreeNode) int {
function core (line 21) | func core(root *TreeNode, nums []int) int {
function myMin (line 37) | func myMin(a, b int) int {
FILE: algorithm/leetcode/530/ac530_test.go
function Test_getMinimumDifference (line 8) | func Test_getMinimumDifference(t *testing.T) {
FILE: algorithm/leetcode/532/ac532.go
function findPairs (line 3) | func findPairs(nums []int, k int) int {
FILE: algorithm/leetcode/532/ac532_test.go
function Test_findPairs (line 8) | func Test_findPairs(t *testing.T) {
FILE: algorithm/leetcode/538/ac538.go
type TreeNode (line 3) | type TreeNode struct
function convertBST (line 9) | func convertBST(root *TreeNode) *TreeNode {
function convertBSTCore (line 19) | func convertBSTCore(root *TreeNode, sums *int) {
FILE: algorithm/leetcode/541/ac541.go
function reverseStr (line 3) | func reverseStr(s string, k int) string {
FILE: algorithm/leetcode/541/ac541_test.go
function Test_reverseStr (line 8) | func Test_reverseStr(t *testing.T) {
FILE: algorithm/leetcode/543/ac543.go
type TreeNode (line 3) | type TreeNode struct
function diameterOfBinaryTree (line 11) | func diameterOfBinaryTree(root *TreeNode) int {
function depthOfTree (line 26) | func depthOfTree(node *TreeNode, diameter *int) int {
function myMax (line 41) | func myMax(a, b int) int {
FILE: algorithm/leetcode/551/ac551.go
function checkRecord (line 5) | func checkRecord(s string) bool {
function checkRecord2 (line 25) | func checkRecord2(s string) bool {
FILE: algorithm/leetcode/551/ac551_test.go
function Test_checkRecord (line 8) | func Test_checkRecord(t *testing.T) {
FILE: algorithm/leetcode/557/ac557.go
function reverseWords (line 3) | func reverseWords(s string) string {
function reverse (line 19) | func reverse(chars []rune, start, end int) {
FILE: algorithm/leetcode/557/ac557_test.go
function Test_reverseWords (line 8) | func Test_reverseWords(t *testing.T) {
FILE: algorithm/leetcode/561/ac561.go
function arrayPairSum (line 5) | func arrayPairSum(nums []int) int {
function arrayPairSum2 (line 16) | func arrayPairSum2(nums []int) int {
FILE: algorithm/leetcode/561/ac561_test.go
function Test_arrayPairSum (line 8) | func Test_arrayPairSum(t *testing.T) {
FILE: algorithm/leetcode/563/ac563.go
type TreeNode (line 3) | type TreeNode struct
function findTilt (line 9) | func findTilt(root *TreeNode) int {
function helper (line 15) | func helper(root *TreeNode, ret []int) int {
function myAbs (line 27) | func myAbs(a, b int) int {
FILE: algorithm/leetcode/566/ac566.go
function matrixReshape (line 3) | func matrixReshape(nums [][]int, r int, c int) [][]int {
function matrixReshape2 (line 26) | func matrixReshape2(nums [][]int, r int, c int) [][]int {
FILE: algorithm/leetcode/566/ac566_test.go
function Test_matrixReshape (line 8) | func Test_matrixReshape(t *testing.T) {
FILE: algorithm/leetcode/572/ac572.go
type TreeNode (line 3) | type TreeNode struct
function isSubtree (line 9) | func isSubtree(s *TreeNode, t *TreeNode) bool {
function isSame (line 18) | func isSame(t1, t2 *TreeNode) bool {
FILE: algorithm/leetcode/575/ac575.go
function distributeCandies (line 3) | func distributeCandies(candies []int) int {
FILE: algorithm/leetcode/575/ac575_test.go
function Test_distributeCandies (line 8) | func Test_distributeCandies(t *testing.T) {
FILE: algorithm/leetcode/581/ac581.go
function findUnsortedSubarray (line 5) | func findUnsortedSubarray(nums []int) int {
function findUnsortedSubarray2 (line 28) | func findUnsortedSubarray2(nums []int) int {
function myMax (line 49) | func myMax(a, b int) int {
function myMin (line 56) | func myMin(a, b int) int {
FILE: algorithm/leetcode/581/ac581_test.go
function Test_findUnsortedSubarray (line 8) | func Test_findUnsortedSubarray(t *testing.T) {
FILE: algorithm/leetcode/594/ac594.go
function findLHS (line 4) | func findLHS(nums []int) int {
function myMax (line 19) | func myMax(a, b int) int {
FILE: algorithm/leetcode/594/ac594_test.go
function Test_findLHS (line 8) | func Test_findLHS(t *testing.T) {
FILE: algorithm/leetcode/598/ac598.go
function maxCount (line 3) | func maxCount(m int, n int, ops [][]int) int {
function myMin (line 16) | func myMin(a, b int) int {
FILE: algorithm/leetcode/598/ac598_test.go
function Test_maxCount (line 8) | func Test_maxCount(t *testing.T) {
FILE: algorithm/leetcode/599/ac599.go
function findRestaurant (line 3) | func findRestaurant(list1 []string, list2 []string) []string {
FILE: algorithm/leetcode/599/ac599_test.go
function Test_findRestaurant (line 8) | func Test_findRestaurant(t *testing.T) {
FILE: algorithm/leetcode/605/ac605.go
function canPlaceFlowers (line 3) | func canPlaceFlowers(flowerbed []int, n int) bool {
FILE: algorithm/leetcode/605/ac605_test.go
function Test_canPlaceFlowers (line 8) | func Test_canPlaceFlowers(t *testing.T) {
FILE: algorithm/leetcode/606/ac606.go
type TreeNode (line 5) | type TreeNode struct
function tree2str (line 11) | func tree2str(t *TreeNode) string {
FILE: algorithm/leetcode/617/ac617.go
type TreeNode (line 3) | type TreeNode struct
function mergeTrees (line 9) | func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
FILE: algorithm/leetcode/628/ac628.go
function maximumProduct (line 8) | func maximumProduct(nums []int) int {
function maximumProduct2 (line 30) | func maximumProduct2(nums []int) int {
function myMax (line 59) | func myMax(a, b int) int {
FILE: algorithm/leetcode/633/ac633.go
function judgeSquareSum (line 5) | func judgeSquareSum(c int) bool {
FILE: algorithm/leetcode/637/ac637.go
type TreeNode (line 3) | type TreeNode struct
function averageOfLevels (line 9) | func averageOfLevels(root *TreeNode) []float64 {
FILE: algorithm/leetcode/643/ac643.go
function findMaxAverage (line 5) | func findMaxAverage(nums []int, k int) float64 {
FILE: algorithm/leetcode/643/ac643_test.go
function Test_findMaxAverage (line 8) | func Test_findMaxAverage(t *testing.T) {
FILE: algorithm/leetcode/645/ac645.go
function findErrorNums (line 3) | func findErrorNums(nums []int) []int {
function myAbs (line 26) | func myAbs(a int) int {
FILE: algorithm/leetcode/645/ac645_test.go
function Test_findErrorNums (line 8) | func Test_findErrorNums(t *testing.T) {
FILE: algorithm/leetcode/653/ac653.go
type TreeNode (line 3) | type TreeNode struct
function findTarget (line 9) | func findTarget(root *TreeNode, k int) bool {
function dfs (line 18) | func dfs(node *TreeNode, k int, dict map[int]bool) bool {
FILE: algorithm/leetcode/653/ac653_test.go
function Test_findTarget (line 8) | func Test_findTarget(t *testing.T) {
FILE: algorithm/leetcode/657/ac657.go
type Position (line 5) | type Position struct
function judgeCircle (line 9) | func judgeCircle(moves string) bool {
function judgeCircle2 (line 32) | func judgeCircle2(moves string) bool {
FILE: algorithm/leetcode/657/ac657_test.go
function Test_judgeCircle (line 8) | func Test_judgeCircle(t *testing.T) {
FILE: algorithm/leetcode/661/ac661.go
function imageSmoother (line 3) | func imageSmoother(M [][]int) [][]int {
function smooth (line 20) | func smooth(M [][]int, x, y int) int {
FILE: algorithm/leetcode/661/ac661_test.go
function Test_imageSmoother (line 8) | func Test_imageSmoother(t *testing.T) {
FILE: algorithm/leetcode/665/ac665.go
function checkPossibility (line 3) | func checkPossibility(nums []int) bool {
FILE: algorithm/leetcode/665/ac665_test.go
function Test_checkPossibility (line 8) | func Test_checkPossibility(t *testing.T) {
FILE: algorithm/leetcode/669/ac669.go
type TreeNode (line 3) | type TreeNode struct
function trimBST (line 9) | func trimBST(root *TreeNode, L int, R int) *TreeNode {
FILE: algorithm/leetcode/671/ac671.go
type TreeNode (line 3) | type TreeNode struct
function findSecondMinimumValue (line 9) | func findSecondMinimumValue(root *TreeNode) int {
function findCore (line 17) | func findCore(node *TreeNode, min int) int {
function myMin (line 34) | func myMin(a, b int) int {
function myMax (line 41) | func myMax(a, b int) int {
FILE: algorithm/leetcode/671/ac671_test.go
function Test_findSecondMinimumValue (line 8) | func Test_findSecondMinimumValue(t *testing.T) {
FILE: algorithm/leetcode/674/ac674.go
function findLengthOfLCIS (line 3) | func findLengthOfLCIS(nums []int) int {
FILE: algorithm/leetcode/674/ac674_test.go
function Test_findLengthOfLCIS (line 8) | func Test_findLengthOfLCIS(t *testing.T) {
FILE: algorithm/leetcode/680/ac680.go
function validPalindrome (line 3) | func validPalindrome(s string) bool {
function isPalindromic (line 15) | func isPalindromic(s string, l, r int) bool {
FILE: algorithm/leetcode/682/ac682.go
function calPoints (line 5) | func calPoints(ops []string) int {
FILE: algorithm/leetcode/682/ac682_test.go
function Test_calPoints (line 8) | func Test_calPoints(t *testing.T) {
FILE: algorithm/leetcode/686/ac686.go
function repeatedStringMatch (line 5) | func repeatedStringMatch(A string, B string) int {
function repeatedStringMatch2 (line 20) | func repeatedStringMatch2(A string, B string) int {
FILE: algorithm/leetcode/686/ac686_test.go
function Test_repeatedStringMatch (line 8) | func Test_repeatedStringMatch(t *testing.T) {
FILE: algorithm/leetcode/687/ac687.go
type TreeNode (line 3) | type TreeNode struct
function longestUnivaluePath (line 9) | func longestUnivaluePath(root *TreeNode) int {
function dfs (line 17) | func dfs(node *TreeNode, res []int) int {
FILE: algorithm/leetcode/693/ac693.go
function hasAlternatingBits (line 8) | func hasAlternatingBits(n int) bool {
function decimalToBinary (line 20) | func decimalToBinary(n int) []int {
function hasAlternatingBits2 (line 33) | func hasAlternatingBits2(n int) bool {
FILE: algorithm/leetcode/693/ac693_test.go
function Test_hasAlternatingBits2 (line 8) | func Test_hasAlternatingBits2(t *testing.T) {
FILE: algorithm/leetcode/695/ac695.go
function maxAreaOfIsland (line 3) | func maxAreaOfIsland(grid [][]int) int {
function AreaOfIsland (line 18) | func AreaOfIsland(grid [][]int, i, j int) int {
FILE: algorithm/leetcode/696/ac696.go
function countBinarySubstrings (line 10) | func countBinarySubstrings(s string) int {
FILE: algorithm/leetcode/697/ac697.go
function findShortestSubArray (line 3) | func findShortestSubArray(nums []int) int {
FILE: algorithm/leetcode/717/ac717.go
function isOneBitCharacter (line 8) | func isOneBitCharacter(bits []int) bool {
FILE: algorithm/leetcode/724/ac724.go
function pivotIndex (line 3) | func pivotIndex(nums []int) int {
FILE: algorithm/leetcode/728/ac728.go
function selfDividingNumbers (line 3) | func selfDividingNumbers(left int, right int) []int {
FILE: algorithm/leetcode/744/ac744.go
function nextGreatestLetter (line 3) | func nextGreatestLetter(letters []byte, target byte) byte {
FILE: algorithm/leetcode/746/ac746.go
function minCostClimbingStairs (line 3) | func minCostClimbingStairs(cost []int) int {
function myMin (line 10) | func myMin(a, b int) int {
FILE: algorithm/leetcode/747/ac747.go
function dominantIndex (line 3) | func dominantIndex(nums []int) int {
FILE: algorithm/leetcode/747/ac747_test.go
function Test_dominantIndex (line 8) | func Test_dominantIndex(t *testing.T) {
FILE: algorithm/leetcode/762/ac762.go
function countPrimeSetBits (line 3) | func countPrimeSetBits(L, R int) int {
FILE: algorithm/leetcode/766/ac766.go
function isToeplitzMatrix (line 4) | func isToeplitzMatrix(matrix [][]int) bool {
FILE: algorithm/leetcode/766/ac766_test.go
function Test_isToeplitzMatrix (line 8) | func Test_isToeplitzMatrix(t *testing.T) {
FILE: algorithm/leetcode/771/ac771.go
function numJewelsInStones (line 3) | func numJewelsInStones(J string, S string) int {
FILE: algorithm/leetcode/771/ac771_test.go
function Test_numJewelsInStones (line 8) | func Test_numJewelsInStones(t *testing.T) {
FILE: algorithm/leetcode/783/ac783.go
type TreeNode (line 5) | type TreeNode struct
function minDiffInBST (line 12) | func minDiffInBST(root *TreeNode) int {
function check (line 20) | func check(node *TreeNode, nums []int) {
function myMin (line 32) | func myMin(a, b int) int {
FILE: algorithm/leetcode/783/ac783_test.go
function Test_minDiffInBST (line 8) | func Test_minDiffInBST(t *testing.T) {
FILE: algorithm/leetcode/784/ac784.go
function letterCasePermutation (line 5) | func letterCasePermutation(S string) []string {
function core (line 13) | func core(ans []string, chars []byte, index int) []string {
function isLetter (line 27) | func isLetter(c byte) bool {
function toLower (line 34) | func toLower(c byte) byte {
function toUpper (line 42) | func toUpper(c byte) byte {
FILE: algorithm/leetcode/784/ac784_test.go
function Test_letterCasePermutation (line 8) | func Test_letterCasePermutation(t *testing.T) {
FILE: algorithm/leetcode/788/ac788.go
function rotatedDigits (line 3) | func rotatedDigits(N int) int {
function isValid (line 13) | func isValid(n int) bool {
FILE: algorithm/leetcode/796/ac796.go
function rotateString (line 6) | func rotateString(A string, B string) bool {
FILE: algorithm/leetcode/804/ac804.go
function uniqueMorseRepresentations (line 3) | func uniqueMorseRepresentations(words []string) int {
FILE: algorithm/leetcode/804/ac804_test.go
function Test_uniqueMorseRepresentations (line 8) | func Test_uniqueMorseRepresentations(t *testing.T) {
FILE: algorithm/leetcode/811/ac811.go
function subdomainVisits (line 8) | func subdomainVisits(cpdomains []string) []string {
FILE: algorithm/leetcode/811/ac811_test.go
function Test_subdomainVisits (line 8) | func Test_subdomainVisits(t *testing.T) {
FILE: algorithm/leetcode/812/ac812.go
function largestTriangleArea (line 7) | func largestTriangleArea(points [][]int) float64 {
FILE: algorithm/leetcode/812/ac812_test.go
function Test_largestTriangleArea (line 8) | func Test_largestTriangleArea(t *testing.T) {
FILE: algorithm/leetcode/819/ac819.go
function mostCommonWord (line 8) | func mostCommonWord(paragraph string, banned []string) string {
function isContain (line 33) | func isContain(words []string, word string) bool {
FILE: algorithm/leetcode/819/ac819_test.go
function Test_mostCommonWord (line 8) | func Test_mostCommonWord(t *testing.T) {
FILE: algorithm/leetcode/821/ac821.go
function shortestToChar (line 5) | func shortestToChar(S string, C byte) []int {
function getMinDistance (line 26) | func getMinDistance(position []int, p int) int {
function myAbs (line 37) | func myAbs(a int) int {
FILE: algorithm/leetcode/821/ac821_test.go
function Test_shortestToChar (line 8) | func Test_shortestToChar(t *testing.T) {
FILE: algorithm/leetcode/824/ac824.go
function toGoatLatin (line 8) | func toGoatLatin(S string) string {
FILE: algorithm/leetcode/824/ac824_test.go
function Test_toGoatLatin (line 8) | func Test_toGoatLatin(t *testing.T) {
FILE: algorithm/leetcode/829/ac829.go
function consecutiveNumbersSum (line 6) | func consecutiveNumbersSum(N int) int {
FILE: algorithm/leetcode/829/ac829_test.go
function Test_consecutiveNumbersSum (line 8) | func Test_consecutiveNumbersSum(t *testing.T) {
FILE: algorithm/leetcode/830/ac830.go
function largeGroupPositions (line 3) | func largeGroupPositions(S string) [][]int {
FILE: algorithm/leetcode/830/ac830_test.go
function Test_largeGroupPositions (line 8) | func Test_largeGroupPositions(t *testing.T) {
FILE: algorithm/leetcode/832/ac832.go
function flipAndInvertImage (line 3) | func flipAndInvertImage(A [][]int) [][]int {
function reverse (line 11) | func reverse(array []int) {
function invert (line 19) | func invert(array []int) {
FILE: algorithm/leetcode/836/ac836.go
function isRectangleOverlap (line 5) | func isRectangleOverlap(rec1 []int, rec2 []int) bool {
FILE: algorithm/leetcode/840/ac840.go
function numMagicSquaresInside (line 3) | func numMagicSquaresInside(grid [][]int) int {
FILE: algorithm/leetcode/844/ac844.go
function backspaceCompare (line 3) | func backspaceCompare(S string, T string) bool {
function core (line 13) | func core(s string) string {
FILE: algorithm/sfo/30/ac30.go
function push (line 13) | func push(number int) {
function pop (line 30) | func pop() {
function min (line 37) | func min() (int, error) {
FILE: algorithm/sfo/30/ac30_test.go
function TestOk (line 8) | func TestOk(t *testing.T) {
FILE: algorithm/sfo/31/ac31.go
function isPopOrder (line 5) | func isPopOrder(pushStack, popStack []int) bool {
function Ma (line 38) | func Ma(){
FILE: algorithm/sfo/31/ac31_test.go
function Test_isPopOrder (line 8) | func Test_isPopOrder(t *testing.T) {
FILE: algorithm/sfo/32/ac32.go
type binaryTreeNode (line 8) | type binaryTreeNode struct
function breadthFirstSearch (line 14) | func breadthFirstSearch(root *binaryTreeNode) {
function breadthFirstSearch2 (line 35) | func breadthFirstSearch2(root *binaryTreeNode) {
FILE: algorithm/sfo/32/ac32_test.go
function Test_breadthFirstSearch (line 5) | func Test_breadthFirstSearch(t *testing.T) {
FILE: algorithm/sfo/33/ac33.go
function verifySquenceOfBST (line 4) | func verifySquenceOfBST(sequence []int, begin, end int) bool {
FILE: algorithm/sfo/33/ac33_test.go
function Test_verifySquenceOfBST (line 8) | func Test_verifySquenceOfBST(t *testing.T) {
FILE: algorithm/sfo/34/ac34.go
type binaryTreeNode (line 8) | type binaryTreeNode struct
function findPath (line 14) | func findPath(root *binaryTreeNode, expectedSum int) {
function findPathCore (line 23) | func findPathCore(root *binaryTreeNode, stack *arraystack.Stack, expecte...
FILE: algorithm/sfo/34/ac34_test.go
function Test_findPath (line 7) | func Test_findPath(t *testing.T) {
FILE: algorithm/sfo/35/ac35.go
type complexListNode (line 3) | type complexListNode struct
function cloneNodes (line 9) | func cloneNodes(head *complexListNode) {
function connectSiblingNodes (line 21) | func connectSiblingNodes(head *complexListNode) {
function reconnectNodes (line 32) | func reconnectNodes(head *complexListNode) *complexListNode {
function clone (line 51) | func clone(pHead *complexListNode) *complexListNode {
FILE: algorithm/sfo/36/ac36.go
type binaryTreeNode (line 3) | type binaryTreeNode struct
function conver (line 8) | func conver(root *binaryTreeNode) *binaryTreeNode {
function convertNode (line 20) | func convertNode(node *binaryTreeNode, lastNodeInList **binaryTreeNode) {
FILE: algorithm/sfo/36/ac36_test.go
function Test_conver (line 8) | func Test_conver(t *testing.T) {
FILE: algorithm/sfo/38/ac38.go
function permutation (line 5) | func permutation(arrayChar []byte, start int) {
function swap (line 23) | func swap(array []byte, m, n int) {
FILE: algorithm/sfo/38/ac38_test.go
function Test_permutation (line 5) | func Test_permutation(t *testing.T) {
FILE: algorithm/sfo/39/ac39.go
function moreThanHalfNum (line 3) | func moreThanHalfNum(numbers []int) int {
FILE: algorithm/sfo/39/ac39_test.go
function Test_moreThanHalfNum (line 8) | func Test_moreThanHalfNum(t *testing.T) {
FILE: algorithm/sfo/40/ac40.go
function getLeastNumbers (line 5) | func getLeastNumbers(data []int, k int) []int {
FILE: algorithm/sfo/40/ac40_test.go
function Test_getLeastNumbers (line 8) | func Test_getLeastNumbers(t *testing.T) {
FILE: algorithm/sfo/42/ac42.go
function findGreatestSumOfSubArray (line 3) | func findGreatestSumOfSubArray(data []int) (int, bool) {
FILE: algorithm/sfo/42/ac42_test.go
function Test_findGreatestSumOfSubArray (line 8) | func Test_findGreatestSumOfSubArray(t *testing.T) {
FILE: algorithm/sfo/43/ac43.go
function numberOf1Between1AndN (line 4) | func numberOf1Between1AndN(n int) int {
FILE: algorithm/sfo/43/ac43_test.go
function Test_numberOf1Between1AndN (line 8) | func Test_numberOf1Between1AndN(t *testing.T) {
FILE: algorithm/sfo/44/ac44.go
function countOfIntegers (line 5) | func countOfIntegers(digits int) int {
function beginNumber (line 12) | func beginNumber(digits int) int {
function digitAtIndexCore (line 19) | func digitAtIndexCore(index, digits int) int {
function digitAtIndex (line 28) | func digitAtIndex(index int) int {
FILE: algorithm/sfo/44/ac44_test.go
function Test_digitAtIndex (line 8) | func Test_digitAtIndex(t *testing.T) {
FILE: algorithm/sfo/45/ac45.go
type intSlice (line 8) | type intSlice
method Len (line 10) | func (p intSlice) Len() int {
method Less (line 14) | func (p intSlice) Less(i, j int) bool {
method Swap (line 18) | func (p intSlice) Swap(i, j int) {
function printMinNumber (line 22) | func printMinNumber(numbers []int) string {
FILE: algorithm/sfo/45/ac45_test.go
function Test_printMinNumber (line 8) | func Test_printMinNumber(t *testing.T) {
FILE: algorithm/sfo/46/ac46.go
function getTranslation (line 5) | func getTranslation(number int) int {
function getTranslationCore (line 12) | func getTranslationCore(str string) int {
FILE: algorithm/sfo/46/ac46_test.go
function Test_getTranslation (line 8) | func Test_getTranslation(t *testing.T) {
FILE: algorithm/sfo/47/ac47.go
function getMaxValue (line 3) | func getMaxValue(matrix [][]int) int {
function getMaxValue2 (line 36) | func getMaxValue2(matrix [][]int) int {
FILE: algorithm/sfo/47/ac47_test.go
function Test_getMaxValue (line 8) | func Test_getMaxValue(t *testing.T) {
FILE: algorithm/sfo/48/ac48.go
function longestSubstring (line 3) | func longestSubstring(str string) int {
FILE: algorithm/sfo/48/ac48_test.go
function Test_longestSubstring (line 8) | func Test_longestSubstring(t *testing.T) {
FILE: algorithm/sfo/49/ac49.go
function getUglyNumber (line 3) | func getUglyNumber(index int) int {
function min3 (line 30) | func min3(num1, num2, num3 int) int {
function min (line 35) | func min(num1, num2 int) int {
FILE: algorithm/sfo/49/ac49_test.go
function Test_getUglyNumber (line 8) | func Test_getUglyNumber(t *testing.T) {
FILE: algorithm/sfo/50/ac50.go
function firstNoRepeatingChar (line 3) | func firstNoRepeatingChar(str string) byte {
FILE: algorithm/sfo/50/ac50_test.go
function Test_firstNoRepeatingChar (line 8) | func Test_firstNoRepeatingChar(t *testing.T) {
FILE: algorithm/sfo/51/ac51.go
function inversePairs (line 3) | func inversePairs(data []int) int {
function inversePairsCore (line 17) | func inversePairsCore(data, copy []int, start, end int) int {
FILE: algorithm/sfo/51/ac51_test.go
function Test_inversePairs (line 8) | func Test_inversePairs(t *testing.T) {
FILE: algorithm/sfo/52/ac52.go
type ListNode (line 3) | type ListNode struct
function findFirstCommonNode (line 8) | func findFirstCommonNode(head1, head2 *ListNode) *ListNode {
function getListLength (line 34) | func getListLength(head *ListNode) int {
FILE: algorithm/sfo/52/ac52_test.go
function Test_findFirstCommonNode (line 8) | func Test_findFirstCommonNode(t *testing.T) {
FILE: algorithm/sfo/53/ac53.go
function getFirstK (line 3) | func getFirstK(numbers []int, k, start, end int) int {
function getLastK (line 24) | func getLastK(numbers []int, k, start, end int) int {
function getNumberOfK (line 45) | func getNumberOfK(numbers []int, k int) int {
function getMissingNumber (line 57) | func getMissingNumber(numbers []int) int {
function getNumberSameAsIndex (line 79) | func getNumberSameAsIndex(numbers []int) int {
FILE: algorithm/sfo/53/ac53_test.go
function Test_getNumberOfK (line 8) | func Test_getNumberOfK(t *testing.T) {
FILE: algorithm/sfo/54/ac54.go
type BinaryTreeNode (line 3) | type BinaryTreeNode struct
function kthNode (line 8) | func kthNode(root *BinaryTreeNode, k int) *BinaryTreeNode {
FILE: algorithm/sfo/55/ac55.go
type BinaryTreeNode (line 3) | type BinaryTreeNode struct
function treeDepth (line 8) | func treeDepth(root *BinaryTreeNode) int {
function isBalanced (line 21) | func isBalanced(root *BinaryTreeNode, depth *int) bool {
function isBalance (line 41) | func isBalance(root *BinaryTreeNode) bool {
FILE: algorithm/sfo/55/ac55_test.go
function Test_treeDepth (line 8) | func Test_treeDepth(t *testing.T) {
FILE: algorithm/sfo/56/ac56.go
function findNumbersAppearOnce (line 8) | func findNumbersAppearOnce(data []int) (int, int) {
function findFirstBitIs1 (line 29) | func findFirstBitIs1(num int) uint {
function isBit1 (line 38) | func isBit1(num int, indexBit uint) bool {
function findNumberAppearOnce2 (line 46) | func findNumberAppearOnce2(numbers []int) int {
FILE: algorithm/sfo/56/ac56_test.go
function Test_findNumbersAppearOnce (line 8) | func Test_findNumbersAppearOnce(t *testing.T) {
FILE: algorithm/sfo/57/ac57.go
function findNumbers (line 5) | func findNumbers(data []int, sum int) (int, int, bool) {
function findContinuousSequence (line 26) | func findContinuousSequence(sum int) {
function printContinuousSequence (line 52) | func printContinuousSequence(small, big int) {
FILE: algorithm/sfo/57/ac57_test.go
function Test_findNumbers (line 8) | func Test_findNumbers(t *testing.T) {
FILE: algorithm/sfo/58/ac58.go
function reverse (line 3) | func reverse(str []rune) {
function reverseSentence (line 14) | func reverseSentence(str string) string {
function leftRotateString (line 40) | func leftRotateString(str string, n int) string {
FILE: algorithm/sfo/58/ac58_test.go
function Test_reverseSentence (line 8) | func Test_reverseSentence(t *testing.T) {
function Test_leftRotateString (line 14) | func Test_leftRotateString(t *testing.T) {
FILE: algorithm/sfo/59/ac59.go
function maxInWindows (line 7) | func maxInWindows(num []int, size int) []int {
FILE: algorithm/sfo/59/ac59_test.go
function Test_maxInWindows (line 8) | func Test_maxInWindows(t *testing.T) {
FILE: algorithm/sfo/61/ac60.go
function isContinuous (line 5) | func isContinuous(numbers []int) bool {
FILE: algorithm/sfo/61/ac60_test.go
function Test_isContinuous (line 8) | func Test_isContinuous(t *testing.T) {
FILE: algorithm/sfo/62/ac62.go
function lastRemaining (line 3) | func lastRemaining(n, m int) int {
FILE: algorithm/sfo/62/ac62_test.go
function Test_lastRemaining (line 8) | func Test_lastRemaining(t *testing.T) {
FILE: algorithm/sfo/63/ac63.go
function maxDiff (line 3) | func maxDiff(numbers []int) int {
FILE: algorithm/sfo/63/ac63_test.go
function Test_maxDiff (line 8) | func Test_maxDiff(t *testing.T) {
FILE: algorithm/sfo/65/ac64.go
function Add (line 3) | func Add(num1, num2 int) int {
FILE: algorithm/sfo/65/ac64_test.go
function TestAdd (line 8) | func TestAdd(t *testing.T) {
FILE: lib/BinaryTreeNode.go
type BinaryTreeNode (line 3) | type BinaryTreeNode struct
FILE: lib/GetLine.go
function GetLine (line 9) | func GetLine() string {
FILE: lib/Reverse.go
function Reverse (line 3) | func Reverse(data []interface{}) {
FILE: lib/grpool/Pool.go
type Pool (line 7) | type Pool struct
method Add (line 22) | func (p *Pool) Add(delta int) {
method Done (line 32) | func (p *Pool) Done() {
method WaitAll (line 37) | func (p *Pool) WaitAll() {
function New (line 12) | func New(size int) *Pool {
FILE: notes/defer/main.go
function main (line 10) | func main() {
FILE: notes/learn/main.go
function main (line 9) | func main() {
function f2 (line 32) | func f2(text string) {
function f1 (line 39) | func f1(arg int) (int, error) {
function modify (line 47) | func modify(slice []int) {
function test (line 52) | func test(a, b int, c float64) {
function testMap (line 56) | func testMap() {
function testArray (line 70) | func testArray() {
function IsEven (line 83) | func IsEven(number int) bool {
function IsEven2 (line 91) | func IsEven2(number int) bool {
function testEven (line 99) | func testEven() {
FILE: notes/main/main.go
function main (line 7) | func main() {
FILE: notes/nil/main.go
function main (line 7) | func main() {
FILE: notes/switch/main.go
function main (line 12) | func main() {
function test (line 35) | func test(x float64) {
function test2 (line 47) | func test2() {
FILE: notes/time/main.go
function main (line 8) | func main() {
FILE: notes/函数/main.go
function main (line 6) | func main() {
function Add (line 16) | func Add(x int, y int) int {
function add (line 20) | func add(x int, y int, z int) int {
function add2 (line 24) | func add2(x, y, z int) int {
function add3 (line 28) | func add3(x bool, y, z int) (bool, int) {
function swap (line 32) | func swap(x, y string) (string, string) {
function split (line 41) | func split(sum int) (x, y int) {
FILE: notes/判断/main.go
function main (line 8) | func main() {
function myPow (line 16) | func myPow(x, y, ans float64) float64 {
function Sqrt (line 26) | func Sqrt(x float64) float64 {
FILE: notes/协程/main.go
function main (line 10) | func main() {
function lowwerCase (line 54) | func lowwerCase(id int, group *grpool.Pool) {
function upperCase (line 66) | func upperCase(id int, group *grpool.Pool) {
FILE: notes/变量/main.go
function main (line 8) | func main() {
FILE: notes/命令行参数/iie.go
function usage (line 9) | func usage() {
function main (line 23) | func main() {
FILE: notes/导入导出/main.go
function main (line 11) | func main() {
FILE: notes/常量/main.go
constant PI (line 9) | PI = 3.1415926
function main (line 11) | func main(){
FILE: notes/循环/main.go
function main (line 5) | func main() {
FILE: notes/数组/main.go
type Currency (line 5) | type Currency
constant USD (line 9) | USD Currency = iota
constant EUR (line 11) | EUR
constant GBP (line 15) | GBP
constant RMB (line 19) | RMB
function main (line 25) | func main() {
FILE: notes/输入/main.go
function main (line 11) | func main() {
FILE: notes/输出/main.go
function main (line 100) | func main() {
function learnScan (line 104) | func learnScan(){
function learnPrintf (line 119) | func learnPrintf(){
Condensed preview — 348 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (207K chars).
[
{
"path": ".gitignore",
"chars": 293,
"preview": ".idea/\n.DS_Store\n\n# Binaries for programs and plugins\n*.exe\n*.dll\n*.so\n*.dylib\n\n# Test binary, build with `go test -c`\n*"
},
{
"path": "LICENSE",
"chars": 11357,
"preview": " Apache License\n Version 2.0, January 2004\n "
},
{
"path": "README.md",
"chars": 8660,
"preview": "## mygo\n\n> 此项目建议搭配[Code tree for GitHub](https://github.com/buunguyen/octotree)效果更佳。\n> \n> ##### 环境\n> \n> go1.10 darwin/am"
},
{
"path": "algorithm/leetcode/001/ac001.go",
"chars": 388,
"preview": "package problem001\n\nfunc twoSum(nums []int, target int) []int {\n\t// m 负责保存map[整数]整数的序列号\n\tm := make(map[int]int, len(nums"
},
{
"path": "algorithm/leetcode/001/ac001_test.go",
"chars": 760,
"preview": "package problem001\n\nimport (\n\t\"testing\"\n\t\"github.com/stretchr/testify/assert\"\n)\n\ntype argument struct {\n\tnumberArray []i"
},
{
"path": "algorithm/leetcode/007/ac007.go",
"chars": 233,
"preview": "package problem007\n\nimport \"math\"\n\nfunc reverse(x int) int {\n\tvar result int64\n\tfor 0 != x {\n\t\tresult = result*10 + int6"
},
{
"path": "algorithm/leetcode/007/ac007_test.go",
"chars": 653,
"preview": "package problem007\n\nimport (\n\t\"testing\"\n\t\"github.com/stretchr/testify/assert\"\n)\n\ntype argument struct {\n\tnumber int\n}\n\nt"
},
{
"path": "algorithm/leetcode/009/ac009.go",
"chars": 287,
"preview": "package problem009\n\n// 负数不是回文的\nfunc isPalindrome(x int) bool {\n\tif 0 > x || (0 == x%10 && 0 != x) {\n\t\treturn false\n\t}\n\n\t"
},
{
"path": "algorithm/leetcode/009/ac009_test.go",
"chars": 578,
"preview": "package problem009\n\nimport (\n\t\"testing\"\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc Test_isPalindrome(t *testing.T) {\n"
},
{
"path": "algorithm/leetcode/013/ac013.go",
"chars": 423,
"preview": "package problem013\n\nfunc romanToInt(s string) int {\n\troman := map[byte]int{\n\t\t'I': 1,\n\t\t'V': 5,\n\t\t'X': 10,\n\t\t'L': 50,\n\t\t"
},
{
"path": "algorithm/leetcode/013/ac013_test.go",
"chars": 515,
"preview": "package problem013\n\nimport (\n\t\"testing\"\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc Test_romanToInt(t *testing.T) {\n\tt"
},
{
"path": "algorithm/leetcode/014/ac014.go",
"chars": 382,
"preview": "package problem014\n\nimport (\n\t\"sort\"\n)\n\nfunc longestCommonPrefix(strs []string) string {\n\tif nil == strs || 0 >= len(str"
},
{
"path": "algorithm/leetcode/014/ac014_test.go",
"chars": 508,
"preview": "package problem014\n\nimport (\n\t\"testing\"\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc Test_longestCommonPrefix(t *testin"
},
{
"path": "algorithm/leetcode/020/ac020.go",
"chars": 427,
"preview": "package problem020\n\nimport \"github.com/emirpasic/gods/stacks/arraystack\"\n\nfunc isValid(s string) bool {\n\tstack := arrays"
},
{
"path": "algorithm/leetcode/020/ac020_test.go",
"chars": 512,
"preview": "package problem020\n\nimport (\n\t\"testing\"\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc Test_isValid(t *testing.T) {\n\ttype"
},
{
"path": "algorithm/leetcode/021/ac021.go",
"chars": 333,
"preview": "package problem021\n\ntype ListNode struct {\n\tVal int\n\tNext *ListNode\n}\n\nfunc mergeTwoLists(l1 *ListNode, l2 *ListNode) *"
},
{
"path": "algorithm/leetcode/026/ac026.go",
"chars": 267,
"preview": "package problem026\n\nfunc removeDuplicates(nums []int) int {\n\tif nil == nums || len(nums) < 1 {\n\t\treturn 0\n\t}\n\n\tnewLength"
},
{
"path": "algorithm/leetcode/027/ac027.go",
"chars": 238,
"preview": "package problem027\n\nfunc removeElement(nums []int, val int) int {\n\tif nil == nums || len(nums) < 1 {\n\t\treturn 0\n\t}\n\n\tvar"
},
{
"path": "algorithm/leetcode/028/ac028.go",
"chars": 287,
"preview": "package problem028\n\nfunc strStr(haystack string, needle string) int {\n\tif \"\" == needle {\n\t\treturn 0\n\t}\n\n\tfor i := 0; i <"
},
{
"path": "algorithm/leetcode/035/ac035.go",
"chars": 357,
"preview": "package problem035\n\nfunc searchInsert(nums []int, target int) int {\n\tif nil == nums || len(nums) < 1 {\n\t\treturn 0\n\t}\n\n\ts"
},
{
"path": "algorithm/leetcode/035/ac035_test.go",
"chars": 172,
"preview": "package problem035\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_searchInsert(t *testing.T) {\n\tdata := []int{1, 3, 5, 6}\n\ttar"
},
{
"path": "algorithm/leetcode/038/ac038.go",
"chars": 358,
"preview": "package problem038\n\nimport \"strconv\"\n\nfunc countAndSay(n int) string {\n\tif 0 == n {\n\t\treturn \"\"\n\t}\n\n\tans := \"1\"\n\tfor n--"
},
{
"path": "algorithm/leetcode/053/ac053.go",
"chars": 330,
"preview": "package problem053\n\nfunc maxSubArray(nums []int) int {\n\tif nil == nums || len(nums) < 1 {\n\t\treturn 0\n\t}\n\n\tpreSum, maxSum"
},
{
"path": "algorithm/leetcode/058/ac058.go",
"chars": 424,
"preview": "package problem058\n\nimport (\n\t\"strings\"\n)\n\nfunc lengthOfLastWord(s string) int {\n\tsubArray := strings.Split(s, \" \")\n\tfor"
},
{
"path": "algorithm/leetcode/058/ac058_test.go",
"chars": 552,
"preview": "package problem058\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_lengthOfLastWord(t *testing.T) {\n\ttext1 := \"Hello World\"\n\tte"
},
{
"path": "algorithm/leetcode/066/ac066.go",
"chars": 289,
"preview": "package problem066\n\nfunc plusOne(digits []int) []int {\n\tfor i := len(digits) - 1; i >= 0; i-- {\n\t\tif 9 != digits[i] {\n\t\t"
},
{
"path": "algorithm/leetcode/067/ac067.go",
"chars": 795,
"preview": "package problem067\n\nfunc addBinary(a string, b string) string {\n\taChar := []rune(a)\n\tbChar := []rune(b)\n\tvar s []rune\n\tv"
},
{
"path": "algorithm/leetcode/067/ac067_test.go",
"chars": 171,
"preview": "package problem067\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_addBinary(t *testing.T) {\n\ta := \"11\"\n\tb := \"01\"\n\tfmt.Println"
},
{
"path": "algorithm/leetcode/069/ac069.go",
"chars": 131,
"preview": "package problem069\n\nfunc mySqrt(x int) int {\n\tif 0 == x {\n\t\treturn 0\n\t}\n\n\tn := x\n\tfor n*n > x {\n\t\tn = (n + x/n) / 2\n\t}\n\t"
},
{
"path": "algorithm/leetcode/069/ac069_test.go",
"chars": 109,
"preview": "package problem069\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_mySqrt(t *testing.T) {\n\tfmt.Println(mySqrt(9))\n}\n"
},
{
"path": "algorithm/leetcode/070/ac070.go",
"chars": 293,
"preview": "package problem070\n\nfunc climbStairs(n int) int {\n\tif 1 == n {\n\t\treturn 1\n\t}\n\n\tf := make([]int, n+1)\n\tf[1], f[2] = 1, 2\n"
},
{
"path": "algorithm/leetcode/083/ac083.go",
"chars": 488,
"preview": "package problem083\n\ntype ListNode struct {\n\tVal int\n\tNext *ListNode\n}\n\nfunc deleteDuplicates(head *ListNode) *ListNode "
},
{
"path": "algorithm/leetcode/088/ac088.go",
"chars": 428,
"preview": "package problem088\n\nfunc merge(nums1 []int, m int, nums2 []int, n int) {\n\tidm, idn := m-1, n-1\n\tfor i := m + n - 1; i >="
},
{
"path": "algorithm/leetcode/100/ac100.go",
"chars": 356,
"preview": "package problem100\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc isSameTree(p *TreeNode, "
},
{
"path": "algorithm/leetcode/101/ac101.go",
"chars": 425,
"preview": "package problem101\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc isSymmetric(root *TreeNo"
},
{
"path": "algorithm/leetcode/104/ac104.go",
"chars": 293,
"preview": "package problem104\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc maxDepth(root *TreeNode)"
},
{
"path": "algorithm/leetcode/107/ac107.go",
"chars": 506,
"preview": "package problem107\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc levelOrderBottom(root *T"
},
{
"path": "algorithm/leetcode/107/ac107_test.go",
"chars": 421,
"preview": "package problem107\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_levelOrderBottom(t *testing.T) {\n\tnode15 := TreeNode{Val: 15"
},
{
"path": "algorithm/leetcode/108/ac108.go",
"chars": 320,
"preview": "package problem108\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc sortedArrayToBST(nums []"
},
{
"path": "algorithm/leetcode/110/ac110.go",
"chars": 508,
"preview": "package problem110\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc isBalanced(root *TreeNod"
},
{
"path": "algorithm/leetcode/111/ac111.go",
"chars": 416,
"preview": "package problem111\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc minDepth(root *TreeNode)"
},
{
"path": "algorithm/leetcode/112/ac112.go",
"chars": 366,
"preview": "package problem112\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc hasPathSum(root *TreeNod"
},
{
"path": "algorithm/leetcode/118/ac118.go",
"chars": 337,
"preview": "package problem118\n\nfunc generate(numRows int) [][]int {\n\tif numRows < 1 {\n\t\treturn nil\n\t}\n\n\tans := make([][]int, 0)\n\n\tf"
},
{
"path": "algorithm/leetcode/119/ac119.go",
"chars": 240,
"preview": "package problem119\n\nfunc getRow(rowIndex int) []int {\n\tif rowIndex < 0 {\n\t\treturn nil\n\t}\n\n\tvi := make([]int, rowIndex+1)"
},
{
"path": "algorithm/leetcode/120/ac120.go",
"chars": 367,
"preview": "package problem120\n\n// 自下向上推导\n// ans数组保存的是:到达下一行某位置的最小路径和\nfunc minimumTotal(triangle [][]int) int {\n\tans := make([]int, "
},
{
"path": "algorithm/leetcode/120/ac120_test.go",
"chars": 189,
"preview": "package problem120\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_minimumTotal(t *testing.T) {\n\tdata := [][]int{\n\t\t{2},\n\t\t{3, "
},
{
"path": "algorithm/leetcode/121/ac121.go",
"chars": 348,
"preview": "package problem121\n\nfunc maxProfit(prices []int) int {\n\tif len(prices) < 1 {\n\t\treturn 0\n\t}\n\n\tmaxProfit := 0\n\tminPrice :="
},
{
"path": "algorithm/leetcode/121/ac121_test.go",
"chars": 152,
"preview": "package problem121\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_maxProfit(t *testing.T) {\n\tdata := []int{7, 1, 5, 3, 6, 4}\n\n"
},
{
"path": "algorithm/leetcode/122/ac122.go",
"chars": 224,
"preview": "package problem122\n\nfunc maxProfit(prices []int) int {\n\tif len(prices) < 1 {\n\t\treturn 0\n\t}\n\n\tans := 0\n\tfor i := 1; i < l"
},
{
"path": "algorithm/leetcode/125/ac125.go",
"chars": 637,
"preview": "package problem125\n\nfunc isPalindrome(s string) bool {\n\tif len(s) <= 1 {\n\t\treturn true\n\t}\n\n\tleft, right := 0, len(s)-1\n\t"
},
{
"path": "algorithm/leetcode/125/ac125_test.go",
"chars": 211,
"preview": "package problem125\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_isCharacter(t *testing.T) {\n\tstr := \"A man, a plan, a canal:"
},
{
"path": "algorithm/leetcode/136/ac136.go",
"chars": 127,
"preview": "package problem136\n\nfunc singleNumber(nums []int) int {\n\tans := 0\n\tfor _, value := range nums{\n\t\tans ^= value\n\t}\n\treturn"
},
{
"path": "algorithm/leetcode/137/ac137.go",
"chars": 1145,
"preview": "package problem137\n\nfunc singleNumber(nums []int) int {\n\tdict := make(map[int]int, 0)\n\n\tfor _, v := range nums {\n\t\ttemp,"
},
{
"path": "algorithm/leetcode/137/ac137_test.go",
"chars": 189,
"preview": "package problem137\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n)\n\nfunc Test_singleNumber(t *testing.T) {\n\tnums1 := []int{2, 2, 3, 2}\n\n\tf"
},
{
"path": "algorithm/leetcode/160/ac160.go",
"chars": 394,
"preview": "package problem160\n\ntype ListNode struct {\n\tVal int\n\tNext *ListNode\n}\n\nfunc getIntersectionNode(headA, headB *ListNode)"
},
{
"path": "algorithm/leetcode/160/ac160_test.go",
"chars": 614,
"preview": "package problem160\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\n// 两个单链表的相交节点为 node4 该节点值为 4\n// node11 -> node22\n// "
},
{
"path": "algorithm/leetcode/167/ac167.go",
"chars": 298,
"preview": "package problem167\n\nfunc twoSum(numbers []int, target int) []int {\n\tleft, right := 0, len(numbers)-1\n\tfor left < right {"
},
{
"path": "algorithm/leetcode/168/ac141.go",
"chars": 262,
"preview": "package problem168\n\nfunc convertToTitle(n int) string {\n\tans := make([]byte, 0)\n\n\tfor n > 0 {\n\t\tn--\n\t\tans = append(ans, "
},
{
"path": "algorithm/leetcode/168/ac141_test.go",
"chars": 126,
"preview": "package problem168\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_convertToTitle(t *testing.T) {\n\tfmt.Println(convertToTitle(2"
},
{
"path": "algorithm/leetcode/169/ac169.go",
"chars": 297,
"preview": "package problem169\n\nfunc majorityElement(nums []int) int {\n\tif len(nums) < 1 {\n\t\treturn 0\n\t}\n\n\tcount, flag := 1, nums[0]"
},
{
"path": "algorithm/leetcode/171/ac171.go",
"chars": 152,
"preview": "package problem171\n\nfunc titleToNumber(s string) int {\n\tans := 0\n\tfor _, value := range s {\n\t\tans = int(value) - int('A'"
},
{
"path": "algorithm/leetcode/172/ac172.go",
"chars": 127,
"preview": "package problem172\n\nfunc trailingZeroes(n int) int {\n\tif 0 == n {\n\t\treturn 0\n\t} else {\n\t\treturn n/5 + trailingZeroes(n/5"
},
{
"path": "algorithm/leetcode/189/ac189.go",
"chars": 360,
"preview": "package problem189\n\nfunc rotate(nums []int, k int) {\n\tif nil == nums {\n\t\treturn\n\t}\n\n\tlength := len(nums)\n\tif k > length "
},
{
"path": "algorithm/leetcode/189/ac189_test.go",
"chars": 237,
"preview": "package problem189\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_rotate(t *testing.T) {\n\tnumbers := []int{1, 2, 3, 4, 5, 6, 7"
},
{
"path": "algorithm/leetcode/191/ac191.go",
"chars": 117,
"preview": "package problem191\n\nfunc hammingWeight(n uint32) int {\n\tans := 0\n\tfor 0 != n {\n\t\tn &= n - 1\n\t\tans++\n\t}\n\treturn ans\n}\n"
},
{
"path": "algorithm/leetcode/191/ac191_test.go",
"chars": 132,
"preview": "package problem191\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_hammingWeight(t *testing.T) {\n\tfmt.Println(hammingWeight(uin"
},
{
"path": "algorithm/leetcode/198/ac198.go",
"chars": 449,
"preview": "package problem198\n\nfunc rob(nums []int) int {\n\tif nil == nums || len(nums) < 1 {\n\t\treturn 0\n\t}\n\n\tif 1 == len(nums) {\n\t\t"
},
{
"path": "algorithm/leetcode/202/ac202.go",
"chars": 381,
"preview": "package problem202\n\nfunc isHappy(n int) bool {\n\tslow, fast := n, n\n\tfor {\n\t\tslow = digitSquareSum(slow)\n\t\tfast = digitSq"
},
{
"path": "algorithm/leetcode/203/ac203.go",
"chars": 635,
"preview": "package problem203\n\ntype ListNode struct {\n\tVal int\n\tNext *ListNode\n}\n\n// 递归写法 效率低\nfunc removeElements(head *ListNode, "
},
{
"path": "algorithm/leetcode/204/ac204.go",
"chars": 275,
"preview": "package problem204\n\nfunc countPrimes(n int) int {\n\tnotPrime := make([]bool, n)\n\tcount := 0\n\n\tfor i := 2; i < n; i++ {\n\t\t"
},
{
"path": "algorithm/leetcode/205/ac205.go",
"chars": 279,
"preview": "package problem205\n\nfunc isIsomorphic(s string, t string) bool {\n\tlength := len(s)\n\tnum1 := make([]int, 256)\n\tnum2 := ma"
},
{
"path": "algorithm/leetcode/205/ac205_test.go",
"chars": 160,
"preview": "package problem205\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_isIsomorphic(t *testing.T) {\n\tstr1 := \"egg\"\n\tstr2 := \"add\"\n\t"
},
{
"path": "algorithm/leetcode/206/ac206.go",
"chars": 517,
"preview": "package problem206\n\ntype ListNode struct {\n\tVal int\n\tNext *ListNode\n}\n\nfunc reverseList(head *ListNode) *ListNode {\n\tva"
},
{
"path": "algorithm/leetcode/217/ac217.go",
"chars": 405,
"preview": "package problem217\n\nimport \"sort\"\n\nfunc containsDuplicate(nums []int) bool {\n\tdict := make(map[int]int, len(nums))\n\n\tfor"
},
{
"path": "algorithm/leetcode/219/ac219.go",
"chars": 289,
"preview": "package problem219\n\nfunc containsNearbyDuplicate(nums []int, k int) bool {\n\tdict := make(map[int]int, len(nums))\n\n\tfor i"
},
{
"path": "algorithm/leetcode/226/ac226.go",
"chars": 265,
"preview": "package problem226\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc invertTree(root *TreeNod"
},
{
"path": "algorithm/leetcode/231/ac231.go",
"chars": 113,
"preview": "package problem231\n\nfunc isPowerOfTwo(n int) bool {\n\tif n <= 0 {\n\t\treturn false\n\t}\n\n\treturn 0 == (n & (n - 1))\n}\n"
},
{
"path": "algorithm/leetcode/234/ac234.go",
"chars": 600,
"preview": "package problem234\n\ntype ListNode struct {\n\tVal int\n\tNext *ListNode\n}\n\nvar h *ListNode\n\nfunc isPalindrome(head *ListNod"
},
{
"path": "algorithm/leetcode/258/ac258.go",
"chars": 74,
"preview": "package problem258\n\nfunc addDigits(num int) int {\n\treturn 1 + (num-1)%9\n}\n"
},
{
"path": "algorithm/leetcode/263/ac263.go",
"chars": 143,
"preview": "package problem263\n\nfunc isUgly(num int) bool {\n\tfor i := 5; i >= 2 && num > 0; i-- {\n\t\tfor 0 == num%i {\n\t\t\tnum /= i\n\t\t}"
},
{
"path": "algorithm/leetcode/268/ac268.go",
"chars": 591,
"preview": "package problem268\n\nimport \"sort\"\n\nfunc missingNumber(nums []int) int {\n\tif nil == nums {\n\t\treturn -1\n\t}\n\n\tsort.Ints(num"
},
{
"path": "algorithm/leetcode/268/ac268_test.go",
"chars": 228,
"preview": "package problem268\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_missingNumber(t *testing.T) {\n\tnums := []int{3, 0, 1}\n\tnums2"
},
{
"path": "algorithm/leetcode/283/ac283.go",
"chars": 286,
"preview": "package problem283\n\nfunc moveZeroes(nums []int) {\n\tif nil == nums || len(nums) == 0 {\n\t\treturn\n\t}\n\n\tinsertPos := 0\n\tfor "
},
{
"path": "algorithm/leetcode/290/ac290.go",
"chars": 500,
"preview": "package problem290\n\nimport \"strings\"\n\nfunc wordPattern(pattern string, str string) bool {\n\twords := strings.Split(str, \""
},
{
"path": "algorithm/leetcode/290/ac290_test.go",
"chars": 177,
"preview": "package problem290\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_wordPattern(t *testing.T) {\n\tpattern := \"abba\"\n\tstr := \"dog "
},
{
"path": "algorithm/leetcode/292/ac292.go",
"chars": 159,
"preview": "package problem292\n\n// Nim游戏: 每次最多取 1~n 个石头\n// 精髓在于: 先手获胜的条件是 总石头数不能为 n+1 的倍数\nfunc canWinNim(n int) bool {\n\tif n < 1 {\n\t"
},
{
"path": "algorithm/leetcode/303/ac303.go",
"chars": 320,
"preview": "package problem303\n\ntype NumArray struct {\n\tdata []int\n}\n\nfunc Constructor(nums []int) NumArray {\n\tfor i := 1; i < len(n"
},
{
"path": "algorithm/leetcode/303/ac303_test.go",
"chars": 214,
"preview": "package problem303\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc TestNumArray_SumRange(t *testing.T) {\n\tnums := []int{-2, 0, 3, -5"
},
{
"path": "algorithm/leetcode/326/ac326.go",
"chars": 141,
"preview": "package problem326\n\n// 1162261467 is 3^19, 3^20 is bigger than int\nfunc isPowerOfThree(n int) bool {\n\treturn n > 0 && ("
},
{
"path": "algorithm/leetcode/326/ac326_test.go",
"chars": 402,
"preview": "package problem326\n\nimport \"testing\"\n\nfunc Test_isPowerOfThree(t *testing.T) {\n\ttype args struct {\n\t\tn int\n\t}\n\ttests := "
},
{
"path": "algorithm/leetcode/342/ac342.go",
"chars": 210,
"preview": "package problem342\n\n// 4的幂应该满足的条件:\n// 1. 大于0\n// 2. 该数字的二进制表示中 仅有1个bit位数字为 1\n// 3. 数字为 1 的bit位,应该出现在二进制数位的奇数位置\nfunc isPow"
},
{
"path": "algorithm/leetcode/344/ac344.go",
"chars": 192,
"preview": "package problem344\n\nfunc reverseString(s string) string {\n\tchar := []rune(s)\n\tfor i, j := 0, len(s)-1; i < j; i, j = i+1"
},
{
"path": "algorithm/leetcode/344/ac344_test.go",
"chars": 140,
"preview": "package problem344\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_reverseString(t *testing.T) {\n\ts := \"abcdefg\"\n\n\tfmt.Println("
},
{
"path": "algorithm/leetcode/345/ac345.go",
"chars": 466,
"preview": "package problem345\n\nimport \"strings\"\n\nfunc reverseVowels(s string) string {\n\tif len(s) == 0 {\n\t\treturn s\n\t}\n\n\tvowels := "
},
{
"path": "algorithm/leetcode/345/ac345_test.go",
"chars": 195,
"preview": "package problem345\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_reverseVowels(t *testing.T) {\n\tstr := \"hello\"\n\tstr2 := \"leet"
},
{
"path": "algorithm/leetcode/349/ac349.go",
"chars": 759,
"preview": "package problem349\n\nfunc intersection(nums1 []int, nums2 []int) []int {\n\tif nil == nums1 || len(nums1) == 0 {\n\t\treturn n"
},
{
"path": "algorithm/leetcode/349/ac349_test.go",
"chars": 179,
"preview": "package problem349\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_intersection(t *testing.T) {\n\tnum1 := []int{1, 2, 2, 1}\n\tnum"
},
{
"path": "algorithm/leetcode/350/ac350.go",
"chars": 591,
"preview": "package problem350\n\nfunc intersect(nums1 []int, nums2 []int) []int {\n\tif nil == nums1 || len(nums1) == 0 {\n\t\treturn nums"
},
{
"path": "algorithm/leetcode/350/ac350_test.go",
"chars": 173,
"preview": "package problem350\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_intersect(t *testing.T) {\n\tnum1 := []int{1, 2, 2, 1}\n\tnum2 :"
},
{
"path": "algorithm/leetcode/367/ac367.go",
"chars": 546,
"preview": "package problem367\n\nfunc isPerfectSquare(num int) bool {\n\tif 1 == num {\n\t\treturn true\n\t}\n\n\tfor middle := num / 2; middle"
},
{
"path": "algorithm/leetcode/371/ac371.go",
"chars": 145,
"preview": "package problem371\n\nfunc getSum(a int, b int) int {\n\tif 0 == b {\n\t\treturn a\n\t}\n\tsum := a ^ b\n\tcarry := (a & b) << 1\n\tret"
},
{
"path": "algorithm/leetcode/383/ac383.go",
"chars": 505,
"preview": "package problem383\n\nfunc canConstruct(ransomNote string, magazine string) bool {\n\tif len(ransomNote) == 0 && len(magazin"
},
{
"path": "algorithm/leetcode/383/ac383_test.go",
"chars": 171,
"preview": "package problem383\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_canConstruct(t *testing.T) {\n\transom := \"aa\"\n\tmagazine := \"a"
},
{
"path": "algorithm/leetcode/387/ac387.go",
"chars": 448,
"preview": "package problem387\n\nfunc firstUniqChar(s string) int {\n\tif len(s) == 0 {\n\t\treturn -1\n\t}\n\n\tdict := make(map[rune]int, 0)\n"
},
{
"path": "algorithm/leetcode/389/ac389.go",
"chars": 509,
"preview": "package problem389\n\nfunc findTheDifference(s string, t string) byte {\n\tdict1 := make(map[rune]int, 0)\n\tfor _, v := range"
},
{
"path": "algorithm/leetcode/389/ac389_test.go",
"chars": 165,
"preview": "package problem389\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findTheDifference(t *testing.T) {\n\ts := \"a\"\n\ttt := \"aa\"\n\n\tfm"
},
{
"path": "algorithm/leetcode/400/ac400.go",
"chars": 295,
"preview": "package problem400\n\nimport \"strconv\"\n\nfunc findNthDigit(n int) int {\n\t// 初始值定义 1 位数 共有 9 个, 起始数字为 1\n\tlen, count, start :"
},
{
"path": "algorithm/leetcode/400/ac400_test.go",
"chars": 183,
"preview": "package problem400\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findNthDigit(t *testing.T) {\n\tfmt.Println(findNthDigit(3))\n\t"
},
{
"path": "algorithm/leetcode/404/ac404.go",
"chars": 374,
"preview": "package problem404\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc sumOfLeftLeaves(root *Tr"
},
{
"path": "algorithm/leetcode/405/ac405.go",
"chars": 426,
"preview": "package problem405\n\nfunc toHex(num int) string {\n\tcharMap := []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a"
},
{
"path": "algorithm/leetcode/405/ac405_test.go",
"chars": 132,
"preview": "package problem405\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_toHex(t *testing.T) {\n\tfmt.Println(toHex(14))\n\tfmt.Println(t"
},
{
"path": "algorithm/leetcode/409/ac409.go",
"chars": 392,
"preview": "package problem409\n\n//1、统计所有字母的出现频率(分大小写)\n//2、统计只出现奇数次数字母的个数\n//3、如果2中结果不为0,字符串的长度减去2中的字母个数+1\n//\n//其中3的意思是,保留出现次数最多的那个奇数字"
},
{
"path": "algorithm/leetcode/409/ac409_test.go",
"chars": 154,
"preview": "package problem409\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_longestPalindrome(t *testing.T) {\n\tstr := \"abccccdd\"\n\n\tfmt.P"
},
{
"path": "algorithm/leetcode/412/ac412.go",
"chars": 792,
"preview": "package problem412\n\nimport (\n\t\"strconv\"\n\t\"fmt\"\n)\n\nfunc fizzBuzz(n int) []string {\n\tans := make([]string, 0)\n\n\tfor i := 1"
},
{
"path": "algorithm/leetcode/412/ac412_test.go",
"chars": 142,
"preview": "package problem412\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_fizzBuzz(t *testing.T) {\n\tfor _, v := range fizzBuzz2(15) {\n"
},
{
"path": "algorithm/leetcode/414/ac414.go",
"chars": 1025,
"preview": "package problem414\n\nimport (\n\t\"sort\"\n)\n\nfunc thirdMax(nums []int) int {\n\tlength := len(nums)\n\tif length < 1 {\n\t\treturn -"
},
{
"path": "algorithm/leetcode/414/ac414_test.go",
"chars": 247,
"preview": "package problem414\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_thirdMax(t *testing.T) {\n\tnum1 := []int{3, 2, 1}\n\tnum2 := []"
},
{
"path": "algorithm/leetcode/415/ac415.go",
"chars": 1041,
"preview": "package problem415\n\nfunc addStrings(num1 string, num2 string) string {\n\tstr1 := []rune(num1)\n\tstr2 := []rune(num2)\n\tans "
},
{
"path": "algorithm/leetcode/415/ac415_test.go",
"chars": 157,
"preview": "package problem415\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_addStrings(t *testing.T) {\n\tstr1 := \"123\"\n\tstr2 := \"231\"\n\n\tf"
},
{
"path": "algorithm/leetcode/434/ac434.go",
"chars": 174,
"preview": "package problem434\n\nfunc countSegments(s string) int {\n\tans := 0\n\tfor i := 0; i < len(s); i++ {\n\t\tif s[i] != ' ' && (0 ="
},
{
"path": "algorithm/leetcode/437/ac437.go",
"chars": 473,
"preview": "package problem437\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc pathSum(root *TreeNode, "
},
{
"path": "algorithm/leetcode/438/ac438.go",
"chars": 1103,
"preview": "package problem438\n\nfunc findAnagrams(s string, p string) []int {\n\tdict := make(map[byte]int, 0)\n\tfor i := 0; i < len(p)"
},
{
"path": "algorithm/leetcode/438/ac438_test.go",
"chars": 287,
"preview": "package problem438\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findAnagrams(t *testing.T) {\n\ts := \"cbaebabacd\"\n\tp := \"abc\"\n"
},
{
"path": "algorithm/leetcode/441/ac441.go",
"chars": 403,
"preview": "package problem441\n\nimport \"math\"\n\n// 题目的意思其实就是从1~x层完整楼梯硬币数量加起来,要小于等于n,求最大的x。\n// 说到加起来的数量,很容易想到求累加和,我们知道求累加和的公式为:\n// sum"
},
{
"path": "algorithm/leetcode/441/ac441_test.go",
"chars": 151,
"preview": "package problem441\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_arrangeCoins(t *testing.T) {\n\tfmt.Println(arrangeCoins(5))\n\t"
},
{
"path": "algorithm/leetcode/443/ac443.go",
"chars": 434,
"preview": "package problem443\n\nimport \"strconv\"\n\nfunc compress(chars []byte) int {\n\tindex, indexAns := 0, 0\n\tfor index < len(chars)"
},
{
"path": "algorithm/leetcode/443/ac443_test.go",
"chars": 167,
"preview": "package problem443\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_compress(t *testing.T) {\n\tdata := []byte{'a', 'a', 'b', 'b',"
},
{
"path": "algorithm/leetcode/447/ac447.go",
"chars": 445,
"preview": "package problem447\n\nfunc numberOfBoomerangs(points [][]int) int {\n\tans := 0\n\n\tfor i := 0; i < len(points); i++ {\n\t\tdict "
},
{
"path": "algorithm/leetcode/448/ac448.go",
"chars": 362,
"preview": "package problem448\n\nfunc findDisappearedNumbers(nums []int) []int {\n\tans := make([]int, 0)\n\n\tfor i := 0; i < len(nums); "
},
{
"path": "algorithm/leetcode/453/ac453.go",
"chars": 323,
"preview": "package problem453\n\nfunc minMoves(nums []int) int {\n\tif nil == nums || len(nums) == 0 {\n\t\treturn -1\n\t}\n\tif len(nums) == "
},
{
"path": "algorithm/leetcode/453/ac453_test.go",
"chars": 139,
"preview": "package problem453\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_minMoves(t *testing.T) {\n\tnum := []int{1, 2, 3}\n\n\tfmt.Printl"
},
{
"path": "algorithm/leetcode/455/ac455.go",
"chars": 212,
"preview": "package problem455\n\nimport \"sort\"\n\nfunc findContentChildren(g []int, s []int) int {\n\tsort.Ints(g)\n\tsort.Ints(s)\n\n\ti := 0"
},
{
"path": "algorithm/leetcode/455/ac455_test.go",
"chars": 266,
"preview": "package problem455\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findContentChildren(t *testing.T) {\n\tg1 := []int{1, 2, 3}\n\ts"
},
{
"path": "algorithm/leetcode/458/ac458.go",
"chars": 755,
"preview": "package problem458\n\n// 1. 一只猪在一小时内最多能验多少桶?\n//\n//一次喝一个桶的,15分钟后没挂再喝第二桶,一小时60分钟内可以喝 60/15 = 4 次,如果有5桶水,那个只要喝前4桶就只能第5桶是否有毒。\n"
},
{
"path": "algorithm/leetcode/459/ac459.go",
"chars": 193,
"preview": "package problem459\n\nimport \"strings\"\n\nfunc repeatedSubstringPattern(s string) bool {\n\tif len(s) < 2 {\n\t\treturn false\n\t}\n"
},
{
"path": "algorithm/leetcode/459/ac459_test.go",
"chars": 215,
"preview": "package problem459\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_repeatedSubstringPattern(t *testing.T) {\n\ts := \"ab\"\n\tfmt.Pri"
},
{
"path": "algorithm/leetcode/461/ac461.go",
"chars": 135,
"preview": "package problem461\n\nfunc hammingDistance(x int, y int) int {\n\tn := x ^ y\n\tans := 0\n\tfor n != 0 {\n\t\tn &= n - 1\n\t\tans++\n\t}"
},
{
"path": "algorithm/leetcode/463/ac463.go",
"chars": 572,
"preview": "package problem463\n\nfunc islandPerimeter(grid [][]int) int {\n\tisLand, neighbours := 0, 0\n\n\tfor i := 0; i < len(grid); i+"
},
{
"path": "algorithm/leetcode/463/ac463_test.go",
"chars": 214,
"preview": "package problem463\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_islandPerimeter(t *testing.T) {\n\tland := [][]int{\n\t\t{0, 1, 0"
},
{
"path": "algorithm/leetcode/475/ac475.go",
"chars": 446,
"preview": "package problem475\n\nimport \"sort\"\n\nfunc findRadius(houses []int, heaters []int) int {\n\tsort.Ints(houses)\n\tsort.Ints(heat"
},
{
"path": "algorithm/leetcode/475/ac475_test.go",
"chars": 201,
"preview": "package problem475\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findRadius(t *testing.T) {\n\tfmt.Println(findRadius([]int{1, "
},
{
"path": "algorithm/leetcode/476/ac476.go",
"chars": 150,
"preview": "package problem476\n\nfunc findComplement(num int) int {\n\ttemp, mask := num, 0\n\n\tfor temp > 0 {\n\t\tmask = mask*2 + 1\n\t\ttemp"
},
{
"path": "algorithm/leetcode/476/ac476_test.go",
"chars": 125,
"preview": "package problem476\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findComplement(t *testing.T) {\n\tfmt.Println(findComplement(5"
},
{
"path": "algorithm/leetcode/479/ac479.go",
"chars": 516,
"preview": "package problem479\n\nimport (\n\t\"strconv\"\n\t\"math\"\n)\n\nfunc largestPalindrome(n int) int {\n\tif 1 == n {\n\t\treturn 9\n\t}\n\n\tmax "
},
{
"path": "algorithm/leetcode/479/ac479_test.go",
"chars": 201,
"preview": "package problem479\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_largestPalindrome(t *testing.T) {\n\tfmt.Println(largestPalind"
},
{
"path": "algorithm/leetcode/485/ac485.go",
"chars": 223,
"preview": "package problem485\n\nfunc findMaxConsecutiveOnes(nums []int) int {\n\tans, count := 0, 0\n\tfor _, v := range nums {\n\t\tif 1 ="
},
{
"path": "algorithm/leetcode/485/ac485_test.go",
"chars": 178,
"preview": "package problem485\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findMaxConsecutiveOnes(t *testing.T) {\n\tnums := []int{1, 1, "
},
{
"path": "algorithm/leetcode/492/ac492.go",
"chars": 368,
"preview": "package problem492\n\nimport \"math\"\n\nfunc constructRectangle(area int) []int {\n\tans := make([]int, 0)\n\n\tnum := int(math.Sq"
},
{
"path": "algorithm/leetcode/492/ac492_test.go",
"chars": 133,
"preview": "package problem492\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_constructRectangle(t *testing.T) {\n\tfmt.Println(constructRec"
},
{
"path": "algorithm/leetcode/496/ac496.go",
"chars": 444,
"preview": "package problem496\n\nfunc nextGreaterElement(findNums []int, nums []int) []int {\n\tans := make([]int, 0)\n\tfor _, v := rang"
},
{
"path": "algorithm/leetcode/496/ac496_test.go",
"chars": 194,
"preview": "package problem496\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_nextGreaterElement(t *testing.T) {\n\tnum1 := []int{4, 1, 2}\n\t"
},
{
"path": "algorithm/leetcode/500/ac500.go",
"chars": 628,
"preview": "package problem500\n\nimport \"strings\"\n\nfunc findWords(words []string) []string {\n\tans := make([]string, 0)\n\tdata := []str"
},
{
"path": "algorithm/leetcode/500/ac500_test.go",
"chars": 173,
"preview": "package problem500\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findWords(t *testing.T) {\n\twords := []string{\"Hello\", \"Alask"
},
{
"path": "algorithm/leetcode/501/ac501.go",
"chars": 762,
"preview": "package problem501\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc findMode(root *TreeNode)"
},
{
"path": "algorithm/leetcode/501/ac501_test.go",
"chars": 277,
"preview": "package problem501\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findMode(t *testing.T) {\n\tnode21 := TreeNode{Val: 2, Left: n"
},
{
"path": "algorithm/leetcode/504/ac504.go",
"chars": 293,
"preview": "package problem504\n\nimport (\n\t\"strconv\"\n)\n\nfunc convertToBase7(num int) string {\n\tif 0 == num {\n\t\treturn \"0\"\n\t}\n\n\tans :="
},
{
"path": "algorithm/leetcode/504/ac504_test.go",
"chars": 192,
"preview": "package problem504\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_convertToBase7(t *testing.T) {\n\tfmt.Println(convertToBase7(7"
},
{
"path": "algorithm/leetcode/506/ac506.go",
"chars": 830,
"preview": "package problem506\n\nimport (\n\t\"sort\"\n\t\"strconv\"\n)\n\ntype person struct {\n\tscore int\n\tindex int\n}\n\ntype persons []person\n\n"
},
{
"path": "algorithm/leetcode/506/ac506_test.go",
"chars": 165,
"preview": "package problem506\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findRelativeRanks(t *testing.T) {\n\tnums := []int{5, 4, 3, 2,"
},
{
"path": "algorithm/leetcode/507/ac507.go",
"chars": 260,
"preview": "package problem507\n\nimport \"math\"\n\nfunc checkPerfectNumber(num int) bool {\n\tif 1 == num {\n\t\treturn false\n\t}\n\n\t// 1 为该数字的"
},
{
"path": "algorithm/leetcode/507/ac507_test.go",
"chars": 207,
"preview": "package problem507\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_checkPerfectNumber(t *testing.T) {\n\tfmt.Println(checkPerfect"
},
{
"path": "algorithm/leetcode/520/ac520.go",
"chars": 982,
"preview": "package problem520\n\nfunc detectCapitalUse(word string) bool {\n\tif len(word) <= 1 {\n\t\treturn true\n\t}\n\n\tfirstChar := word["
},
{
"path": "algorithm/leetcode/520/ac520_test.go",
"chars": 378,
"preview": "package problem520\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_detectCapitalUse(t *testing.T) {\n\tstr1 := \"USA\"\n\tfmt.Println"
},
{
"path": "algorithm/leetcode/521/ac521.go",
"chars": 152,
"preview": "package problem521\n\nfunc findLUSlength(a string, b string) int {\n\tif a == b {\n\t\treturn -1\n\t}\n\n\tif len(a) > len(b) {\n\t\tre"
},
{
"path": "algorithm/leetcode/530/ac530.go",
"chars": 580,
"preview": "package problem530\n\nimport \"math\"\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc getMinimu"
},
{
"path": "algorithm/leetcode/530/ac530_test.go",
"chars": 300,
"preview": "package problem530\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_getMinimumDifference(t *testing.T) {\n\tnode3 := TreeNode{Val:"
},
{
"path": "algorithm/leetcode/532/ac532.go",
"chars": 362,
"preview": "package problem532\n\nfunc findPairs(nums []int, k int) int {\n\tif nil == nums || len(nums) == 0 || k < 0 {\n\t\treturn 0\n\t}\n\n"
},
{
"path": "algorithm/leetcode/532/ac532_test.go",
"chars": 151,
"preview": "package problem532\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findPairs(t *testing.T) {\n\tnums := []int{3, 1, 4, 1, 5}\n\tfmt"
},
{
"path": "algorithm/leetcode/538/ac538.go",
"chars": 415,
"preview": "package problem538\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc convertBST(root *TreeNod"
},
{
"path": "algorithm/leetcode/541/ac541.go",
"chars": 340,
"preview": "package problem541\n\nfunc reverseStr(s string, k int) string {\n\tchars := []byte(s)\n\n\tfor idx := 0; idx*k < len(chars); id"
},
{
"path": "algorithm/leetcode/541/ac541_test.go",
"chars": 199,
"preview": "package problem541\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_reverseStr(t *testing.T) {\n\ts := \"abcdefg\"\n\tk := 2\n\n\tfmt.Pri"
},
{
"path": "algorithm/leetcode/543/ac543.go",
"chars": 766,
"preview": "package problem543\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\n// 二叉树的直径解题思路:\n// 二叉树中的某个节点的左"
},
{
"path": "algorithm/leetcode/551/ac551.go",
"chars": 442,
"preview": "package problem551\n\nimport \"strings\"\n\nfunc checkRecord(s string) bool {\n\tcountA := 0\n\n\tfor i := 0; i < len(s); i++ {\n\t\ti"
},
{
"path": "algorithm/leetcode/551/ac551_test.go",
"chars": 192,
"preview": "package problem551\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_checkRecord(t *testing.T) {\n\tstr1 := \"PPALLP\"\n\tfmt.Println(c"
},
{
"path": "algorithm/leetcode/557/ac557.go",
"chars": 435,
"preview": "package problem557\n\nfunc reverseWords(s string) string {\n\tstrChar := []rune(s)\n\n\tstart, end := 0, 0\n\tfor i := 0; i < len"
},
{
"path": "algorithm/leetcode/557/ac557_test.go",
"chars": 161,
"preview": "package problem557\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_reverseWords(t *testing.T) {\n\tstr := \"Let's take LeetCode co"
},
{
"path": "algorithm/leetcode/561/ac561.go",
"chars": 512,
"preview": "package problem561\n\nimport \"sort\"\n\nfunc arrayPairSum(nums []int) int {\n\tans := 0\n\tsort.Ints(nums)\n\tfor i, value := range"
},
{
"path": "algorithm/leetcode/561/ac561_test.go",
"chars": 153,
"preview": "package problem561\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_arrayPairSum(t *testing.T) {\n\tnums := []int{1, 4, 3, 2}\n\n\tfm"
},
{
"path": "algorithm/leetcode/563/ac563.go",
"chars": 467,
"preview": "package problem563\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc findTilt(root *TreeNode)"
},
{
"path": "algorithm/leetcode/566/ac566.go",
"chars": 961,
"preview": "package problem566\n\nfunc matrixReshape(nums [][]int, r int, c int) [][]int {\n\tif nil == nums || 0 == len(nums) || len(nu"
},
{
"path": "algorithm/leetcode/566/ac566_test.go",
"chars": 294,
"preview": "package problem566\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_matrixReshape(t *testing.T) {\n\tnums := [][]int{\n\t\t{1, 2},\n\t\t"
},
{
"path": "algorithm/leetcode/572/ac572.go",
"chars": 466,
"preview": "package problem572\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc isSubtree(s *TreeNode, t"
},
{
"path": "algorithm/leetcode/575/ac575.go",
"chars": 227,
"preview": "package problem575\n\nfunc distributeCandies(candies []int) int {\n\tmySet := make(map[int]int, 0)\n\n\tfor _, v := range candi"
},
{
"path": "algorithm/leetcode/575/ac575_test.go",
"chars": 236,
"preview": "package problem575\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_distributeCandies(t *testing.T) {\n\tnums := []int{1, 1, 2, 2,"
},
{
"path": "algorithm/leetcode/581/ac581.go",
"chars": 964,
"preview": "package problem581\n\nimport \"sort\"\n\nfunc findUnsortedSubarray(nums []int) int {\n\tif nil == nums || len(nums) == 0 {\n\t\tret"
},
{
"path": "algorithm/leetcode/581/ac581_test.go",
"chars": 245,
"preview": "package problem581\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findUnsortedSubarray(t *testing.T) {\n\tnum := []int{2, 6, 4, "
},
{
"path": "algorithm/leetcode/594/ac594.go",
"chars": 343,
"preview": "package problem594\n\n// 和谐数组为 原数组的子数组即可,不需要是连续的子数组...\nfunc findLHS(nums []int) int {\n\tdict := make(map[int]int, 0)\n\tfor _"
},
{
"path": "algorithm/leetcode/594/ac594_test.go",
"chars": 220,
"preview": "package problem594\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n)\n\nfunc Test_findLHS(t *testing.T) {\n\tnums := []int{1, 3, 2, 2, 5, 2, 3, "
},
{
"path": "algorithm/leetcode/598/ac598.go",
"chars": 319,
"preview": "package problem598\n\nfunc maxCount(m int, n int, ops [][]int) int {\n\tif nil == ops || len(ops) == 0 {\n\t\treturn m * n\n\t}\n\n"
},
{
"path": "algorithm/leetcode/598/ac598_test.go",
"chars": 159,
"preview": "package problem598\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_maxCount(t *testing.T) {\n\tope := [][]int{\n\t\t{2, 2},\n\t\t{3, 3}"
},
{
"path": "algorithm/leetcode/599/ac599.go",
"chars": 489,
"preview": "package problem599\n\nfunc findRestaurant(list1 []string, list2 []string) []string {\n\tdict1 := make(map[string]int, len(li"
},
{
"path": "algorithm/leetcode/599/ac599_test.go",
"chars": 303,
"preview": "package problem599\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_findRestaurant(t *testing.T) {\n\tlist1 := []string{\"Shogun\", "
},
{
"path": "algorithm/leetcode/605/ac605.go",
"chars": 456,
"preview": "package problem605\n\nfunc canPlaceFlowers(flowerbed []int, n int) bool {\n\tcount := 0\n\tfor i := 0; i < len(flowerbed); i++"
},
{
"path": "algorithm/leetcode/605/ac605_test.go",
"chars": 218,
"preview": "package problem605\n\nimport (\n\t\"testing\"\n\t\"fmt\"\n)\n\nfunc Test_canPlaceFlowers(t *testing.T) {\n\tflowerbed := []int{1, 0, 0,"
},
{
"path": "algorithm/leetcode/606/ac606.go",
"chars": 503,
"preview": "package problem606\n\nimport \"strconv\"\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc tree2s"
},
{
"path": "algorithm/leetcode/617/ac617.go",
"chars": 327,
"preview": "package problem617\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc mergeTrees(t1 *TreeNode,"
},
{
"path": "algorithm/leetcode/628/ac628.go",
"chars": 912,
"preview": "package problem628\n\nimport (\n\t\"sort\"\n\t\"math\"\n)\n\nfunc maximumProduct(nums []int) int {\n\tif len(nums) < 3 {\n\t\treturn 0\n\t}\n"
},
{
"path": "algorithm/leetcode/633/ac633.go",
"chars": 319,
"preview": "package problem633\n\nimport \"math\"\n\nfunc judgeSquareSum(c int) bool {\n\tif c < 0 {\n\t\treturn false\n\t}\n\n\tleft, right := 0, i"
},
{
"path": "algorithm/leetcode/637/ac637.go",
"chars": 641,
"preview": "package problem637\n\ntype TreeNode struct {\n\tVal int\n\tLeft *TreeNode\n\tRight *TreeNode\n}\n\nfunc averageOfLevels(root *Tr"
},
{
"path": "algorithm/leetcode/643/ac643.go",
"chars": 345,
"preview": "package problem643\n\nimport \"math\"\n\nfunc findMaxAverage(nums []int, k int) float64 {\n\tif nil == nums || len(nums) == 0 {\n"
}
]
// ... and 148 more files (download for full content)
About this extraction
This page contains the full source code of the mayuanucas/mygo GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 348 files (155.9 KB), approximately 66.9k tokens, and a symbol index with 554 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.