diff --git a/leetcode/.gitignore b/leetcode/.gitignore
new file mode 100644
index 00000000..70375054
--- /dev/null
+++ b/leetcode/.gitignore
@@ -0,0 +1,3 @@
+/target
+/cmake-build-debug
+/cmake-build-release
diff --git a/leetcode/CMakeLists.txt b/leetcode/CMakeLists.txt
new file mode 100644
index 00000000..d39d33b5
--- /dev/null
+++ b/leetcode/CMakeLists.txt
@@ -0,0 +1,5 @@
+
+cmake_minimum_required(VERSION 3.10)
+project(leetcode)
+
+add_subdirectory(cpp)
diff --git a/leetcode/Cargo.lock b/leetcode/Cargo.lock
new file mode 100644
index 00000000..17184853
--- /dev/null
+++ b/leetcode/Cargo.lock
@@ -0,0 +1,127 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "lc-0001-two-sum"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0002-add-two-numbers"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0003-longest-substring-without-repeating-characters"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0004-median-of-two-sorted-arrays"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0011-container-with-most-water"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0015-3sum"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0016-3sum-closest"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0018-4sum"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0026-remove-duplicates-from-sorted-array"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0027-remove-element"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0034-find-first-and-last-position-of-element-in-sorted-array"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0035-search-insert-position"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0036-valid-sudoku"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0053-maximum-subarray"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0066-plus-one"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0078-subsets"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0083-remove-duplicates-from-sorted-list"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0088-merge-sorted-array"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0128-longest-consecutive-sequence"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0136-single-number"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0137-single-number-ii"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0191-number-of-1-bits"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0217-contains-duplicate"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0219-contains-duplicate-ii"
+version = "0.1.0"
+
+[[package]]
+name = "lc-0220-contains-duplicate-iii"
+version = "0.1.0"
+
+[[package]]
+name = "lc-1114-print-in-order"
+version = "0.1.0"
+
+[[package]]
+name = "lc-1115-print-foobar-alternately"
+version = "0.1.0"
+
+[[package]]
+name = "lc-1116-print-zero-even-odd"
+version = "0.1.0"
+
+[[package]]
+name = "lc-1117-building-h2o"
+version = "0.1.0"
+
+[[package]]
+name = "lc-1195-fizz-buzz-multithreaded"
+version = "0.1.0"
+
+[[package]]
+name = "lc-704-binary-search"
+version = "0.1.0"
diff --git a/leetcode/Cargo.toml b/leetcode/Cargo.toml
new file mode 100644
index 00000000..79c66a42
--- /dev/null
+++ b/leetcode/Cargo.toml
@@ -0,0 +1,11 @@
+[workspace]
+resolver = "2"
+
+members = [
+ "rust/*.*"
+]
+
+exclude = [
+ "cpp",
+ "problems",
+]
diff --git a/leetcode/LICENSE b/leetcode/LICENSE
new file mode 100644
index 00000000..d41c0bd9
--- /dev/null
+++ b/leetcode/LICENSE
@@ -0,0 +1,232 @@
+GNU GENERAL PUBLIC LICENSE
+Version 3, 29 June 2007
+
+Copyright © 2007 Free Software Foundation, Inc.
+
+Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
+
+Preamble
+
+The GNU General Public License is a free, copyleft license for software and other kinds of works.
+
+The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.
+
+When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.
+
+To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others.
+
+For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
+
+Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it.
+
+For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions.
+
+Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users.
+
+Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free.
+
+The precise terms and conditions for copying, distribution and modification follow.
+
+TERMS AND CONDITIONS
+
+0. Definitions.
+
+“This License” refers to version 3 of the GNU General Public License.
+
+“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
+
+“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations.
+
+To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work.
+
+A “covered work” means either the unmodified Program or a work based on the Program.
+
+To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
+
+To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
+
+An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
+
+1. Source Code.
+The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.
+
+A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
+
+The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
+
+The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work.
+
+The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
+
+The Corresponding Source for a work in source code form is that same work.
+
+2. Basic Permissions.
+All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
+
+You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
+
+Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
+
+3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
+
+When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
+
+4. Conveying Verbatim Copies.
+You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
+
+You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
+
+5. Conveying Modified Source Versions.
+You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”.
+
+ c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.
+
+A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
+
+6. Conveying Non-Source Forms.
+You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.
+
+ d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.
+
+A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.
+
+A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.
+
+“Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.
+
+If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).
+
+The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
+
+Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
+
+7. Additional Terms.
+“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
+
+When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
+
+Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.
+
+All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.
+
+If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
+
+Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
+
+8. Termination.
+You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
+
+However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
+
+Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
+
+Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
+
+9. Acceptance Not Required for Having Copies.
+You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
+
+10. Automatic Licensing of Downstream Recipients.
+Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
+
+An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
+
+You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
+
+11. Patents.
+A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”.
+
+A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
+
+Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
+
+In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
+
+If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
+
+If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.
+
+A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
+
+Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
+
+12. No Surrender of Others' Freedom.
+If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
+
+13. Use with the GNU Affero General Public License.
+Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such.
+
+14. Revised Versions of this License.
+The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation.
+
+If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
+
+Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
+
+15. Disclaimer of Warranty.
+THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+16. Limitation of Liability.
+IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+
+17. Interpretation of Sections 15 and 16.
+If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
+
+END OF TERMS AND CONDITIONS
+
+How to Apply These Terms to Your New Programs
+
+If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
+
+To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
+
+
+ Copyright (C)
+
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with this program. If not, see .
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
+
+ Copyright (C)
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”.
+
+You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see .
+
+The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read .
diff --git a/leetcode/README.md b/leetcode/README.md
new file mode 100644
index 00000000..1a85c5eb
--- /dev/null
+++ b/leetcode/README.md
@@ -0,0 +1,5 @@
+
+# About
+
+Leetcode problems and solutions
+
diff --git a/leetcode/cpp/0002.add-two-numbers/CMakeLists.txt b/leetcode/cpp/0002.add-two-numbers/CMakeLists.txt
new file mode 100644
index 00000000..1bd738b2
--- /dev/null
+++ b/leetcode/cpp/0002.add-two-numbers/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0002-add-two-numbers
+ main.c
+ )
diff --git a/leetcode/cpp/0002.add-two-numbers/main.c b/leetcode/cpp/0002.add-two-numbers/main.c
new file mode 100644
index 00000000..899bd9dd
--- /dev/null
+++ b/leetcode/cpp/0002.add-two-numbers/main.c
@@ -0,0 +1,69 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* new_node = malloc(sizeof(struct ListNode));
+ assert(new_node != NULL);
+ new_node->val = arr[i];
+ new_node->next = list;
+ list = new_node;
+ }
+ return list;
+}
+
+struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
+ struct ListNode* l3 = NULL;
+ struct ListNode* tail_node = NULL;
+ int carry = 0;
+ while (l1 != NULL || l2 != NULL || carry != 0) {
+ struct ListNode* new_node = malloc(sizeof(struct ListNode));
+ assert(new_node != NULL);
+ int sum = carry;
+ if (l1 != NULL) {
+ sum += l1->val;
+ l1 = l1->next;
+ }
+ if (l2 != NULL) {
+ sum += l2->val;
+ l2 = l2->next;
+ }
+ carry = sum / 10;
+ new_node->val = sum % 10;
+ new_node->next = NULL;
+ if (tail_node == NULL) {
+ l3 = new_node;
+ tail_node = new_node;
+ } else {
+ tail_node->next = new_node;
+ tail_node = new_node;
+ }
+ }
+ return l3;
+}
+
+int main() {
+ int arr1[] = {2, 4, 3};
+ int arr2[] = {5, 6, 4};
+ struct ListNode* l1 = listFromArray(arr1, 3);
+ struct ListNode* l2 = listFromArray(arr2, 3);
+ struct ListNode* l3 = addTwoNumbers(l1, l2);
+ while (l3 != NULL) {
+ printf("%d, ", l3->val);
+ l3 = l3->next;
+ }
+ printf("\n");
+
+ return 0;
+}
diff --git a/leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt b/leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt
new file mode 100644
index 00000000..9b295693
--- /dev/null
+++ b/leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0003-longest-substring-without-repeating-characters
+ main.c
+ )
diff --git a/leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c b/leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c
new file mode 100644
index 00000000..87ceec72
--- /dev/null
+++ b/leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c
@@ -0,0 +1,52 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+int lengthOfLongestSubstring(char* s) {
+ int n = strlen(s);
+ if (n == 0) {
+ return 0;
+ }
+ if (n == 1) {
+ return 1;
+ }
+
+ int max_val = 1;
+ int len = 0;
+
+ for (int i = 0; i < n; ++i) {
+ for (int j = i + 1; j < n; ++j) {
+ if (s[i] == s[j]) {
+ break;
+ }
+
+ len = j - i - 1;
+ if (max_val < len) {
+ max_val = len;
+ }
+ }
+ }
+ return max_val;
+}
+
+void checkSolutioin(char* s, int exp) {
+ int out = lengthOfLongestSubstring(s);
+ printf("out: %d, exp: %d\n", out, exp);
+ assert(out == exp);
+}
+
+int main() {
+ char* s1 = "abcabcbb";
+ checkSolutioin(s1, 3);
+ char* s2 = "bbbbbb";
+ checkSolutioin(s2, 1);
+ char* s3 = "pwwkew";
+ checkSolutioin(s3, 3);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt b/leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt
new file mode 100644
index 00000000..2b086e2b
--- /dev/null
+++ b/leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0005-longest-palindromic-substring
+ main.c
+ )
diff --git a/leetcode/cpp/0005.longest-palindromic-substring/main.c b/leetcode/cpp/0005.longest-palindromic-substring/main.c
new file mode 100644
index 00000000..65945e42
--- /dev/null
+++ b/leetcode/cpp/0005.longest-palindromic-substring/main.c
@@ -0,0 +1,11 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0007.reverse-integer/CMakeLists.txt b/leetcode/cpp/0007.reverse-integer/CMakeLists.txt
new file mode 100644
index 00000000..05fdf0e0
--- /dev/null
+++ b/leetcode/cpp/0007.reverse-integer/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0007-reverse-integer
+ main.c
+ )
diff --git a/leetcode/cpp/0007.reverse-integer/main.c b/leetcode/cpp/0007.reverse-integer/main.c
new file mode 100644
index 00000000..1c63e483
--- /dev/null
+++ b/leetcode/cpp/0007.reverse-integer/main.c
@@ -0,0 +1,39 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+#include
+
+int reverse(int x) {
+ int rev = 0;
+ while (x != 0) {
+ if (rev > INT_MAX / 10 || rev < INT_MIN / 10) {
+ return 0;
+ }
+ rev = rev * 10 + x % 10;
+ x /= 10;
+ }
+
+ return rev;
+}
+
+void checkSolution(int x, int expected_rev_x) {
+ int rev_x = reverse(x);
+ printf("x: %d, rev_x: %d, expected: %d\n", x, rev_x, expected_rev_x);
+ assert(rev_x == expected_rev_x);
+}
+
+int main() {
+ checkSolution(123, 321);
+ checkSolution(-123, -321);
+ checkSolution(120, 21);
+ checkSolution(1534236469, 0);
+ checkSolution(-2147483648, 0);
+ checkSolution(-2147483412, -2143847412);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt b/leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt
new file mode 100644
index 00000000..b83f2dbd
--- /dev/null
+++ b/leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0008-string-to-integer-atoi
+ main.c
+ )
diff --git a/leetcode/cpp/0008.string-to-integer-atoi/main.c b/leetcode/cpp/0008.string-to-integer-atoi/main.c
new file mode 100644
index 00000000..fcef919d
--- /dev/null
+++ b/leetcode/cpp/0008.string-to-integer-atoi/main.c
@@ -0,0 +1,79 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+int myAtoi(char* s) {
+ char* ptr = s;
+ long val = 0;
+ int is_negative = 0;
+ int has_sign = 0;
+ int digit_count = 0;
+ const long kIntMax = -(long)INT_MIN;
+
+ while (*ptr != '\0') {
+ char c = *ptr;
+ ptr += 1;
+
+ if (c == ' ' && has_sign == 0 && digit_count == 0) {
+ continue;
+ }
+
+ if (c == '-' || c == '+') {
+ if (has_sign || digit_count) {
+ // Invalid sign.
+ break;
+ }
+
+ has_sign = 1;
+ if (c == '-') {
+ is_negative = 1;
+ }
+ } else if (c >= '0' && c <= '9') {
+ int digit = c - '0';
+ val = val * 10 + digit;
+ digit_count += 1;
+ // FIXME(shaohua): overflow
+ if (val > kIntMax) {
+ val = kIntMax;
+ }
+ } else {
+ // End of valid digits.
+ break;
+ }
+ }
+
+ if (val >= kIntMax) {
+ if (is_negative) {
+ val = INT_MIN;
+ } else {
+ val = INT_MAX;
+ }
+ } else if (is_negative) {
+ val = -val;
+ }
+
+ return val;
+}
+
+void checkSolution(char* s, int exp) {
+ int val = myAtoi(s);
+ printf("val: %d, exp: %d\n", val, exp);
+ assert(val == exp);
+}
+
+int main() {
+ checkSolution("42", 42);
+ checkSolution(" -42", -42);
+ checkSolution("4193 with words", 4193);
+ checkSolution("-91283472332", -2147483648);
+ checkSolution(" +0 123", 0);
+ checkSolution("20000000000000000000", 2147483647);
+ checkSolution("-2147483647", -2147483647);
+ checkSolution("-5-", -5);
+ return 0;
+}
diff --git a/leetcode/cpp/0011.container-with-most-water/CMakeLists.txt b/leetcode/cpp/0011.container-with-most-water/CMakeLists.txt
new file mode 100644
index 00000000..232fd292
--- /dev/null
+++ b/leetcode/cpp/0011.container-with-most-water/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0011-container-with-most-water
+ main.c
+ )
diff --git a/leetcode/cpp/0011.container-with-most-water/main.c b/leetcode/cpp/0011.container-with-most-water/main.c
new file mode 100644
index 00000000..ee0d805d
--- /dev/null
+++ b/leetcode/cpp/0011.container-with-most-water/main.c
@@ -0,0 +1,44 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+int maxArea(int* height, int heightSize) {
+ assert(heightSize >= 2);
+ int max_area = 0;
+ int left = 0;
+ int right = heightSize - 1;
+ int area = 0;
+
+ while (left < right) {
+ if (height[left] < height[right]) {
+ area = (right - left) * height[left];
+ left += 1;
+ } else {
+ area = (right - left) * height[right];
+ right -= 1;
+ }
+ max_area = (max_area < area) ? area : max_area;
+ }
+
+ return max_area;
+}
+
+void checkSolution(int* height, int heightSize, int expected_area) {
+ int area = maxArea(height, heightSize);
+ printf("area: %d, expected: %d\n", area, expected_area);
+ assert(area == expected_area);
+}
+
+int main() {
+ int heigh1[] = {1, 8, 6, 2, 5, 4, 8, 3, 7};
+ checkSolution(heigh1, sizeof(heigh1) / sizeof(heigh1[0]), 49);
+
+ int height2[] = {1, 1};
+ checkSolution(height2, sizeof(height2) / sizeof(height2[0]), 1);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0014.longest-common-prefix/CMakeLists.txt b/leetcode/cpp/0014.longest-common-prefix/CMakeLists.txt
new file mode 100644
index 00000000..d4dc19b0
--- /dev/null
+++ b/leetcode/cpp/0014.longest-common-prefix/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0014-longest-common-prefix
+ main.c
+ )
diff --git a/leetcode/cpp/0014.longest-common-prefix/main.c b/leetcode/cpp/0014.longest-common-prefix/main.c
new file mode 100644
index 00000000..27d86d02
--- /dev/null
+++ b/leetcode/cpp/0014.longest-common-prefix/main.c
@@ -0,0 +1,50 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+char* longestCommonPrefix(char** strs, int strsSize) {
+ int index = 0;
+ int stop = 0;
+ while (!stop) {
+ if (strs[0][index] == '\0') {
+ break;
+ }
+
+ for (int i = 1; i < strsSize; ++i) {
+ if (strs[0][index] != strs[i][index]) {
+ stop = 1;
+ break;
+ }
+ }
+ if (!stop) {
+ index += 1;
+ }
+ }
+
+ char* out = (char*)malloc(index + 1);
+ assert(out);
+ memcpy(out, strs[0], index);
+ out[index] = '\0';
+ return out;
+}
+
+void checkSolutioin(char** strs, int strsSize, char* expected) {
+ char* got = longestCommonPrefix(strs, strsSize);
+ printf("got: %s, expected: %s\n", got, expected);
+ assert(strcmp(got, expected));
+}
+
+int main() {
+ char* strs[] = {"flower","flow","flight"};
+ checkSolutioin(strs, 3, "fl");
+
+ char* strs2[] = {"dog","racecar","car"};
+ checkSolutioin(strs2, 3, "");
+
+ return 0;
+}
diff --git a/leetcode/cpp/0015.3sum/CMakeLists.txt b/leetcode/cpp/0015.3sum/CMakeLists.txt
new file mode 100644
index 00000000..05d40b98
--- /dev/null
+++ b/leetcode/cpp/0015.3sum/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0015-3sum
+ main.cpp
+ )
diff --git a/leetcode/cpp/0015.3sum/main.cpp b/leetcode/cpp/0015.3sum/main.cpp
new file mode 100644
index 00000000..cf4276c6
--- /dev/null
+++ b/leetcode/cpp/0015.3sum/main.cpp
@@ -0,0 +1,72 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+
+#include
+#include
+#include
+
+class Solution {
+ public:
+ static
+ std::vector> threeSum(std::vector& nums) {
+ std::sort(nums.begin(), nums.end());
+ std::vector> result;
+
+ for (size_t i = 0; i < nums.size() - 2; ++i) {
+ if (nums[i] > 0) {
+ break;
+ }
+ if (i > 0 && nums[i] == nums[i-1]) {
+ continue;
+ }
+
+ int left = i + 1;
+ int right = nums.size() - 1;
+ int first = -nums[i];
+
+ while (left < right) {
+ int sum = nums[left] + nums[right];
+ if (sum < first) {
+ left += 1;
+ } else if (sum > first) {
+ right -= 1;
+ } else {
+ result.push_back({nums[i], nums[left], nums[right]});
+ while (left < right && nums[left] == nums[left + 1]) {
+ left += 1;
+ }
+ while (right > left && nums[right] == nums[right - 1]) {
+ right -= 1;
+ }
+ left += 1;
+ right -= 1;
+ }
+ }
+ }
+
+ return result;
+ }
+};
+
+void checkSolution(int* nums, int numsSize) {
+ std::vector vector(nums, nums + numsSize);
+ const auto result = Solution::threeSum(vector);
+ for (const auto& entry : result) {
+ printf("[%d, %d, %d]\n", entry[0], entry[1], entry[2]);
+ }
+}
+
+int main() {
+ int arr1[] = {-1, 0, 1, 2, -1, -4};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]));
+
+ int arr2[ ] =
+ {82597,-9243,62390,83030,-97960,-26521,-61011,83390,-38677,12333,75987,46091,83794,19355,-71037,-6242,-28801,324,1202,-90885,-2989,-95597,-34333,35528,5680,89093,-90606,50360,-29393,-27012,53313,65213,99818,-82405,-41661,-3333,-51952,72135,-1523,26377,74685,96992,92263,15929,5467,-99555,-43348,-41689,-60383,-3990,32165,65265,-72973,-58372,12741,-48568,-46596,72419,-1859,34153,62937,81310,-61823,-96770,-54944,8845,-91184,24208,-29078,31495,65258,14198,85395,70506,-40908,56740,-12228,-40072,32429,93001,68445,-73927,25731,-91859,-24150,10093,-60271,-81683,-18126,51055,48189,-6468,25057,81194,-58628,74042,66158,-14452,-49851,-43667,11092,39189,-17025,-79173,13606,83172,92647,-59741,19343,-26644,-57607,82908,-20655,1637,80060,98994,39331,-31274,-61523,91225,-72953,13211,-75116,-98421,-41571,-69074,99587,39345,42151,-2460,98236,15690,-52507,-95803,-48935,-46492,-45606,-79254,-99851,52533,73486,39948,-7240,71815,-585,-96252,90990,-93815,93340,-71848,58733,-14859,-83082,-75794,-82082,-24871,-15206,91207,-56469,-93618,67131,-8682,75719,87429,-98757,-7535,-24890,-94160,85003,33928,75538,97456,-66424,-60074,-8527,-28697,-22308,2246,-70134,-82319,-10184,87081,-34949,-28645,-47352,-83966,-60418,-15293,-53067,-25921,55172,75064,95859,48049,34311,-86931,-38586,33686,-36714,96922,76713,-22165,-80585,-34503,-44516,39217,-28457,47227,-94036,43457,24626,-87359,26898,-70819,30528,-32397,-69486,84912,-1187,-98986,-32958,4280,-79129,-65604,9344,58964,50584,71128,-55480,24986,15086,-62360,-42977,-49482,-77256,-36895,-74818,20,3063,-49426,28152,-97329,6086,86035,-88743,35241,44249,19927,-10660,89404,24179,-26621,-6511,57745,-28750,96340,-97160,-97822,-49979,52307,79462,94273,-24808,77104,9255,-83057,77655,21361,55956,-9096,48599,-40490,-55107,2689,29608,20497,66834,-34678,23553,-81400,-66630,-96321,-34499,-12957,-20564,25610,-4322,-58462,20801,53700,71527,24669,-54534,57879,-3221,33636,3900,97832,-27688,-98715,5992,24520,-55401,-57613,-69926,57377,-77610,20123,52174,860,60429,-91994,-62403,-6218,-90610,-37263,-15052,62069,-96465,44254,89892,-3406,19121,-41842,-87783,-64125,-56120,73904,-22797,-58118,-4866,5356,75318,46119,21276,-19246,-9241,-97425,57333,-15802,93149,25689,-5532,95716,39209,-87672,-29470,-16324,-15331,27632,-39454,56530,-16000,29853,46475,78242,-46602,83192,-73440,-15816,50964,-36601,89758,38375,-40007,-36675,-94030,67576,46811,-64919,45595,76530,40398,35845,41791,67697,-30439,-82944,63115,33447,-36046,-50122,-34789,43003,-78947,-38763,-89210,32756,-20389,-31358,-90526,-81607,88741,86643,98422,47389,-75189,13091,95993,-15501,94260,-25584,-1483,-67261,-70753,25160,89614,-90620,-48542,83889,-12388,-9642,-37043,-67663,28794,-8801,13621,12241,55379,84290,21692,-95906,-85617,-17341,-63767,80183,-4942,-51478,30997,-13658,8838,17452,-82869,-39897,68449,31964,98158,-49489,62283,-62209,-92792,-59342,55146,-38533,20496,62667,62593,36095,-12470,5453,-50451,74716,-17902,3302,-16760,-71642,-34819,96459,-72860,21638,47342,-69897,-40180,44466,76496,84659,13848,-91600,-90887,-63742,-2156,-84981,-99280,94326,-33854,92029,-50811,98711,-36459,-75555,79110,-88164,-97397,-84217,97457,64387,30513,-53190,-83215,252,2344,-27177,-92945,-89010,82662,-11670,86069,53417,42702,97082,3695,-14530,-46334,17910,77999,28009,-12374,15498,-46941,97088,-35030,95040,92095,-59469,-24761,46491,67357,-66658,37446,-65130,-50416,99197,30925,27308,54122,-44719,12582,-99525,-38446,-69050,-22352,94757,-56062,33684,-40199,-46399,96842,-50881,-22380,-65021,40582,53623,-76034,77018,-97074,-84838,-22953,-74205,79715,-33920,-35794,-91369,73421,-82492,63680,-14915,-33295,37145,76852,-69442,60125,-74166,74308,-1900,-30195,-16267,-60781,-27760,5852,38917,25742,-3765,49097,-63541,98612,-92865,-30248,9612,-8798,53262,95781,-42278,-36529,7252,-27394,-5021,59178,80934,-48480,-75131,-54439,-19145,-48140,98457,-6601,-51616,-89730,78028,32083,-48904,16822,-81153,-8832,48720,-80728,-45133,-86647,-4259,-40453,2590,28613,50523,-4105,-27790,-74579,-17223,63721,33489,-47921,97628,-97691,-14782,-65644,18008,-93651,-71266,80990,-76732,-47104,35368,28632,59818,-86269,-89753,34557,-92230,-5933,-3487,-73557,-13174,-43981,-43630,-55171,30254,-83710,-99583,-13500,71787,5017,-25117,-78586,86941,-3251,-23867,-36315,75973,86272,-45575,77462,-98836,-10859,70168,-32971,-38739,-12761,93410,14014,-30706,-77356,-85965,-62316,63918,-59914,-64088,1591,-10957,38004,15129,-83602,-51791,34381,-89382,-26056,8942,5465,71458,-73805,-87445,-19921,-80784,69150,-34168,28301,-68955,18041,6059,82342,9947,39795,44047,-57313,48569,81936,-2863,-80932,32976,-86454,-84207,33033,32867,9104,-16580,-25727,80157,-70169,53741,86522,84651,68480,84018,61932,7332,-61322,-69663,76370,41206,12326,-34689,17016,82975,-23386,39417,72793,44774,-96259,3213,79952,29265,-61492,-49337,14162,65886,3342,-41622,-62659,-90402,-24751,88511,54739,-21383,-40161,-96610,-24944,-602,-76842,-21856,69964,43994,-15121,-85530,12718,13170,-13547,69222,62417,-75305,-81446,-38786,-52075,-23110,97681,-82800,-53178,11474,35857,94197,-58148,-23689,32506,92154,-64536,-73930,-77138,97446,-83459,70963,22452,68472,-3728,-25059,-49405,95129,-6167,12808,99918,30113,-12641,-26665,86362,-33505,50661,26714,33701,89012,-91540,40517,-12716,-57185,-87230,29914,-59560,13200,-72723,58272,23913,-45586,-96593,-26265,-2141,31087,81399,92511,-34049,20577,2803,26003,8940,42117,40887,-82715,38269,40969,-50022,72088,21291,-67280,-16523,90535,18669,94342,-39568,-88080,-99486,-20716,23108,-28037,63342,36863,-29420,-44016,75135,73415,16059,-4899,86893,43136,-7041,33483,-67612,25327,40830,6184,61805,4247,81119,-22854,-26104,-63466,63093,-63685,60369,51023,51644,-16350,74438,-83514,99083,10079,-58451,-79621,48471,67131,-86940,99093,11855,-22272,-67683,-44371,9541,18123,37766,-70922,80385,-57513,-76021,-47890,36154,72935,84387,-92681,-88303,-7810,59902,-90,-64704,-28396,-66403,8860,13343,33882,85680,7228,28160,-14003,54369,-58893,92606,-63492,-10101,64714,58486,29948,-44679,-22763,10151,-56695,4031,-18242,-36232,86168,-14263,9883,47124,47271,92761,-24958,-73263,-79661,-69147,-18874,29546,-92588,-85771,26451,-86650,-43306,-59094,-47492,-34821,-91763,-47670,33537,22843,67417,-759,92159,63075,94065,-26988,55276,65903,30414,-67129,-99508,-83092,-91493,-50426,14349,-83216,-76090,32742,-5306,-93310,-60750,-60620,-45484,-21108,-58341,-28048,-52803,69735,78906,81649,32565,-86804,-83202,-65688,-1760,89707,93322,-72750,84134,71900,-37720,19450,-78018,22001,-23604,26276,-21498,65892,-72117,-89834,-23867,55817,-77963,42518,93123,-83916,63260,-2243,-97108,85442,-36775,17984,-58810,99664,-19082,93075,-69329,87061,79713,16296,70996,13483,-74582,49900,-27669,-40562,1209,-20572,34660,83193,75579,7344,64925,88361,60969,3114,44611,-27445,53049,-16085,-92851,-53306,13859,-33532,86622,-75666,-18159,-98256,51875,-42251,-27977,-18080,23772,38160,41779,9147,94175,99905,-85755,62535,-88412,-52038,-68171,93255,-44684,-11242,-104,31796,62346,-54931,-55790,-70032,46221,56541,-91947,90592,93503,4071,20646,4856,-63598,15396,-50708,32138,-85164,38528,-89959,53852,57915,-42421,-88916,-75072,67030,-29066,49542,-71591,61708,-53985,-43051,28483,46991,-83216,80991,-46254,-48716,39356,-8270,-47763,-34410,874,-1186,-7049,28846,11276,21960,-13304,-11433,-4913,55754,79616,70423,-27523,64803,49277,14906,-97401,-92390,91075,70736,21971,-3303,55333,-93996,76538,54603,-75899,98801,46887,35041,48302,-52318,55439,24574,14079,-24889,83440,14961,34312,-89260,-22293,-81271,-2586,-71059,-10640,-93095,-5453,-70041,66543,74012,-11662,-52477,-37597,-70919,92971,-17452,-67306,-80418,7225,-89296,24296,86547,37154,-10696,74436,-63959,58860,33590,-88925,-97814,-83664,85484,-8385,-50879,57729,-74728,-87852,-15524,-91120,22062,28134,80917,32026,49707,-54252,-44319,-35139,13777,44660,85274,25043,58781,-89035,-76274,6364,-63625,72855,43242,-35033,12820,-27460,77372,-47578,-61162,-70758,-1343,-4159,64935,56024,-2151,43770,19758,-30186,-86040,24666,-62332,-67542,73180,-25821,-27826,-45504,-36858,-12041,20017,-24066,-56625,-52097,-47239,-90694,8959,7712,-14258,-5860,55349,61808,-4423,-93703,64681,-98641,-25222,46999,-83831,-54714,19997,-68477,66073,51801,-66491,52061,-52866,79907,-39736,-68331,68937,91464,98892,910,93501,31295,-85873,27036,-57340,50412,21,-2445,29471,71317,82093,-94823,-54458,-97410,39560,-7628,66452,39701,54029,37906,46773,58296,60370,-61090,85501,-86874,71443,-72702,-72047,14848,34102,77975,-66294,-36576,31349,52493,-70833,-80287,94435,39745,-98291,84524,-18942,10236,93448,50846,94023,-6939,47999,14740,30165,81048,84935,-19177,-13594,32289,62628,-90612,-542,-66627,64255,71199,-83841,-82943,-73885,8623,-67214,-9474,-35249,62254,-14087,-90969,21515,-83303,94377,-91619,19956,-98810,96727,-91939,29119,-85473,-82153,-69008,44850,74299,-76459,-86464,8315,-49912,-28665,59052,-69708,76024,-92738,50098,18683,-91438,18096,-19335,35659,91826,15779,-73070,67873,-12458,-71440,-46721,54856,97212,-81875,35805,36952,68498,81627,-34231,81712,27100,-9741,-82612,18766,-36392,2759,41728,69743,26825,48355,-17790,17165,56558,3295,-24375,55669,-16109,24079,73414,48990,-11931,-78214,90745,19878,35673,-15317,-89086,94675,-92513,88410,-93248,-19475,-74041,-19165,32329,-26266,-46828,-18747,45328,8990,-78219,-25874,-74801,-44956,-54577,-29756,-99822,-35731,-18348,-68915,-83518,-53451,95471,-2954,-13706,-8763,-21642,-37210,16814,-60070,-42743,27697,-36333,-42362,11576,85742,-82536,68767,-56103,-63012,71396,-78464,-68101,-15917,-11113,-3596,77626,-60191,-30585,-73584,6214,-84303,18403,23618,-15619,-89755,-59515,-59103,-74308,-63725,-29364,-52376,-96130,70894,-12609,50845,-2314,42264,-70825,64481,55752,4460,-68603,-88701,4713,-50441,-51333,-77907,97412,-66616,-49430,60489,-85262,-97621,-18980,44727,-69321,-57730,66287,-92566,-64427,-14270,11515,-92612,-87645,61557,24197,-81923,-39831,-10301,-23640,-76219,-68025,92761,-76493,68554,-77734,-95620,-11753,-51700,98234,-68544,-61838,29467,46603,-18221,-35441,74537,40327,-58293,75755,-57301,-7532,-94163,18179,-14388,-22258,-46417,-48285,18242,-77551,82620,250,-20060,-79568,-77259,82052,-98897,-75464,48773,-79040,-11293,45941,-67876,-69204,-46477,-46107,792,60546,-34573,-12879,-94562,20356,-48004,-62429,96242,40594,2099,99494,25724,-39394,-2388,-18563,-56510,-83570,-29214,3015,74454,74197,76678,-46597,60630,-76093,37578,-82045,-24077,62082,-87787,-74936,58687,12200,-98952,70155,-77370,21710,-84625,-60556,-84128,925,65474,-15741,-94619,88377,89334,44749,22002,-45750,-93081,-14600,-83447,46691,85040,-66447,-80085,56308,44310,24979,-29694,57991,4675,-71273,-44508,13615,-54710,23552,-78253,-34637,50497,68706,81543,-88408,-21405,6001,-33834,-21570,-46692,-25344,20310,71258,-97680,11721,59977,59247,-48949,98955,-50276,-80844,-27935,-76102,55858,-33492,40680,66691,-33188,8284,64893,-7528,6019,-85523,8434,-64366,-56663,26862,30008,-7611,-12179,-70076,21426,-11261,-36864,-61937,-59677,929,-21052,3848,-20888,-16065,98995,-32293,-86121,-54564,77831,68602,74977,31658,40699,29755,98424,80358,-69337,26339,13213,-46016,-18331,64713,-46883,-58451,-70024,-92393,-4088,70628,-51185,71164,-75791,-1636,-29102,-16929,-87650,-84589,-24229,-42137,-15653,94825,13042,88499,-47100,-90358,-7180,29754,-65727,-42659,-85560,-9037,-52459,20997,-47425,17318,21122,20472,-23037,65216,-63625,-7877,-91907,24100,-72516,22903,-85247,-8938,73878,54953,87480,-31466,-99524,35369,-78376,89984,-15982,94045,-7269,23319,-80456,-37653,-76756,2909,81936,54958,-12393,60560,-84664,-82413,66941,-26573,-97532,64460,18593,-85789,-38820,-92575,-43663,-89435,83272,-50585,13616,-71541,-53156,727,-27644,16538,34049,57745,34348,35009,16634,-18791,23271,-63844,95817,21781,16590,59669,15966,-6864,48050,-36143,97427,-59390,96931,78939,-1958,50777,43338,-51149,39235,-27054,-43492,67457,-83616,37179,10390,85818,2391,73635,87579,-49127,-81264,-79023,-81590,53554,-74972,-83940,-13726,-39095,29174,78072,76104,47778,25797,-29515,-6493,-92793,22481,-36197,-65560,42342,15750,97556,99634,-56048,-35688,13501,63969,-74291,50911,39225,93702,-3490,-59461,-30105,-46761,-80113,92906,-68487,50742,36152,-90240,-83631,24597,-50566,-15477,18470,77038,40223,-80364,-98676,70957,-63647,99537,13041,31679,86631,37633,-16866,13686,-71565,21652,-46053,-80578,-61382,68487,-6417,4656,20811,67013,-30868,-11219,46,74944,14627,56965,42275,-52480,52162,-84883,-52579,-90331,92792,42184,-73422,-58440,65308,-25069,5475,-57996,59557,-17561,2826,-56939,14996,-94855,-53707,99159,43645,-67719,-1331,21412,41704,31612,32622,1919,-69333,-69828,22422,-78842,57896,-17363,27979,-76897,35008,46482,-75289,65799,20057,7170,41326,-76069,90840,-81253,-50749,3649,-42315,45238,-33924,62101,96906,58884,-7617,-28689,-66578,62458,50876,-57553,6739,41014,-64040,-34916,37940,13048,-97478,-11318,-89440,-31933,-40357,-59737,-76718,-14104,-31774,28001,4103,41702,-25120,-31654,63085,-3642,84870,-83896,-76422,-61520,12900,88678,85547,33132,-88627,52820,63915,-27472,78867,-51439,33005,-23447,-3271,-39308,39726,-74260,-31874,-36893,93656,910,-98362,60450,-88048,99308,13947,83996,-90415,-35117,70858,-55332,-31721,97528,82982,-86218,6822,25227,36946,97077,-4257,-41526,56795,89870,75860,-70802,21779,14184,-16511,-89156,-31422,71470,69600,-78498,74079,-19410,40311,28501,26397,-67574,-32518,68510,38615,19355,-6088,-97159,-29255,-92523,3023,-42536,-88681,64255,41206,44119,52208,39522,-52108,91276,-70514,83436,63289,-79741,9623,99559,12642,85950,83735,-21156,-67208,98088,-7341,-27763,-30048,-44099,-14866,-45504,-91704,19369,13700,10481,-49344,-85686,33994,19672,36028,60842,66564,-24919,33950,-93616,-47430,-35391,-28279,56806,74690,39284,-96683,-7642,-75232,37657,-14531,-86870,-9274,-26173,98640,88652,64257,46457,37814,-19370,9337,-22556,-41525,39105,-28719,51611,-93252,98044,-90996,21710,-47605,-64259,-32727,53611,-31918,-3555,33316,-66472,21274,-37731,-2919,15016,48779,-88868,1897,41728,46344,-89667,37848,68092,-44011,85354,-43776,38739,-31423,-66330,65167,-22016,59405,34328,-60042,87660,-67698,-59174,-1408,-46809,-43485,-88807,-60489,13974,22319,55836,-62995,-37375,-4185,32687,-36551,-75237,58280,26942,-73756,71756,78775,-40573,14367,-71622,-77338,24112,23414,-7679,-51721,87492,85066,-21612,57045,10673,-96836,52461,-62218,-9310,65862,-22748,89906,-96987,-98698,26956,-43428,46141,47456,28095,55952,67323,-36455,-60202,-43302,-82932,42020,77036,10142,60406,70331,63836,58850,-66752,52109,21395,-10238,-98647,-41962,27778,69060,98535,-28680,-52263,-56679,66103,-42426,27203,80021,10153,58678,36398,63112,34911,20515,62082,-15659,-40785,27054,43767,-20289,65838,-6954,-60228,-72226,52236,-35464,25209,-15462,-79617,-41668,-84083,62404,-69062,18913,46545,20757,13805,24717,-18461,-47009,-25779,68834,64824,34473,39576,31570,14861,-15114,-41233,95509,68232,67846,84902,-83060,17642,-18422,73688,77671,-26930,64484,-99637,73875,6428,21034,-73471,19664,-68031,15922,-27028,48137,54955,-82793,-41144,-10218,-24921,-28299,-2288,68518,-54452,15686,-41814,66165,-72207,-61986,80020,50544,-99500,16244,78998,40989,14525,-56061,-24692,-94790,21111,37296,-90794,72100,70550,-31757,17708,-74290,61910,78039,-78629,-25033,73172,-91953,10052,64502,99585,-1741,90324,-73723,68942,28149,30218,24422,16659,10710,-62594,94249,96588,46192,34251,73500,-65995,-81168,41412,-98724,-63710,-54696,-52407,19746,45869,27821,-94866,-76705,-13417,-61995,-71560,43450,67384,-8838,-80293,-28937,23330,-89694,-40586,46918,80429,-5475,78013,25309,-34162,37236,-77577,86744,26281,-29033,-91813,35347,13033,-13631,-24459,3325,-71078,-75359,81311,19700,47678,-74680,-84113,45192,35502,37675,19553,76522,-51098,-18211,89717,4508,-82946,27749,85995,89912,-53678,-64727,-14778,32075,-63412,-40524,86440,-2707,-36821,63850,-30883,67294,-99468,-23708,34932,34386,98899,29239,-23385,5897,54882,98660,49098,70275,17718,88533,52161,63340,50061,-89457,19491,-99156,24873,-17008,64610,-55543,50495,17056,-10400,-56678,-29073,-42960,-76418,98562,-88104,-96255,10159,-90724,54011,12052,45871,-90933,-69420,67039,37202,78051,-52197,-40278,-58425,65414,-23394,-1415,6912,-53447,7352,17307,-78147,63727,98905,55412,-57658,-32884,-44878,22755,39730,3638,35111,39777,74193,38736,-11829,-61188,-92757,55946,-71232,-63032,-83947,39147,-96684,-99233,25131,-32197,24406,-55428,-61941,25874,-69453,64483,-19644,-68441,12783,87338,-48676,66451,-447,-61590,50932,-11270,29035,65698,-63544,10029,80499,-9461,86368,91365,-81810,-71914,-52056,-13782,44240,-30093,-2437,24007,67581,-17365,-69164,-8420,-69289,-29370,48010,90439,13141,69243,50668,39328,61731,78266,-81313,17921,-38196,55261,9948,-24970,75712,-72106,28696,7461,31621,61047,51476,56512,11839,-96916,-82739,28924,-99927,58449,37280,69357,11219,-32119,-62050,-48745,-83486,-52376,42668,82659,68882,38773,46269,-96005,97630,25009,-2951,-67811,99801,81587,-79793,-18547,-83086,69512,33127,-92145,-88497,47703,59527,1909,88785,-88882,69188,-46131,-5589,-15086,36255,-53238,-33009,82664,53901,35939,-42946,-25571,33298,69291,53199,74746,-40127,-39050,91033,51717,-98048,87240,36172,65453,-94425,-63694,-30027,59004,88660,3649,-20267,-52565,-67321,34037,4320,91515,-56753,60115,27134,68617,-61395,-26503,-98929,-8849,-63318,10709,-16151,61905,-95785,5262,23670,-25277,90206,-19391,45735,37208,-31992,-92450,18516,-90452,-58870,-58602,93383,14333,17994,82411,-54126,-32576,35440,-60526,-78764,-25069,-9022,-394,92186,-38057,55328,-61569,67780,77169,19546,-92664,-94948,44484,-13439,83529,27518,-48333,72998,38342,-90553,-98578,-76906,81515,-16464,78439,92529,35225,-39968,-10130,-7845,-32245,-74955,-74996,67731,-13897,-82493,33407,93619,59560,-24404,-57553,19486,-45341,34098,-24978,-33612,79058,71847,76713,-95422,6421,-96075,-59130,-28976,-16922,-62203,69970,68331,21874,40551,89650,51908,58181,66480,-68177,34323,-3046,-49656,-59758,43564,-10960,-30796,15473,-20216,46085,-85355,41515,-30669,-87498,57711,56067,63199,-83805,62042,91213,-14606,4394,-562,74913,10406,96810,-61595,32564,31640,-9732,42058,98052,-7908,-72330,1558,-80301,34878,32900,3939,-8824,88316,20937,21566,-3218,-66080,-31620,86859,54289,90476,-42889,-15016,-18838,75456,30159,-67101,42328,-92703,85850,-5475,23470,-80806,68206,17764,88235,46421,-41578,74005,-81142,80545,20868,-1560,64017,83784,68863,-97516,-13016,-72223,79630,-55692,82255,88467,28007,-34686,-69049,-41677,88535,-8217,68060,-51280,28971,49088,49235,26905,-81117,-44888,40623,74337,-24662,97476,79542,-72082,-35093,98175,-61761,-68169,59697,-62542,-72965,59883,-64026,-37656,-92392,-12113,-73495,98258,68379,-21545,64607,-70957,-92254,-97460,-63436,-8853,-19357,-51965,-76582,12687,-49712,45413,-60043,33496,31539,-57347,41837,67280,-68813,52088,-13155,-86430,-15239,-45030,96041,18749,-23992,46048,35243,-79450,85425,-58524,88781,-39454,53073,-48864,-82289,39086,82540,-11555,25014,-5431,-39585,-89526,2705,31953,-81611,36985,-56022,68684,-27101,11422,64655,-26965,-63081,-13840,-91003,-78147,-8966,41488,1988,99021,-61575,-47060,65260,-23844,-21781,-91865,-19607,44808,2890,63692,-88663,-58272,15970,-65195,-45416,-48444,-78226,-65332,-24568,42833,-1806,-71595,80002,-52250,30952,48452,-90106,31015,-22073,62339,63318,78391,28699,77900,-4026,-76870,-45943,33665,9174,-84360,-22684,-16832,-67949,-38077,-38987,-32847,51443,-53580,-13505,9344,-92337,26585,70458,-52764,-67471,-68411,-1119,-2072,-93476,67981,40887,-89304,-12235,41488,1454,5355,-34855,-72080,24514,-58305,3340,34331,8731,77451,-64983,-57876,82874,62481,-32754,-39902,22451,-79095,-23904,78409,-7418,77916};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]));
+
+ return 0;
+}
diff --git a/leetcode/cpp/0016.3sum-closest/CMakeLists.txt b/leetcode/cpp/0016.3sum-closest/CMakeLists.txt
new file mode 100644
index 00000000..6a4c3bf7
--- /dev/null
+++ b/leetcode/cpp/0016.3sum-closest/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0016-3sum-closest
+ main.cpp
+ )
diff --git a/leetcode/cpp/0016.3sum-closest/main.cpp b/leetcode/cpp/0016.3sum-closest/main.cpp
new file mode 100644
index 00000000..a553bece
--- /dev/null
+++ b/leetcode/cpp/0016.3sum-closest/main.cpp
@@ -0,0 +1,59 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+
+#include
+#include
+
+class Solution {
+ public:
+ static
+ int threeSumClosest(std::vector& nums, int target) {
+ std::sort(nums.begin(), nums.end());
+ int min_diff = INT_MAX;
+ int result = 0;
+
+ for (size_t i = 0; i < nums.size() - 2; ++i) {
+ int left = i + 1;
+ int right = nums.size() - 1;
+ int first = nums[i];
+
+ while (left < right) {
+ int sum = first + nums[left] + nums[right];
+ if (sum == target) {
+ return sum;
+ }
+
+ int d = std::abs(sum - target);
+ if (d < min_diff) {
+ min_diff = d;
+ result = sum;
+ }
+ if (sum < target) {
+ left += 1;
+ } else if (sum > target) {
+ right -= 1;
+ }
+ }
+ }
+
+ return result;
+ }
+
+};
+
+void checkSolution(int* nums, int numsSize, int target, int expectedSum) {
+ std::vector vector(nums, nums + numsSize);
+ int sum = Solution::threeSumClosest(vector, target);
+ assert(sum == expectedSum);
+}
+
+int main() {
+ int arr1[] = {-1, 2, 1, -4};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 1, 2);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0018.4sum/CMakeLists.txt b/leetcode/cpp/0018.4sum/CMakeLists.txt
new file mode 100644
index 00000000..051c4881
--- /dev/null
+++ b/leetcode/cpp/0018.4sum/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0018-4sum
+ main.cpp
+ )
diff --git a/leetcode/cpp/0018.4sum/main.cpp b/leetcode/cpp/0018.4sum/main.cpp
new file mode 100644
index 00000000..48cfe3bc
--- /dev/null
+++ b/leetcode/cpp/0018.4sum/main.cpp
@@ -0,0 +1,82 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+
+#include
+#include
+#include
+
+class Solution {
+public:
+ static
+ std::vector> fourSum(std::vector& nums, int target) {
+ int len = nums.size();
+ std::vector> result;
+ std::sort(nums.begin(), nums.end());
+
+ for (int i = 0; i < len-3; ++i) {
+ if (i > 0 && nums[i] == nums[i-1]) {
+ continue;
+ }
+
+ for (int j = i+1; j < len-2; ++j) {
+ if (j > i + 1 && nums[j] == nums[j-1]) {
+ continue;
+ }
+
+ int left = j + 1;
+ int right = len - 1;
+ long remains = (long)target - (long)nums[i] - (long)nums[j];
+ while (left < right) {
+ long sum = (long)nums[left] + (long)nums[right];
+ if (remains < sum) {
+ right -= 1;
+ } else if (remains > sum) {
+ left += 1;
+ } else {
+ result.push_back({nums[i], nums[j], nums[left], nums[right]});
+ while (left < right && nums[left] == nums[left+1]) {
+ left += 1;
+ }
+ while (left < right && nums[right] == nums[right-1]) {
+ right -= 1;
+ }
+ left += 1;
+ right -= 1;
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+};
+
+void checkSolution(int* arr, int arr_len, int target, int out_len) {
+ std::vector nums{arr, arr + arr_len};
+ std::vector> result = Solution::fourSum(nums, target);
+ for (const auto& entry : result) {
+ printf("[%d, %d, %d, %d]\n", entry[0], entry[1], entry[2], entry[3]);
+ }
+ printf("result size: %d, expected; %d\n", result.size(), out_len);
+ assert(result.size() == out_len);
+}
+
+int main() {
+ int arr1[] = {1, 0, -1, 0, -2, 2};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 0, 3);
+
+ int arr2[] = {2, 2, 2, 2, 2};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 8, 1);
+
+ int arr3[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
+ checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 8, 1);
+
+ int arr4[] = {1, -2, -5, -4, -3, 3, 3, 5};
+ checkSolution(arr4, sizeof(arr4) / sizeof(arr4[0]), -11, 1);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0019.remove-nth-node-from-end-of-list/CMakeLists.txt b/leetcode/cpp/0019.remove-nth-node-from-end-of-list/CMakeLists.txt
new file mode 100644
index 00000000..a1c73c62
--- /dev/null
+++ b/leetcode/cpp/0019.remove-nth-node-from-end-of-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0019-remove-nth-node-from-end-of-list
+ main.c
+ )
diff --git a/leetcode/cpp/0019.remove-nth-node-from-end-of-list/main.c b/leetcode/cpp/0019.remove-nth-node-from-end-of-list/main.c
new file mode 100644
index 00000000..2dfbc678
--- /dev/null
+++ b/leetcode/cpp/0019.remove-nth-node-from-end-of-list/main.c
@@ -0,0 +1,143 @@
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+static struct ListNode* listNodeNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+static struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNodeNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+static int listSize(struct ListNode* list) {
+ int size = 0;
+ while (list != NULL) {
+ size += 1;
+ list = list->next;
+ }
+ return size;
+}
+
+static struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
+ assert(head != NULL);
+ assert(n >= 0);
+
+ struct ListNode* tmp = NULL;
+ int size = listSize(head);
+ assert(n <= size);
+
+ if (size == n) {
+ // Remove head node.
+ tmp = head;
+ head = head->next;
+ free(tmp);
+ return head;
+ }
+
+ int index = 0;
+ struct ListNode* head_clone = head;
+ while (index + 1 < size - n && head != NULL) {
+ head = head->next;
+ index += 1;
+ }
+ assert(head != NULL);
+ if (head->next != NULL) {
+ tmp = head->next;
+ head->next = head->next->next;
+ free(tmp);
+ }
+
+ return head_clone;
+}
+
+static struct ListNode* removeNthFromEnd2Helper(struct ListNode* current, int n, int* count) {
+ // End of list.
+ if (current == NULL) {
+ return NULL;
+ }
+
+ struct ListNode* next = removeNthFromEnd2Helper(current->next, n, count);
+ *count += 1;
+ if (*count == n) {
+ free(current);
+ return next;
+ }
+
+ current->next = next;
+ return current;
+}
+
+static struct ListNode* removeNthFromEnd2(struct ListNode* head, int n) {
+ int count = 0;
+ return removeNthFromEnd2Helper(head, n, &count);
+}
+
+static struct ListNode* removeNthFromEnd3(struct ListNode* head, int n) {
+ assert(n >= 0);
+ if (head == NULL) {
+ return NULL;
+ }
+
+ // Used for counting.
+ struct ListNode* fast = head;
+ struct ListNode* slow = head;
+
+ for (int i = 0; i < n; ++i) {
+ fast = fast->next;
+ }
+ if (fast == NULL) {
+ return NULL;
+ }
+
+ while (fast->next != NULL) {
+ fast = fast->next;
+ slow = slow->next;
+ }
+ struct ListNode* tmp = slow->next;
+ assert(tmp != NULL);
+ slow->next = slow->next->next;
+ free(tmp);
+ return head;
+}
+
+static void checkSolution(int arr[], int arr_len, int target) {
+ struct ListNode* l1 = listFromArray(arr, arr_len);
+ //struct ListNode* l2 = removeNthFromEnd(l1, target);
+ struct ListNode* l2 = removeNthFromEnd2(l1, target);
+ //struct ListNode* l2 = removeNthFromEnd3(l1, target);
+ while (l2 != NULL) {
+ printf("%d, ", l2->val);
+ l2 = l2->next;
+ }
+ printf("\n");
+}
+
+int main() {
+ int arr1[] = {1, 2, 3, 4, 5};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 2);
+
+ int arr2[] = {1};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 1);
+
+ int arr3[] = {1, 2};
+ checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 1);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0020.valid-parentheses/CMakeLists.txt b/leetcode/cpp/0020.valid-parentheses/CMakeLists.txt
new file mode 100644
index 00000000..540e3485
--- /dev/null
+++ b/leetcode/cpp/0020.valid-parentheses/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0020-valid-parentheses
+ main.cpp
+ )
diff --git a/leetcode/cpp/0020.valid-parentheses/main.cpp b/leetcode/cpp/0020.valid-parentheses/main.cpp
new file mode 100644
index 00000000..aa98be98
--- /dev/null
+++ b/leetcode/cpp/0020.valid-parentheses/main.cpp
@@ -0,0 +1,43 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+
+#include
+#include
+
+bool isValid(std::string s) {
+ std::stack stack;
+
+ for (char c : s) {
+ if (c == '(' || c == '[' || c == '{') {
+ stack.push(c);
+ } else {
+ if (stack.empty()) {
+ return false;
+ }
+ char old_c = stack.top();
+ stack.pop();
+ if ((old_c == '(' && c != ')') ||
+ (old_c == '[' && c != ']') ||
+ (old_c == '{' && c != '}')) {
+ return false;
+ }
+ }
+ }
+
+ return stack.empty();
+}
+
+void checkSolution(char* s, bool exp_valid) {
+ bool is_valid = isValid(s);
+ assert(is_valid == exp_valid);
+}
+
+int main() {
+ checkSolution("()", true);
+ checkSolution("()[]{}", true);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0021.merge-two-sorted-lists/CMakeLists.txt b/leetcode/cpp/0021.merge-two-sorted-lists/CMakeLists.txt
new file mode 100644
index 00000000..cb2b96ec
--- /dev/null
+++ b/leetcode/cpp/0021.merge-two-sorted-lists/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0021-merge-two-sorted-lists
+ main.c
+ )
diff --git a/leetcode/cpp/0021.merge-two-sorted-lists/main.c b/leetcode/cpp/0021.merge-two-sorted-lists/main.c
new file mode 100644
index 00000000..694667fd
--- /dev/null
+++ b/leetcode/cpp/0021.merge-two-sorted-lists/main.c
@@ -0,0 +1,93 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode *next;
+};
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = arr[i];
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+static
+struct ListNode* listReverse(struct ListNode* list) {
+ assert(list != NULL);
+ struct ListNode* prev = NULL;
+ struct ListNode* next = NULL;
+ while (list != NULL) {
+ next = list->next;
+ list->next = prev;
+ prev = list;
+ list = next;
+ }
+ return prev;
+}
+
+struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
+ if (l1 == NULL) {
+ return l2;
+ }
+ if (l2 == NULL) {
+ return l1;
+ }
+ l1 = listReverse(l1);
+ l2 = listReverse(l2);
+
+ struct ListNode* l3 = NULL;
+ struct ListNode* node = NULL;
+
+ while (l1 != NULL && l2 != NULL) {
+ if (l1->val >= l2->val) {
+ node = l1;
+ l1 = l1->next;
+ } else {
+ node = l2;
+ l2 = l2->next;
+ }
+ node->next = l3;
+ l3 = node;
+ }
+ while (l1 != NULL) {
+ node = l1;
+ l1 = l1->next;
+ node->next = l3;
+ l3 = node;
+ }
+ while (l2 != NULL) {
+ node = l2;
+ l2 = l2->next;
+ node->next = l3;
+ l3 = node;
+ }
+
+ return l3;
+}
+
+int main() {
+ int arr1[] = {1, 2, 4};
+ int arr2[] = {1, 3, 4};
+ struct ListNode* l1 = listFromArray(arr1, sizeof(arr1) / sizeof(arr1[0]));
+ struct ListNode* l2 = listFromArray(arr2, sizeof(arr2) / sizeof(arr2[0]));
+ struct ListNode* l3 = mergeTwoLists(l1, l2);
+ while (l3 != NULL) {
+ printf("%d, ", l3->val);
+ l3 = l3->next;
+ }
+ printf("\n");
+
+ return 0;
+}
diff --git a/leetcode/cpp/0025.reverse-nodes-in-k-group/CMakeLists.txt b/leetcode/cpp/0025.reverse-nodes-in-k-group/CMakeLists.txt
new file mode 100644
index 00000000..79e865c3
--- /dev/null
+++ b/leetcode/cpp/0025.reverse-nodes-in-k-group/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0025.reverse-nodes-in-k-group
+ main.c
+ )
diff --git a/leetcode/cpp/0026.remove-duplicates-from-sorted-array/CMakeLists.txt b/leetcode/cpp/0026.remove-duplicates-from-sorted-array/CMakeLists.txt
new file mode 100644
index 00000000..ca808660
--- /dev/null
+++ b/leetcode/cpp/0026.remove-duplicates-from-sorted-array/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0026-remove-duplicates-from-sorted-array
+ main.c
+ )
diff --git a/leetcode/cpp/0026.remove-duplicates-from-sorted-array/main.c b/leetcode/cpp/0026.remove-duplicates-from-sorted-array/main.c
new file mode 100644
index 00000000..2aebc3b8
--- /dev/null
+++ b/leetcode/cpp/0026.remove-duplicates-from-sorted-array/main.c
@@ -0,0 +1,41 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+int removeDuplicates(int* nums, int numsSize) {
+ int* slow = nums;
+ int* fast = nums;
+
+ for (int i = 0; i < numsSize; ++i) {
+ // Skip duplicate elements
+ while (i+1 < numsSize && fast[i] == fast[i+1]) {
+ i += 1;
+ }
+ *slow = fast[i];
+ slow += 1;
+ }
+
+ return slow - nums;
+}
+
+void checkSolution(int* nums, int numsSize) {
+ int k = removeDuplicates(nums, numsSize);
+ for (int i = 0; i < k; ++i) {
+ printf("%d, ", nums[i]);
+ }
+ printf("\n");
+}
+
+int main() {
+ int arr1[] = {0,0,1,1,1,2,2,3,3,4};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]));
+
+ int arr2[] = {1, 1, 2};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]));
+
+ return 0;
+}
diff --git a/leetcode/cpp/0027.remove-element/CMakeLists.txt b/leetcode/cpp/0027.remove-element/CMakeLists.txt
new file mode 100644
index 00000000..1d5e168c
--- /dev/null
+++ b/leetcode/cpp/0027.remove-element/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0027-remove-element
+ main.c
+ )
diff --git a/leetcode/cpp/0027.remove-element/main.c b/leetcode/cpp/0027.remove-element/main.c
new file mode 100644
index 00000000..78881098
--- /dev/null
+++ b/leetcode/cpp/0027.remove-element/main.c
@@ -0,0 +1,39 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+int removeElement(int* nums, int numsSize, int val) {
+ for (int i = 0; i < numsSize; /* empty */) {
+ if (nums[i] == val) {
+ // Implements swap_remove()
+ nums[i] = nums[numsSize - 1];
+ numsSize -= 1;
+ } else {
+ i += 1;
+ }
+ }
+ return numsSize;
+}
+
+void checkSolution(int* nums, int numsSize, int val, int expected_k) {
+ int k = removeElement(nums, numsSize, val);
+ for (int i = 0; i < k; ++i) {
+ printf("%d, ", nums[i]);
+ }
+ printf("\n");
+ assert(k == expected_k);
+}
+
+int main() {
+ int arr1[] = {0, 1, 2, 2, 3, 0, 4, 2};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 2, 5);
+
+ int arr2[] = {3, 2, 2, 3};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 3, 2);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0031.next-permutation/CMakeLists.txt b/leetcode/cpp/0031.next-permutation/CMakeLists.txt
new file mode 100644
index 00000000..df8bb6bc
--- /dev/null
+++ b/leetcode/cpp/0031.next-permutation/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0031-next-permutation
+ main.c
+ )
diff --git a/leetcode/cpp/0031.next-permutation/main.c b/leetcode/cpp/0031.next-permutation/main.c
new file mode 100644
index 00000000..e69de29b
diff --git a/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/CMakeLists.txt b/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/CMakeLists.txt
new file mode 100644
index 00000000..19cdd24c
--- /dev/null
+++ b/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0034-find-first-and-last-position-of-element-in-sorted-array
+ main.cpp
+ )
diff --git a/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/main.cpp b/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/main.cpp
new file mode 100644
index 00000000..9362b060
--- /dev/null
+++ b/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/main.cpp
@@ -0,0 +1,60 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+
+#include
+
+class Solution {
+ public:
+ static
+ std::vector searchRange(std::vector& nums, int target) {
+ std::vector range;
+ range.push_back(-1);
+ range.push_back(-1);
+ if (nums.empty()) {
+ return range;
+ }
+
+ int left = 0;
+ int right = nums.size() - 1;
+
+ while (left <= right) {
+ printf("left: %d, right: %d\n", left, right);
+ int middle = left + (right - left) / 2;
+ if (nums[middle] == target) {
+ for (int i = middle; i >= 0 && nums[i] == target; --i) {
+ range[0] = i;
+ }
+ for (int i = middle; i < nums.size() && nums[i] == target; ++i) {
+ range[1] = i;
+ }
+ break;
+ } else if (nums[middle] < target) {
+ left = middle + 1;
+ } else {
+ right = middle - 1;
+ }
+ }
+
+ return range;
+ }
+};
+
+void checkSolution(int* nums, int numsSize, int target, int left, int right) {
+ std::vector vec{nums, nums + numsSize};
+ std::vector range = Solution::searchRange(vec, target);
+ assert(range[0] == left);
+ assert(range[1] == right);
+}
+
+int main() {
+ int arr1[] = {5,7,7,8,8,10};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 8, 3, 4);
+
+ int arr2[] = {1};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 1, 0, 0);
+ return 0;
+}
diff --git a/leetcode/cpp/0035.search-insert-position/CMakeLists.txt b/leetcode/cpp/0035.search-insert-position/CMakeLists.txt
new file mode 100644
index 00000000..b927e51b
--- /dev/null
+++ b/leetcode/cpp/0035.search-insert-position/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0035-search-insert-position
+ main.c
+ )
diff --git a/leetcode/cpp/0035.search-insert-position/main.c b/leetcode/cpp/0035.search-insert-position/main.c
new file mode 100644
index 00000000..b6b32048
--- /dev/null
+++ b/leetcode/cpp/0035.search-insert-position/main.c
@@ -0,0 +1,48 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+// Binary search
+int searchInsert(int* nums, int numsSize, int target) {
+ if (nums[numsSize - 1] < target) {
+ return numSize;
+ }
+
+ int left = 0;
+ int right = numsSize - 1;
+ int middle = 0;
+ while (left < right) {
+ middle = left + (right - left) / 2;
+ if (nums[middle] == target) {
+ return middle;
+ }
+ if (nums[middle] > target) {
+ right = middle - 1;
+ } else {
+ left = middle + 1;
+ }
+ }
+ return right;
+}
+
+void checkSolution(int* nums, int numsSize, int target, int expected) {
+ int out = searchInsert(nums, numsSize, target);
+ printf("out: %d, expected: %d\n", out, expected);
+ assert(out == expected);
+}
+int main() {
+ int arr1[] = {1, 3, 5, 6};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 5, 2);
+
+ int arr2[] = {1, 3, 5, 6};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 2, 1);
+
+ int arr3[] = {1, 3, 5, 6};
+ checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 7, 4);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0053.maximum-subarray/CMakeLists.txt b/leetcode/cpp/0053.maximum-subarray/CMakeLists.txt
new file mode 100644
index 00000000..cce5f150
--- /dev/null
+++ b/leetcode/cpp/0053.maximum-subarray/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0053-maximum-subarray
+ main.c
+ )
diff --git a/leetcode/cpp/0053.maximum-subarray/main.c b/leetcode/cpp/0053.maximum-subarray/main.c
new file mode 100644
index 00000000..e69de29b
diff --git a/leetcode/cpp/0061.rotate-list/CMakeLists.txt b/leetcode/cpp/0061.rotate-list/CMakeLists.txt
new file mode 100644
index 00000000..82eb9c2c
--- /dev/null
+++ b/leetcode/cpp/0061.rotate-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0061-rotate-list
+ main.c
+ )
diff --git a/leetcode/cpp/0061.rotate-list/main.c b/leetcode/cpp/0061.rotate-list/main.c
new file mode 100644
index 00000000..e545fc8f
--- /dev/null
+++ b/leetcode/cpp/0061.rotate-list/main.c
@@ -0,0 +1,138 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+static struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+static struct ListNode* listFromArray(int arr[], int len) {
+ assert(len >= 0);
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+static void listFree(struct ListNode* list) {
+ assert(list != NULL);
+ while (list != NULL) {
+ struct ListNode* tmp = list;
+ list = list->next;
+ free(tmp);
+ }
+}
+
+static int listSize(struct ListNode* list) {
+ assert(list != NULL);
+ int len = 0;
+ while (list != NULL) {
+ len += 1;
+ list = list->next;
+ }
+ return len;
+}
+
+struct ListNode* rotateRight(struct ListNode* head, int k) {
+ if (head == NULL) {
+ return NULL;
+ }
+
+ assert(k >= 0);
+
+ //const int len = listSize(head);
+ int len = 1;
+ struct ListNode* tail = head;
+ while (tail != NULL && tail->next != NULL) {
+ tail = tail->next;
+ len += 1;
+ }
+
+ k = len - (k % len);
+ if (k == 0) {
+ return head;
+ }
+
+ // Connect tail to head.
+ tail->next = head;
+
+ // Count node from head to tail.
+ while (k > 0) {
+ head = head->next;
+ tail = tail->next;
+ k -= 1;
+ }
+ // Break the ring at k point.
+ tail->next = NULL;
+
+ return head;
+}
+
+struct ListNode* rotateRight2(struct ListNode* head, int k) {
+ if (head == NULL) {
+ return NULL;
+ }
+ assert(k >= 0);
+
+ struct ListNode* fast = head;
+ struct ListNode* slow = head;
+ // FIXME(Shaohua): k might > len
+ while (k > 0) {
+ fast = fast->next;
+ k -= 1;
+ }
+
+ while (fast != NULL && fast->next != NULL) {
+ fast = fast->next;
+ slow = slow->next;
+ }
+ // Connect tail to head node.
+ fast->next = head;
+ // Now slow is the new tail node.
+ head = slow->next;
+ slow->next = NULL;
+
+ return head;
+}
+
+static void checkResult(int arr[], int len, int k) {
+ struct ListNode* head = listFromArray(arr, len);
+ struct ListNode* new_head = rotateRight(head, k);
+ //struct ListNode* new_head = rotateRight2(head, k);
+ head = new_head;
+ while (head != NULL) {
+ printf("%d, ", head->val);
+ head = head->next;
+ }
+ printf("\n");
+ free(new_head);
+}
+
+int main() {
+ int arr1[] = {1,2,3,4,5};
+ checkResult(arr1, sizeof(arr1) / sizeof(arr1[0]), 2);
+
+ int arr2[] = {1,2};
+ checkResult(arr2, sizeof(arr2) / sizeof(arr2[0]), 0);
+
+ int arr3[] = {1};
+ checkResult(arr3, sizeof(arr3) / sizeof(arr3[0]), 10);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0061.rotate-list/time.md b/leetcode/cpp/0061.rotate-list/time.md
new file mode 100644
index 00000000..689fdcde
--- /dev/null
+++ b/leetcode/cpp/0061.rotate-list/time.md
@@ -0,0 +1,3 @@
+
+# Time
+- 00:32:30
diff --git a/leetcode/cpp/0062.unique-paths/CMakeLists.txt b/leetcode/cpp/0062.unique-paths/CMakeLists.txt
new file mode 100644
index 00000000..f1b713d6
--- /dev/null
+++ b/leetcode/cpp/0062.unique-paths/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0062-unique-paths
+ main.c
+ )
diff --git a/leetcode/cpp/0066.plus-one/CMakeLists.txt b/leetcode/cpp/0066.plus-one/CMakeLists.txt
new file mode 100644
index 00000000..2bf7ec53
--- /dev/null
+++ b/leetcode/cpp/0066.plus-one/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0066-plus-one
+ main.c
+ )
diff --git a/leetcode/cpp/0066.plus-one/main.c b/leetcode/cpp/0066.plus-one/main.c
new file mode 100644
index 00000000..f05b9bf0
--- /dev/null
+++ b/leetcode/cpp/0066.plus-one/main.c
@@ -0,0 +1,56 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+int* plusOne(int* digits, int digitsSize, int* returnSize) {
+ int carry = 1;
+ int new_val = 0;
+ for (int i = digitsSize - 1; i >= 0; --i) {
+ new_val = carry + digits[i];
+ digits[i] = new_val % 10;
+ carry = new_val / 10;
+ }
+
+ int* out_digits;
+ if (carry != 0) {
+ *returnSize = digitsSize + 1;
+ out_digits = (int*)malloc(*returnSize * sizeof(int));
+ assert(out_digits);
+ memcpy(out_digits + 1, digits, digitsSize * sizeof(int));
+ out_digits[0] = carry;
+ } else {
+ *returnSize = digitsSize;
+ out_digits = (int*)malloc(*returnSize * sizeof(int));
+ assert(out_digits);
+ memcpy(out_digits, digits, digitsSize * sizeof(int));
+ }
+ return out_digits;
+}
+
+void checkSolution(int* digits, int digitsSize, int expectedReturnSize) {
+ int returnSize;
+ int* out_digits = plusOne(digits, digitsSize, &returnSize);
+ assert(returnSize == expectedReturnSize);
+ for (int i = 0; i < returnSize; ++i) {
+ printf("%d, ", out_digits[i]);
+ }
+ printf("\n");
+}
+
+int main() {
+ int arr1[] = {1, 2, 3};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 3);
+
+ int arr2[] = {4, 3, 2, 1};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 4);
+
+ int arr3[] = {9};
+ checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 2);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0070.climbing-stairs/CMakeLists.txt b/leetcode/cpp/0070.climbing-stairs/CMakeLists.txt
new file mode 100644
index 00000000..8d9aa2e4
--- /dev/null
+++ b/leetcode/cpp/0070.climbing-stairs/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0070-climbing-stairs
+ main.c
+ )
diff --git a/leetcode/cpp/0070.climbing-stairs/main.c b/leetcode/cpp/0070.climbing-stairs/main.c
new file mode 100644
index 00000000..5c967b80
--- /dev/null
+++ b/leetcode/cpp/0070.climbing-stairs/main.c
@@ -0,0 +1,41 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+int climbStairs(int n) {
+ if (n == 0 || n == 1) {
+ return 1;
+ }
+ return climbStairs(n - 1) + climbStairs(n - 2);
+}
+
+int climbStairs2(int n) {
+ if (n == 0 || n == 1) {
+ return 1;
+ }
+ int f1 = 1;
+ int f2 = 1;
+ int f3 = f1 + f2;
+ for (int i = 2; i <= n; ++i) {
+ f3 = f1 + f2;
+ f1 = f2;
+ f2 = f3;
+ }
+ return f3;
+}
+
+void checkSolution(int n, int expected) {
+ int out = climbStairs2(n);
+ printf("out: %d, exp: %d\n", out, expected);
+ assert(out == expected);
+}
+
+int main() {
+ checkSolution(2, 2);
+ checkSolution(3, 3);
+ return 0;
+}
diff --git a/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/CMakeLists.txt b/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/CMakeLists.txt
new file mode 100644
index 00000000..69000923
--- /dev/null
+++ b/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0082-remove-duplicates-from-sorted-list-ii
+ main.c
+ )
diff --git a/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/main.c b/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/main.c
new file mode 100644
index 00000000..557cd1e3
--- /dev/null
+++ b/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/main.c
@@ -0,0 +1,103 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+static struct ListNode* listNew(int val) {
+ struct ListNode* list = malloc(sizeof(struct ListNode));
+ assert(list != NULL);
+ list->val = val;
+ list->next = NULL;
+ return list;
+}
+
+static struct ListNode* listFromArray(int arr[], int len) {
+ assert(len >= 0);
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+static void listFree(struct ListNode* list) {
+ while (list != NULL) {
+ struct ListNode* tmp = list;
+ list = list->next;
+ free(tmp);
+ }
+}
+
+static struct ListNode* deleteDuplicates(struct ListNode* head) {
+ if (head == NULL) {
+ return NULL;
+ }
+
+ struct ListNode* current = head;
+ struct ListNode* prev = NULL;
+ struct ListNode* duplicated_node = NULL;
+
+ while (current != NULL) {
+ if (current->next != NULL && current->next->val == current->val) {
+ // Remove current node.
+ if (head == current) {
+ head = current->next;
+ }
+ if (prev != NULL) {
+ prev->next = current->next->next;
+ }
+ duplicated_node = current->next;
+ free(current);
+ current = duplicated_node;
+ // Keep prev position unchanged.
+ } else if (duplicated_node != NULL) {
+ // Remove last duplicated node.
+ assert(current == duplicated_node);
+ if (head == current) {
+ head = current->next;
+ }
+ current = current->next;
+ free(duplicated_node);
+ duplicated_node = NULL;
+ } else {
+ // Go to next node.
+ prev = current;
+ current = current->next;
+ }
+ }
+
+ return head;
+}
+
+static void checkSolution(int arr[], int len) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* list_uniq = deleteDuplicates(list);
+ while (list_uniq != NULL) {
+ printf("%d, ", list_uniq->val);
+ list_uniq = list_uniq->next;
+ }
+ printf("\n");
+ listFree(list_uniq);
+}
+
+int main() {
+ // {1, 2, 5}
+ int arr1[] = {1, 2, 3, 3, 4, 4, 5};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]));
+
+ // {2, 3}
+ int arr2[] = {1, 1, 1, 2, 3};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]));
+
+ return 0;
+}
diff --git a/leetcode/cpp/0083.remove-duplicates-from-sorted-list/CMakeLists.txt b/leetcode/cpp/0083.remove-duplicates-from-sorted-list/CMakeLists.txt
new file mode 100644
index 00000000..3c6697e0
--- /dev/null
+++ b/leetcode/cpp/0083.remove-duplicates-from-sorted-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0083-remove-duplicates-from-sorted-list
+ main.c
+ )
diff --git a/leetcode/cpp/0083.remove-duplicates-from-sorted-list/main.c b/leetcode/cpp/0083.remove-duplicates-from-sorted-list/main.c
new file mode 100644
index 00000000..2107dc44
--- /dev/null
+++ b/leetcode/cpp/0083.remove-duplicates-from-sorted-list/main.c
@@ -0,0 +1,83 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+static struct ListNode* listNew(int val) {
+ struct ListNode* list = malloc(sizeof(struct ListNode));
+ assert(list != NULL);
+ list->val = val;
+ list->next = NULL;
+ return list;
+}
+
+static void listFree(struct ListNode* list) {
+ while (list != NULL) {
+ struct ListNode* tmp = list;
+ list = list->next;
+ free(tmp);
+ }
+}
+
+static struct ListNode* listFromArray(int arr[], int len) {
+ assert(len >= 0);
+ struct ListNode* list;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+static struct ListNode* deleteDuplicates(struct ListNode* head) {
+ if (head == NULL) {
+ return NULL;
+ }
+
+ struct ListNode* current = head;
+ struct ListNode* previous = NULL;
+ while (current != NULL) {
+ if (previous != NULL && previous->val == current->val) {
+ // Remove current node.
+ previous->next = current->next;
+ struct ListNode* tmp = current;
+ current = current->next;
+ free(tmp);
+ } else {
+ previous = current;
+ current = current->next;
+ }
+ }
+
+ return head;
+}
+
+static void checkSolution(int arr[], int len) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* list_uniq = deleteDuplicates(list);
+ while (list_uniq != NULL) {
+ printf("%d, ", list_uniq->val);
+ list_uniq = list_uniq->next;
+ }
+ printf("\n");
+
+ listFree(list_uniq);
+}
+
+int main() {
+ int arr1[] = {1, 1, 2};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]));
+
+ int arr2[] = {1, 1, 2, 3, 3};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]));
+ return 0;
+}
diff --git a/leetcode/cpp/0086.partition-list/CMakeLists.txt b/leetcode/cpp/0086.partition-list/CMakeLists.txt
new file mode 100644
index 00000000..93409609
--- /dev/null
+++ b/leetcode/cpp/0086.partition-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0086-partition-list
+ main.c
+ )
diff --git a/leetcode/cpp/0086.partition-list/main.c b/leetcode/cpp/0086.partition-list/main.c
new file mode 100644
index 00000000..1f959232
--- /dev/null
+++ b/leetcode/cpp/0086.partition-list/main.c
@@ -0,0 +1,110 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+static struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+static struct ListNode* listFromArray(int arr[], int len) {
+ assert(len >= 0);
+ struct ListNode* head = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = head;
+ head = node;
+ }
+
+ return head;
+}
+
+static void listFree(struct ListNode* head) {
+ while (head != NULL) {
+ struct ListNode* tmp = head;
+ head = head->next;
+ free(tmp);
+ }
+}
+
+static struct ListNode* partition(struct ListNode* head, int x) {
+ if (head == NULL) {
+ return NULL;
+ }
+
+ struct ListNode* small_head = NULL;
+ struct ListNode* small_tail = NULL;
+ struct ListNode* large_head = NULL;
+ struct ListNode* large_tail = NULL;
+ struct ListNode* tmp = NULL;
+ while (head != NULL) {
+ tmp = head;
+ if (tmp->val < x) {
+ if (small_tail == NULL) {
+ small_head = tmp;
+ small_tail = tmp;
+ } else {
+ small_tail->next = tmp;
+ small_tail = tmp;
+ }
+ } else {
+ if (large_tail == NULL) {
+ large_head = tmp;
+ large_tail = tmp;
+ } else {
+ large_tail->next = tmp;
+ large_tail = tmp;
+ }
+ }
+
+ // Go to next node.
+ head = head->next;
+ }
+
+ if (small_tail == NULL) {
+ return large_head;
+ }
+ // Connect two parts.
+ small_tail->next = large_head;
+ if (large_tail != NULL) {
+ large_tail->next = NULL;
+ }
+
+ return small_head;
+}
+
+static void checkSolution(int arr[], int len, int x) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* list2 = partition(list, x);
+ list = list2;
+
+ while (list2 != NULL) {
+ printf("%d, ", list2->val);
+ list2 = list2->next;
+ }
+ printf("\n");
+
+ listFree(list);
+}
+
+int main() {
+ int arr1[] = {1,4,3,2,5,2};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 3);
+
+ int arr2[] = {2, 1};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 2);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0088.merge-sorted-array/CMakeLists.txt b/leetcode/cpp/0088.merge-sorted-array/CMakeLists.txt
new file mode 100644
index 00000000..1c986a9d
--- /dev/null
+++ b/leetcode/cpp/0088.merge-sorted-array/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0088-merge-sorted-array
+ main.c
+ )
diff --git a/leetcode/cpp/0088.merge-sorted-array/main.c b/leetcode/cpp/0088.merge-sorted-array/main.c
new file mode 100644
index 00000000..182897b0
--- /dev/null
+++ b/leetcode/cpp/0088.merge-sorted-array/main.c
@@ -0,0 +1,38 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {
+ // Merge from end of nums1
+ int i1 = m - 1;
+ int i2 = n - 1;
+ int index = m + n - 1;
+ while (i1 >= 0 && i2 >= 0) {
+ if (nums1[i1] > nums2[i2]) {
+ nums1[index] = nums1[i1];
+ i1 -= 1;
+ } else {
+ nums1[index] = nums2[i2];
+ i2 -= 1;
+ }
+ index -= 1;
+ }
+ while (i1 >= 0) {
+ nums1[index] = nums1[i1];
+ index -= 1;
+ i1 -= 1;
+ }
+ while (i2 >= 0) {
+ nums1[index] = nums2[i2];
+ index -= 1;
+ i2 -= 1;
+ }
+}
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0092.reverse-linked-list-ii/CMakeLists.txt b/leetcode/cpp/0092.reverse-linked-list-ii/CMakeLists.txt
new file mode 100644
index 00000000..4d5f7f6d
--- /dev/null
+++ b/leetcode/cpp/0092.reverse-linked-list-ii/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0092-reverse-linked-list-ii
+ main.c
+ )
diff --git a/leetcode/cpp/0092.reverse-linked-list-ii/main.c b/leetcode/cpp/0092.reverse-linked-list-ii/main.c
new file mode 100644
index 00000000..dcfc9998
--- /dev/null
+++ b/leetcode/cpp/0092.reverse-linked-list-ii/main.c
@@ -0,0 +1,110 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+static struct ListNode* listNew(int val) {
+ struct ListNode* list = malloc(sizeof(struct ListNode));
+ assert(list != NULL);
+ list->val = val;
+ list->next = NULL;
+ return list;
+}
+
+static struct ListNode* listFromArray(int arr[], int len) {
+ assert(len >= 0);
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+static void listFree(struct ListNode* list) {
+ assert(list != NULL);
+ while (list != NULL) {
+ struct ListNode* node = list;
+ list = list->next;
+ free(node);
+ }
+}
+
+static struct ListNode* reverseBetween(struct ListNode* head, int left, int right) {
+ assert(head != NULL);
+ assert(left >= 1 && left <= right);
+ if (left == right) {
+ return head;
+ }
+ left -= 1;
+ right -= 1;
+
+ struct ListNode* range_prev = NULL;
+ struct ListNode* range_start = NULL;
+ struct ListNode* range_end = NULL;
+ struct ListNode* curr = head;
+ struct ListNode* prev = NULL;
+ struct ListNode* next = NULL;
+
+ int index = 0;
+ while (index < left) {
+ range_prev = curr;
+ curr = curr->next;
+ index += 1;
+ }
+
+ range_start = curr;
+
+ while (index < right) {
+ // switch direction
+ next = curr->next;
+ curr->next = prev;
+ prev = curr;
+ curr = next;
+ index += 1;
+ }
+ range_end = curr;
+ if (range_prev == NULL) {
+ head = range_end;
+ } else {
+ range_prev->next = range_end;
+ }
+ range_start->next = range_end->next;
+ range_end->next = prev;
+
+ return head;
+}
+
+static void checkSolution(int arr[], int len, int left, int right) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* reversed = reverseBetween(list, left, right);
+ list = reversed;
+ while (reversed != NULL) {
+ printf("%d, ", reversed->val);
+ reversed = reversed->next;
+ }
+ printf("\n");
+ listFree(list);
+}
+
+int main() {
+ int arr1[] = {1, 2, 3, 4, 5};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 2, 4);
+
+ int arr2[] = {5};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 1, 1);
+
+ int arr3[] = {3, 5};
+ checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 1, 2);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0093.restore-ip-addresses/CMakeLists.txt b/leetcode/cpp/0093.restore-ip-addresses/CMakeLists.txt
new file mode 100644
index 00000000..f240a137
--- /dev/null
+++ b/leetcode/cpp/0093.restore-ip-addresses/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0093-restore-ip-addresses
+ main.cpp
+ )
diff --git a/leetcode/cpp/0093.restore-ip-addresses/main.cpp b/leetcode/cpp/0093.restore-ip-addresses/main.cpp
new file mode 100644
index 00000000..fa5a648b
--- /dev/null
+++ b/leetcode/cpp/0093.restore-ip-addresses/main.cpp
@@ -0,0 +1,78 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+
+#include
+#include
+#include
+
+class Solution {
+ public:
+ static bool isValidIpSector(const std::string& s) {
+ if (s.empty()) {
+ return false;
+ }
+ if (s.size() > 3) {
+ return false;
+ }
+ // Leading zero
+ if (s.size() > 1 && s[0] == '0') {
+ return false;
+ }
+ int val = std::stoi(s);
+ return val >=0 && val <= 255;
+ }
+
+ static
+ std::vector restoreIpAddresses(std::string s) {
+ std::vector out;
+ for (int i = 1; i <= 3 && i < s.size(); ++i) {
+ const std::string& first = s.substr(0, i);
+ if (!isValidIpSector(first)) {
+ continue;
+ }
+ for (int j = 1; j <= 3 && i + j < s.size(); ++j) {
+ const std::string& second = s.substr(i, j);
+ if (!isValidIpSector(second)) {
+ continue;
+ }
+
+ for (int k = 1; k <= 3 && i + j + k < s.size(); ++k) {
+ const std::string& third = s.substr(i + j, k);
+ const std::string& forth = s.substr(i + j + k);
+ if (isValidIpSector(third) && isValidIpSector(forth)) {
+ out.emplace_back(first + "." + second + "." + third + "." + forth);
+ }
+ }
+ }
+ }
+ return out;
+ }
+};
+
+void checkSolution(std::string s, std::vector exp_out) {
+ std::vector out = Solution::restoreIpAddresses(s);
+ printf("size: %d, exp: %d\n", out.size(), exp_out.size());
+ assert(out.size() == exp_out.size());
+ std::sort(out.begin(), out.end());
+ std::sort(exp_out.begin(), exp_out.end());
+ for (int i = 0; i < out.size(); ++i) {
+ assert(out[i] == exp_out[i]);
+ }
+}
+
+int main() {
+ checkSolution("25525511135", {"255.255.11.135", "255.255.111.35"});
+ checkSolution("101023", {
+ "1.0.10.23",
+ "1.0.102.3",
+ "10.1.0.23",
+ "10.10.2.3",
+ "101.0.2.3"
+ });
+
+ return 0;
+}
diff --git a/leetcode/cpp/0098.validate-binary-search-tree/CMakeLists.txt b/leetcode/cpp/0098.validate-binary-search-tree/CMakeLists.txt
new file mode 100644
index 00000000..d1c6de36
--- /dev/null
+++ b/leetcode/cpp/0098.validate-binary-search-tree/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0098-validate-binary-search-tree
+ main.c
+ )
diff --git a/leetcode/cpp/0098.validate-binary-search-tree/main.c b/leetcode/cpp/0098.validate-binary-search-tree/main.c
new file mode 100644
index 00000000..7253fb2d
--- /dev/null
+++ b/leetcode/cpp/0098.validate-binary-search-tree/main.c
@@ -0,0 +1,82 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+struct TreeNode* findMin(struct TreeNode* root) {
+ if (root == NULL) {
+ return NULL;
+ }
+ // traverse
+ struct TreeNode* left = findMin(root->left);
+ struct TreeNode* right = findMin(root->left);
+ struct TreeNode* min_node = root;
+ if (left != NULL && left->val < min_node->val) {
+ min_node = left;
+ }
+ if (right != NULL && right->val < min_node->val) {
+ min_node = right;
+ }
+ return min_node;
+}
+
+struct TreeNode* findMax(struct TreeNode* root) {
+ if (root == NULL) {
+ return NULL;
+ }
+ struct TreeNode* left = findMax(root->left);
+ struct TreeNode* right = findMax(root->left);
+ struct TreeNode* max_node = root;
+ if (left != NULL && left->val > max_node->val) {
+ max_node = left;
+ }
+ if (right != NULL && right->val > max_node->val) {
+ max_node = right;
+ }
+ return max_node;
+}
+
+bool isValidBST(struct TreeNode* root) {
+ if (root == NULL) {
+ return true;
+ }
+ struct TreeNode* left_max = findMax(root->left);
+ if (left_max != NULL && left_max->val >= root->val) {
+ return false;
+ }
+ struct TreeNode* right_min = findMin(root->right);
+ if (right_min != NULL && right_min->val <= root->val) {
+ return false;
+ }
+ return true;
+}
+
+bool isValidBST2Helper(struct TreeNode* root, long min, long max) {
+ if (root == NULL) {
+ return true;
+ }
+ if (root->val < max && root->val > min) {
+ return isValidBST2Helper(root->left, min, root->val) &&
+ isValidBST2Helper(root->right, root->val, max);
+ }
+ return false;
+}
+
+bool isValidBST2(struct TreeNode* root) {
+ return isValidBST2Helper(root, LONG_MIN, LONG_MAX);
+}
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0101.symmetric-tree/CMakeLists.txt b/leetcode/cpp/0101.symmetric-tree/CMakeLists.txt
new file mode 100644
index 00000000..de99cb81
--- /dev/null
+++ b/leetcode/cpp/0101.symmetric-tree/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0101-symmetric-tree
+ main.c
+ )
diff --git a/leetcode/cpp/0101.symmetric-tree/main.c b/leetcode/cpp/0101.symmetric-tree/main.c
new file mode 100644
index 00000000..6aea52c1
--- /dev/null
+++ b/leetcode/cpp/0101.symmetric-tree/main.c
@@ -0,0 +1,36 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+bool isSymmetricHelper(struct TreeNode* left, struct TreeNode* right) {
+ if (left == NULL && right == NULL) {
+ return true;
+ }
+ if (left == NULL || right == NULL) {
+ return false;
+ }
+ if (left->val != right->val) {
+ return false;
+ }
+ return isSymmetricHelper(left->left, right->right) &&
+ isSymmetricHelper(left->right, right->left);
+}
+
+bool isSymmetric(struct TreeNode* root) {
+ return isSymmetricHelper(root->left, root->right);
+}
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0102.binary-tree-level-order-traversal/CMakeLists.txt b/leetcode/cpp/0102.binary-tree-level-order-traversal/CMakeLists.txt
new file mode 100644
index 00000000..e7ae4972
--- /dev/null
+++ b/leetcode/cpp/0102.binary-tree-level-order-traversal/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0102-binary-tree-level-order-traversal
+ main.cpp
+ )
diff --git a/leetcode/cpp/0102.binary-tree-level-order-traversal/main.cpp b/leetcode/cpp/0102.binary-tree-level-order-traversal/main.cpp
new file mode 100644
index 00000000..95dc62e0
--- /dev/null
+++ b/leetcode/cpp/0102.binary-tree-level-order-traversal/main.cpp
@@ -0,0 +1,86 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+
+#include
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+class Solution {
+ public:
+ void levelOrderHelper(std::queue queue, std::vector>& out) {
+ std::queue next_queue;
+ std::vector values;
+
+ while (!queue.empty()) {
+ TreeNode* node = queue.front();
+ queue.pop();
+ values.push_back(node->val);
+ if (node->left != nullptr) {
+ next_queue.push(node->left);
+ }
+ if (node->right != nullptr) {
+ next_queue.push(node->right);
+ }
+ }
+ out.push_back(values);
+
+ if (!next_queue.empty()) {
+ levelOrderHelper(next_queue, out);
+ }
+ }
+
+ std::vector> levelOrder(TreeNode* root) {
+ std::vector> out;
+ if (root == nullptr) {
+ return out;
+ }
+
+ std::queue queue;
+ queue.push(root);
+ levelOrderHelper(queue, out);
+
+ return out;
+ }
+
+ std::vector> levelOrderIterative(TreeNode* root) {
+ std::vector> out;
+ if (root == nullptr) {
+ return out;
+ }
+
+ std::queue queue;
+ queue.push(root);
+
+ while (!queue.empty()) {
+ size_t size = queue.size();
+ std::vector values;
+ // current level.
+ for (size_t i = 0; i < size; ++i) {
+ TreeNode* node = queue.front();
+ queue.pop();
+ values.push_back(node->val);
+ if (node->left != nullptr) {
+ queue.push(node->left);
+ }
+ if (node->right != nullptr) {
+ queue.push(node->right);
+ }
+ }
+ out.push_back(values);
+ }
+
+ return out;
+ }
+};
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/CMakeLists.txt b/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/CMakeLists.txt
new file mode 100644
index 00000000..664fb798
--- /dev/null
+++ b/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0103-binary-tree-zigzag-level-order-traversal
+ main.cpp
+ )
diff --git a/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/main.cpp b/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/main.cpp
new file mode 100644
index 00000000..b3cd6070
--- /dev/null
+++ b/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/main.cpp
@@ -0,0 +1,56 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+class Solution {
+ public:
+ std::vector> zigzagLevelOrder(TreeNode* root) {
+ std::vector> out;
+ if (root == nullptr) {
+ return out;
+ }
+
+ std::queue queue;
+ queue.push(root);
+ bool do_reverse = false;
+
+ while (!queue.empty()) {
+ size_t size = queue.size();
+ std::vector values;
+ // current level.
+ for (size_t i = 0; i < size; ++i) {
+ TreeNode* node = queue.front();
+ queue.pop();
+ values.push_back(node->val);
+ if (node->left != nullptr) {
+ queue.push(node->left);
+ }
+ if (node->right != nullptr) {
+ queue.push(node->right);
+ }
+ }
+
+ if (do_reverse) {
+ std::reverse(values.begin(), values.end());
+ }
+ out.push_back(values);
+ do_reverse = !do_reverse;
+ }
+
+ return out;
+ }
+};
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/CMakeLists.txt b/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/CMakeLists.txt
new file mode 100644
index 00000000..c96235ac
--- /dev/null
+++ b/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0107-binary-tree-level-order-traversal-ii
+ main.cpp
+ )
diff --git a/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/main.cpp b/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/main.cpp
new file mode 100644
index 00000000..e7e76cbe
--- /dev/null
+++ b/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/main.cpp
@@ -0,0 +1,51 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+class Solution {
+ public:
+ std::vector> levelOrderBottom(TreeNode* root) {
+ std::vector> out;
+ if (root == nullptr) {
+ return out;
+ }
+
+ std::queue queue;
+ queue.push(root);
+ while (!queue.empty()) {
+
+ // current level
+ size_t size = queue.size();
+ std::vector values;
+ for (size_t i = 0; i < size; ++i) {
+ TreeNode* node = queue.front();
+ queue.pop();
+ values.push_back(node->val);
+ if (node->left != nullptr) {
+ queue.push(node->left);
+ }
+ if (node->right != nullptr) {
+ queue.push(node->right);
+ }
+ }
+ out.push_back(values);
+ }
+
+ std::reverse(out.begin(), out.end());
+ return out;
+ }
+};
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/CMakeLists.txt b/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/CMakeLists.txt
new file mode 100644
index 00000000..1eedfb5c
--- /dev/null
+++ b/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0108-convert-sorted-array-to-binary-search-tree
+ main.c
+ )
diff --git a/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/main.c b/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/main.c
new file mode 100644
index 00000000..48f37e01
--- /dev/null
+++ b/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/main.c
@@ -0,0 +1,18 @@
+
+#include
+#include
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
+
+}
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/CMakeLists.txt b/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/CMakeLists.txt
new file mode 100644
index 00000000..239d5bed
--- /dev/null
+++ b/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0114-flatten-binary-tree-to-linked-list
+ main.c
+ )
diff --git a/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/main.c b/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/main.c
new file mode 100644
index 00000000..1a40e101
--- /dev/null
+++ b/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/main.c
@@ -0,0 +1,138 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+static struct TreeNode* treeNew(int val) {
+ struct TreeNode* node = malloc(sizeof(struct TreeNode));
+ assert(node != NULL);
+ node->val = val;
+ node->left = NULL;
+ node->right = NULL;
+ return node;
+}
+
+static struct TreeNode* treeFromArrayHelper(int arr[], int len, int index) {
+ if (index >= len) {
+ return NULL;
+ }
+
+ if (arr[index] == INT_MIN) {
+ return NULL;
+ }
+
+ struct TreeNode* node = treeNew(arr[index]);
+ node->left = treeFromArrayHelper(arr, len, index * 2 + 1);
+ node->right = treeFromArrayHelper(arr, len, index * 2 + 2);
+ return node;
+}
+
+static struct TreeNode* treeFromArray(int arr[], int len) {
+ assert(len >= 0);
+ struct TreeNode* root = treeFromArrayHelper(arr, len, 0);
+
+ return root;
+}
+
+static void treeFree(struct TreeNode* root) {
+ if (root != NULL) {
+ treeFree(root->left);
+ treeFree(root->right);
+ free(root);
+ }
+}
+
+static void treeTraversePreOrder(
+ struct TreeNode* node,
+ void (*apply)(struct TreeNode* node, void* user_data),
+ void* user_data) {
+ if (node != NULL) {
+ apply(node, user_data);
+ treeTraversePreOrder(node->left, apply, user_data);
+ treeTraversePreOrder(node->right, apply, user_data);
+ }
+}
+
+static void debugTreeNodeHelper(struct TreeNode* node) {
+ if (node != NULL) {
+ printf("%d, ", node->val);
+ debugTreeNodeHelper(node->left);
+ debugTreeNodeHelper(node->right);
+ } else {
+ printf("null, ");
+ }
+}
+
+static void debugTreeNode(struct TreeNode* root) {
+ assert(root != NULL);
+ debugTreeNodeHelper(root);
+ printf("\n");
+}
+
+static struct TreeNode* treeGetRightLeaf(struct TreeNode* root) {
+ if (root == NULL) {
+ return NULL;
+ }
+ while (root != NULL) {
+ if (root->right == NULL) {
+ return root;
+ }
+ root = root->right;
+ }
+ return NULL;
+}
+
+static void flatten(struct TreeNode* root) {
+ while (root != NULL) {
+ if (root->left != NULL) {
+ struct TreeNode* right_leaf = treeGetRightLeaf(root->left);
+ right_leaf->right = root->right;
+ root->right = root->left;
+ root->left = NULL;
+ }
+ root = root->right;
+ }
+}
+
+static void flatten2(struct TreeNode* root) {
+ while (root != NULL) {
+ if (root->left != NULL) {
+ struct TreeNode* right_leaf = root->left;
+ while (right_leaf->right != NULL) {
+ right_leaf = right_leaf->right;
+ }
+
+ right_leaf->right = root->right;
+ root->right = root->left;
+ root->left = NULL;
+ }
+ root = root->right;
+ }
+}
+
+static void checkSolution(int arr[], int len) {
+ struct TreeNode* root = treeFromArray(arr, len);
+ flatten(root);
+ //flatten2(root);
+ debugTreeNode(root);
+
+ treeFree(root);
+}
+
+int main() {
+ int arr[] = {1, 2, 5, 3, 4, INT_MIN, 6};
+ //int arr[] = {1, 2, INT_MIN, 3};
+ checkSolution(arr, sizeof(arr) / sizeof(arr[0]));
+
+ return 0;
+}
diff --git a/leetcode/cpp/0139.word-break/CMakeLists.txt b/leetcode/cpp/0139.word-break/CMakeLists.txt
new file mode 100644
index 00000000..307bf7e0
--- /dev/null
+++ b/leetcode/cpp/0139.word-break/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0139-word-break
+ main.cpp
+ )
diff --git a/leetcode/cpp/0139.word-break/main.cpp b/leetcode/cpp/0139.word-break/main.cpp
new file mode 100644
index 00000000..04ef8ca3
--- /dev/null
+++ b/leetcode/cpp/0139.word-break/main.cpp
@@ -0,0 +1,18 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+class Solution {
+ public:
+ static
+ bool wordBreak(std::string s, std::vector& wordDict) {
+ }
+};
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0141.linked-list-cycle/CMakeLists.txt b/leetcode/cpp/0141.linked-list-cycle/CMakeLists.txt
new file mode 100644
index 00000000..a69de6ce
--- /dev/null
+++ b/leetcode/cpp/0141.linked-list-cycle/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0141-linked-list-cycle
+ main.c
+ )
diff --git a/leetcode/cpp/0141.linked-list-cycle/main.c b/leetcode/cpp/0141.linked-list-cycle/main.c
new file mode 100644
index 00000000..995d4875
--- /dev/null
+++ b/leetcode/cpp/0141.linked-list-cycle/main.c
@@ -0,0 +1,119 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+struct ListNode* listConnectTailTo(struct ListNode* head, int pos) {
+ assert(head != NULL);
+ assert(pos > 0);
+
+ struct ListNode* curr = head;
+ struct ListNode* pos_node = NULL;
+ while (pos > 0) {
+ curr = curr->next;
+ pos -= 1;
+ }
+ assert(curr != NULL);
+ pos_node = curr;
+ while (curr->next != NULL) {
+ curr = curr->next;
+ }
+ curr->next = pos_node;
+
+ return head;
+}
+
+void listFree(struct ListNode* list) {
+ while (list != NULL) {
+ struct ListNode* tmp = list;
+ list = list->next;
+ free(tmp);
+ }
+}
+
+bool hasCycle(struct ListNode *head) {
+ struct ListNode* alert_node = listNew(INT_MIN);
+ struct ListNode* tmp = NULL;
+ while (head != NULL) {
+ if (head->next == alert_node) {
+ return true;
+ }
+ tmp = head;
+ head = head->next;
+ tmp->next = alert_node;
+ }
+ return false;
+}
+
+bool hasCycle2(struct ListNode *head) {
+ const int kResetVal = INT_MIN;
+ while (head != NULL) {
+ if (head->val == kResetVal) {
+ return true;
+ }
+ head->val = kResetVal;
+ head = head->next;
+ }
+ return false;
+}
+
+// Floyd’s Cycle-Finding Algorithm
+bool hasCycle3(struct ListNode *head) {
+ struct ListNode* fast = head;
+ struct ListNode* slow = head;
+
+ while (fast != NULL && fast->next != NULL) {
+ // 2 nodes forward
+ fast = fast->next->next;
+ // 1 node forward
+ slow = slow->next;
+
+ // Got the same node.
+ if (fast == slow) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void checkSolution(int arr[], int len, int pos) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* list2 = listConnectTailTo(list, pos);
+ bool ret = hasCycle(list2);
+ printf("has cycle: %d\n", ret);
+}
+
+int main() {
+ int arr[] = {3, 2, 0, -4};
+ checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 1);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0142.linked-list-cycle-ii/CMakeLists.txt b/leetcode/cpp/0142.linked-list-cycle-ii/CMakeLists.txt
new file mode 100644
index 00000000..02026e98
--- /dev/null
+++ b/leetcode/cpp/0142.linked-list-cycle-ii/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0142-linked-list-cycle-ii
+ main.c
+ )
diff --git a/leetcode/cpp/0142.linked-list-cycle-ii/main.c b/leetcode/cpp/0142.linked-list-cycle-ii/main.c
new file mode 100644
index 00000000..f65b8b55
--- /dev/null
+++ b/leetcode/cpp/0142.linked-list-cycle-ii/main.c
@@ -0,0 +1,102 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+struct ListNode* listConnectTailTo(struct ListNode* head, int pos) {
+ assert(head != NULL);
+ assert(pos > 0);
+
+ struct ListNode* curr = head;
+ struct ListNode* pos_node = NULL;
+ while (pos > 0) {
+ curr = curr->next;
+ pos -= 1;
+ }
+ assert(curr != NULL);
+ pos_node = curr;
+ while (curr->next != NULL) {
+ curr = curr->next;
+ }
+ curr->next = pos_node;
+
+ return head;
+}
+
+void listFree(struct ListNode* list) {
+ while (list != NULL) {
+ struct ListNode* tmp = list;
+ list = list->next;
+ free(tmp);
+ }
+}
+
+// Floyd’s Cycle-Finding Algorithm
+struct ListNode* detectCycle(struct ListNode* head) {
+ struct ListNode* fast = head;
+ struct ListNode* slow = head;
+ while (fast != NULL && fast->next != NULL) {
+ // 2 nodes forward
+ fast = fast->next->next;
+ // 1 node forward
+ slow = slow->next;
+
+ if (fast == slow) {
+ // Found cycle.
+
+ // Reset slow to head of list.
+ slow = head;
+ while (slow != fast) {
+ // Wait until they meat again.
+ slow = slow->next;
+ fast = fast->next;
+ }
+ return slow;
+ }
+ }
+ return NULL;
+}
+
+void checkSolution(int arr[], int len, int pos) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* list2 = listConnectTailTo(list, pos);
+ struct ListNode* cycle_list = detectCycle(list2);
+ while (cycle_list != NULL) {
+ printf("%d, ", cycle_list->val);
+ cycle_list = cycle_list->next;
+ }
+}
+
+int main() {
+ int arr[] = {3, 2, 0, -4};
+ checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 1);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0144.binary-tree-preorder-traversal/CMakeLists.txt b/leetcode/cpp/0144.binary-tree-preorder-traversal/CMakeLists.txt
new file mode 100644
index 00000000..b6338df7
--- /dev/null
+++ b/leetcode/cpp/0144.binary-tree-preorder-traversal/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0144-binary-tree-preorder-traversal
+ main.cpp
+ )
diff --git a/leetcode/cpp/0144.binary-tree-preorder-traversal/main.cpp b/leetcode/cpp/0144.binary-tree-preorder-traversal/main.cpp
new file mode 100644
index 00000000..d61f99ff
--- /dev/null
+++ b/leetcode/cpp/0144.binary-tree-preorder-traversal/main.cpp
@@ -0,0 +1,36 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+
+struct TreeNode {
+ int val;
+ struct TreeNode* left;
+ struct TreeNode* right;
+};
+
+class Solution {
+public:
+ static
+ void helper(TreeNode* root, std::vector& values) {
+ if (root == nullptr) {
+ return;
+ }
+ values.push_back(root->val);
+ helper(root->left, values);
+ helper(root->right, values);
+ }
+
+ std::vector preorderTraversal(TreeNode* root) {
+ std::vector values;
+ helper(root, values);
+ return values;
+ }
+};
+
+
+int main() {
+
+ return 0;
+}
diff --git a/leetcode/cpp/0160.intersection-of-two-linked-lists/CMakeLists.txt b/leetcode/cpp/0160.intersection-of-two-linked-lists/CMakeLists.txt
new file mode 100644
index 00000000..3c0ecea7
--- /dev/null
+++ b/leetcode/cpp/0160.intersection-of-two-linked-lists/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0160-intersection-of-two-linked-lists
+ main.c
+ )
diff --git a/leetcode/cpp/0160.intersection-of-two-linked-lists/main.c b/leetcode/cpp/0160.intersection-of-two-linked-lists/main.c
new file mode 100644
index 00000000..85c41c46
--- /dev/null
+++ b/leetcode/cpp/0160.intersection-of-two-linked-lists/main.c
@@ -0,0 +1,53 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listSkip(struct ListNode* head, int count) {
+ while (count > 0) {
+ head = head->next;
+ assert(head != NULL);
+ count -= 1;
+ }
+ return head;
+}
+
+int listLen(struct ListNode* head) {
+ int count = 0;
+ while (head != NULL) {
+ head = head->next;
+ count += 1;
+ }
+ return count;
+}
+
+struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
+ int len_a = listLen(headA);
+ int len_b = listLen(headB);
+ if (len_a > len_b) {
+ headA = listSkip(headA, len_a - len_b);
+ } else {
+ headB = listSkip(headB, len_b - len_a);
+ }
+
+ while (headA != NULL) {
+ if (headA == headB) {
+ return headA;
+ }
+ headA = headA->next;
+ headB = headB->next;
+ }
+ return NULL;
+}
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0162.find-peak-element/CMakeLists.txt b/leetcode/cpp/0162.find-peak-element/CMakeLists.txt
new file mode 100644
index 00000000..91626075
--- /dev/null
+++ b/leetcode/cpp/0162.find-peak-element/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0162-find-peak-element
+ main.c
+ )
diff --git a/leetcode/cpp/0191.number-of-1-bits/CMakeLists.txt b/leetcode/cpp/0191.number-of-1-bits/CMakeLists.txt
new file mode 100644
index 00000000..6635c88b
--- /dev/null
+++ b/leetcode/cpp/0191.number-of-1-bits/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0191-number-of-1-bits
+ main.c
+ )
diff --git a/leetcode/cpp/0191.number-of-1-bits/main.c b/leetcode/cpp/0191.number-of-1-bits/main.c
new file mode 100644
index 00000000..a0684486
--- /dev/null
+++ b/leetcode/cpp/0191.number-of-1-bits/main.c
@@ -0,0 +1,18 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+
+int hammingWeight(uint32_t n) {
+ int count = 0;
+ while (n != 0) {
+ count += n % 2;
+ n /= 2;
+ }
+ return count;
+}
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0199.binary-tree-right-side-view/CMakeLists.txt b/leetcode/cpp/0199.binary-tree-right-side-view/CMakeLists.txt
new file mode 100644
index 00000000..81c76663
--- /dev/null
+++ b/leetcode/cpp/0199.binary-tree-right-side-view/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0199-binary-tree-right-side-view
+ main.c
+ )
diff --git a/leetcode/cpp/0200.number-of-islands/CMakeLists.txt b/leetcode/cpp/0200.number-of-islands/CMakeLists.txt
new file mode 100644
index 00000000..ed4310b9
--- /dev/null
+++ b/leetcode/cpp/0200.number-of-islands/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0200-number-of-islands
+ main.c
+ )
diff --git a/leetcode/cpp/0203.remove-linked-list-elements/CMakeLists.txt b/leetcode/cpp/0203.remove-linked-list-elements/CMakeLists.txt
new file mode 100644
index 00000000..dd0bf066
--- /dev/null
+++ b/leetcode/cpp/0203.remove-linked-list-elements/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0203.remove-linked-list-elements
+ main.c
+ )
diff --git a/leetcode/cpp/0203.remove-linked-list-elements/main.c b/leetcode/cpp/0203.remove-linked-list-elements/main.c
new file mode 100644
index 00000000..801c5934
--- /dev/null
+++ b/leetcode/cpp/0203.remove-linked-list-elements/main.c
@@ -0,0 +1,113 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+void listFree(struct ListNode* list) {
+ while (list != NULL) {
+ struct ListNode* tmp = list;
+ list = list->next;
+ free(tmp);
+ }
+}
+
+struct ListNode* removeElements(struct ListNode* head, int val) {
+ if (head == NULL) {
+ return NULL;
+ }
+
+ struct ListNode* curr = head;
+ struct ListNode* prev = NULL;
+
+ while (curr != NULL) {
+ if (curr->val == val) {
+ // Remove current node.
+ if (prev == NULL) {
+ assert(curr == head);
+ curr = curr->next;
+ free(head);
+ head = curr;
+ } else {
+ prev->next = curr->next;
+ free(curr);
+ curr = prev->next;
+ }
+ } else {
+ prev = curr;
+ curr = curr->next;
+ }
+ }
+ return head;
+}
+
+struct ListNode* removeElements2(struct ListNode* head, int val) {
+ if (head == NULL) {
+ return NULL;
+ }
+
+ struct ListNode* curr = head->next;
+ struct ListNode* prev = head;
+
+ while (curr != NULL) {
+ if (curr->val == val) {
+ // Remove current node.
+ prev->next = curr->next;
+ free(curr);
+ curr = prev->next;
+ } else {
+ prev = curr;
+ curr = curr->next;
+ }
+ }
+ if (head->val == val) {
+ curr = head;
+ head = head->next;
+ free(curr);
+ }
+ return head;
+}
+
+void checkSolution(int arr[], int len, int val) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* list2 = removeElements2(list, val);
+ list = list2;
+ while (list != NULL) {
+ printf("%d, ", list->val);
+ list = list->next;
+ }
+ printf("\n");
+ listFree(list2);
+}
+
+int main() {
+ int arr[] = {1,2,6,3,4,5,6};
+ checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 6);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0206.reverse-linked-list/CMakeLists.txt b/leetcode/cpp/0206.reverse-linked-list/CMakeLists.txt
new file mode 100644
index 00000000..6affc6ab
--- /dev/null
+++ b/leetcode/cpp/0206.reverse-linked-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0206-reverse-linked-list
+ main.c
+ )
diff --git a/leetcode/cpp/0206.reverse-linked-list/main.c b/leetcode/cpp/0206.reverse-linked-list/main.c
new file mode 100644
index 00000000..52142915
--- /dev/null
+++ b/leetcode/cpp/0206.reverse-linked-list/main.c
@@ -0,0 +1,79 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+void listFree(struct ListNode* head) {
+ while (head != NULL) {
+ struct ListNode* tmp = head;
+ head = head->next;
+ free(tmp);
+ }
+}
+
+void listDebug(struct ListNode* head) {
+ while (head != NULL) {
+ printf("%d, ", head->val);
+ head = head->next;
+ }
+ printf("\n");
+}
+
+struct ListNode* reverseList(struct ListNode* head) {
+ if (head == NULL) {
+ return NULL;
+ }
+ struct ListNode* prev = NULL;
+ struct ListNode* next = NULL;
+ while (head != NULL) {
+ next = head->next;
+ head->next = prev;
+ prev = head;
+ head = next;
+ }
+ return prev;
+}
+
+void checkSolution(int arr[], int len) {
+ struct ListNode* list = listFromArray(arr, len);
+ listDebug(list);
+ list = reverseList(list);
+ listDebug(list);
+ listFree(list);
+}
+
+int main() {
+ int arr1[] = {1, 2, 3, 4, 5};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]));
+
+ int arr2[] = {1, 2};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]));
+
+ return 0;
+}
diff --git a/leetcode/cpp/0215.kth-largest-element-in-an-array/CMakeLists.txt b/leetcode/cpp/0215.kth-largest-element-in-an-array/CMakeLists.txt
new file mode 100644
index 00000000..9c0331b4
--- /dev/null
+++ b/leetcode/cpp/0215.kth-largest-element-in-an-array/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0215-kth-largest-element-in-an-array
+ main.c
+ )
diff --git a/leetcode/cpp/0234.palindrome-linked-list/CMakeLists.txt b/leetcode/cpp/0234.palindrome-linked-list/CMakeLists.txt
new file mode 100644
index 00000000..e71ef6a2
--- /dev/null
+++ b/leetcode/cpp/0234.palindrome-linked-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0234-palindrome-linked-list
+ main.c
+ )
diff --git a/leetcode/cpp/0234.palindrome-linked-list/main.c b/leetcode/cpp/0234.palindrome-linked-list/main.c
new file mode 100644
index 00000000..5de03065
--- /dev/null
+++ b/leetcode/cpp/0234.palindrome-linked-list/main.c
@@ -0,0 +1,102 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+void listFree(struct ListNode* head) {
+ while (head != NULL) {
+ struct ListNode* node = head;
+ head = head->next;
+ free(node);
+ }
+}
+
+void listDebug(struct ListNode* head) {
+ while (head != NULL) {
+ printf("%d, ", head->val);
+ head = head->next;
+ }
+ printf("\n");
+}
+
+bool isPalindrome(struct ListNode* head) {
+ if (head == NULL) {
+ return true;
+ }
+
+ // 1. Reverse left half of list.
+ struct ListNode* slow = head;
+ struct ListNode* fast = head;
+ struct ListNode* next = NULL;
+ struct ListNode* prev = NULL;
+
+ while (fast != NULL && fast->next != NULL) {
+ slow = slow->next;
+ fast = fast->next->next;
+
+ // Reverse via slow node.
+ next = head->next;
+ head->next = prev;
+ prev = head;
+ head = next;
+ }
+
+ if (fast != NULL) {
+ slow = slow->next;
+ }
+ head = prev;
+
+ // 2. Compare from middle.
+ while (slow != NULL) {
+ if (slow->val != head->val) {
+ return false;
+ }
+ head = head->next;
+ slow = slow->next;
+ }
+ return true;
+}
+
+void checkSolution(int arr[], int len) {
+ struct ListNode* list = listFromArray(arr, len);
+ listDebug(list);
+ printf("is palindrome: %d\n", isPalindrome(list));
+ listFree(list);
+}
+
+int main() {
+ int arr1[] = {1, 2, 2, 1};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]));
+
+ int arr2[] = {1, 2,};
+ checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]));
+
+ return 0;
+}
diff --git a/leetcode/cpp/0237.delete-node-in-a-linked-list/CMakeLists.txt b/leetcode/cpp/0237.delete-node-in-a-linked-list/CMakeLists.txt
new file mode 100644
index 00000000..fe37c2d4
--- /dev/null
+++ b/leetcode/cpp/0237.delete-node-in-a-linked-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0237-delete-node-in-a-linked-list
+ main.c
+ )
diff --git a/leetcode/cpp/0237.delete-node-in-a-linked-list/main.c b/leetcode/cpp/0237.delete-node-in-a-linked-list/main.c
new file mode 100644
index 00000000..717a0895
--- /dev/null
+++ b/leetcode/cpp/0237.delete-node-in-a-linked-list/main.c
@@ -0,0 +1,73 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+void listFree(struct ListNode* head) {
+ while (head != NULL) {
+ struct ListNode* node = head;
+ head = head->next;
+ free(node);
+ }
+}
+
+void listDebug(struct ListNode* head) {
+ while (head != NULL) {
+ printf("%d, ", head->val);
+ head = head->next;
+ }
+ printf("\n");
+}
+
+void deleteNode(struct ListNode* node) {
+ assert(node != NULL);
+ assert(node->next != NULL);
+ struct ListNode* tmp = node->next;
+ node->val = tmp->val;
+ node->next = tmp->next;
+ free(tmp);
+}
+
+void checkSolution(int arr[], int len, int target) {
+ struct ListNode* list = listFromArray(arr, len);
+ struct ListNode* node = list;
+ for (int i = 0; i < target; ++i) {
+ node = node->next;
+ }
+ deleteNode(node);
+ listDebug(list);
+ listFree(list);
+}
+
+int main() {
+ int arr[] = {4, 5, 1, 9};
+ checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 2);
+
+ return 0;
+}
diff --git a/leetcode/cpp/0308.odd-even-linked-list/CMakeLists.txt b/leetcode/cpp/0308.odd-even-linked-list/CMakeLists.txt
new file mode 100644
index 00000000..58a7a525
--- /dev/null
+++ b/leetcode/cpp/0308.odd-even-linked-list/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0308-odd-even-linked-list
+ main.c
+ )
diff --git a/leetcode/cpp/0308.odd-even-linked-list/main.c b/leetcode/cpp/0308.odd-even-linked-list/main.c
new file mode 100644
index 00000000..b72e6098
--- /dev/null
+++ b/leetcode/cpp/0308.odd-even-linked-list/main.c
@@ -0,0 +1,92 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode* next;
+};
+
+struct ListNode* listNew(int val) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = val;
+ node->next = NULL;
+ return node;
+}
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = listNew(arr[i]);
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+void listFree(struct ListNode* head) {
+ while (head != NULL) {
+ struct ListNode* tmp = head;
+ head = head->next;
+ free(tmp);
+ }
+}
+
+void listDebug(struct ListNode* head) {
+ while (head != NULL) {
+ printf("%d, ", head->val);
+ head = head->next;
+ }
+ printf("\n");
+}
+
+struct ListNode* oddEvenList(struct ListNode* head) {
+ if (head == NULL || head->next == NULL) {
+ return NULL;
+ }
+ struct ListNode* even_head = head->next;
+ struct ListNode* even = head->next;
+ struct ListNode* odd = head;
+ struct ListNode* old_next = NULL;
+
+ while (odd != NULL && odd->next != NULL) {
+ printf("odd: %d\n", odd->val);
+ old_next = odd->next;
+ odd->next = old_next->next;
+ odd = old_next->next;
+ }
+
+ /*
+ while (even != NULL && even->next != NULL) {
+ old_next = even->next;
+ even->next = old_next->next;
+ }
+ */
+
+ assert(odd != NULL);
+ //odd->next = even_head;
+ //even->next = NULL;
+ return head;
+}
+
+void checkSolution(int arr[], int len) {
+ struct ListNode* list = listFromArray(arr, len);
+ listDebug(list);
+ list = oddEvenList(list);
+ listDebug(list);
+ listFree(list);
+}
+
+int main() {
+ int arr1[] = {1, 2, 3, 4, 5, 6, 7};
+ checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]));
+
+ //int arr2[] = {2, 1, 3, 5, 6, 4, 7};
+ //checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]));
+ return 0;
+}
diff --git a/leetcode/cpp/0443.string-compression/CMakeLists.txt b/leetcode/cpp/0443.string-compression/CMakeLists.txt
new file mode 100644
index 00000000..86e586b6
--- /dev/null
+++ b/leetcode/cpp/0443.string-compression/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0443-string-compression
+ main.c
+ )
diff --git a/leetcode/cpp/0445.add-two-numbers-ii/CMakeLists.txt b/leetcode/cpp/0445.add-two-numbers-ii/CMakeLists.txt
new file mode 100644
index 00000000..19d85a1c
--- /dev/null
+++ b/leetcode/cpp/0445.add-two-numbers-ii/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0445-add-two-numbers-ii
+ main.c
+ )
diff --git a/leetcode/cpp/0445.add-two-numbers-ii/main.c b/leetcode/cpp/0445.add-two-numbers-ii/main.c
new file mode 100644
index 00000000..bc0c7913
--- /dev/null
+++ b/leetcode/cpp/0445.add-two-numbers-ii/main.c
@@ -0,0 +1,89 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+#include
+#include
+#include
+
+struct ListNode {
+ int val;
+ struct ListNode *next;
+};
+
+struct ListNode* listFromArray(int arr[], int len) {
+ struct ListNode* list = NULL;
+ for (int i = len - 1; i >= 0; --i) {
+ struct ListNode* node = malloc(sizeof(struct ListNode));
+ assert(node != NULL);
+ node->val = arr[i];
+ node->next = list;
+ list = node;
+ }
+ return list;
+}
+
+static
+struct ListNode* listReverse(struct ListNode* list) {
+ struct ListNode* previous = NULL;
+ struct ListNode* next = NULL;
+
+ while (list != NULL) {
+ next = list->next;
+ list->next = previous;
+ previous = list;
+ list = next;
+ }
+ return previous;
+}
+
+struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
+ assert(l1 != NULL);
+ assert(l2 != NULL);
+ l1 = listReverse(l1);
+ l2 = listReverse(l2);
+
+ struct ListNode* l3 = NULL;
+ struct ListNode* tail_node = NULL;
+ int carry = 0;
+
+ while (l1 != NULL || l2 != NULL || carry != 0) {
+ int sum = carry;
+ if (l1 != NULL) {
+ sum += l1->val;
+ l1 = l1->next;
+ }
+ if (l2 != NULL) {
+ sum += l2->val;
+ l2 = l2->next;
+ }
+ carry = sum / 10;
+ struct ListNode* new_node = malloc(sizeof(struct ListNode));
+ assert(new_node != NULL);
+ new_node->val = sum % 10;
+ new_node->next = NULL;
+ if (tail_node == NULL) {
+ l3 = new_node;
+ tail_node = new_node;
+ } else {
+ tail_node->next = new_node;
+ tail_node = new_node;
+ }
+ }
+ return listReverse(l3);
+}
+
+int main() {
+ int arr1[] = {7, 2, 4, 3};
+ int arr2[] = {5, 6, 4};
+ struct ListNode* l1 = listFromArray(arr1, 4);
+ struct ListNode* l2 = listFromArray(arr2, 3);
+ struct ListNode* l3 = addTwoNumbers(l1, l2);
+ while (l3 != NULL) {
+ printf("%d, ", l3->val);
+ l3 = l3->next;
+ }
+ printf("\n");
+
+ return 0;
+}
diff --git a/leetcode/cpp/0695.max-area-of-island/CMakeLists.txt b/leetcode/cpp/0695.max-area-of-island/CMakeLists.txt
new file mode 100644
index 00000000..6d9ebb08
--- /dev/null
+++ b/leetcode/cpp/0695.max-area-of-island/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0695-max-area-of-island
+ main.c
+ )
diff --git a/leetcode/cpp/0704.binary-search/CMakeLists.txt b/leetcode/cpp/0704.binary-search/CMakeLists.txt
new file mode 100644
index 00000000..e674b68a
--- /dev/null
+++ b/leetcode/cpp/0704.binary-search/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0704-binary-search
+ main.c
+ )
diff --git a/leetcode/cpp/0704.binary-search/main.c b/leetcode/cpp/0704.binary-search/main.c
new file mode 100644
index 00000000..921b41c4
--- /dev/null
+++ b/leetcode/cpp/0704.binary-search/main.c
@@ -0,0 +1,30 @@
+// Copyright (c) 2024 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+int search(int* nums, int numsSize, int target) {
+ if (numsSize == 0) {
+ return -1;
+ }
+
+ int left = 0;
+ int right = numsSize - 1;
+ int middle;
+
+ while (left <= right) {
+ middle = left + (right - left) / 2;
+ if (nums[middle] == target) {
+ return middle;
+ }
+ if (nums[middle] > target) {
+ right = middle - 1;
+ } else {
+ left = middle + 1;
+ }
+ }
+ return -1;
+}
+
+int main() {
+ return 0;
+}
diff --git a/leetcode/cpp/0994.rotting-oranges/CMakeLists.txt b/leetcode/cpp/0994.rotting-oranges/CMakeLists.txt
new file mode 100644
index 00000000..e091f9f6
--- /dev/null
+++ b/leetcode/cpp/0994.rotting-oranges/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-0994-rotting-oranges
+ main.c
+ )
diff --git a/leetcode/cpp/1090.largest-values-from-labels/CMakeLists.txt b/leetcode/cpp/1090.largest-values-from-labels/CMakeLists.txt
new file mode 100644
index 00000000..5e43dc78
--- /dev/null
+++ b/leetcode/cpp/1090.largest-values-from-labels/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-1090-largest-values-from-labels
+ main.c
+ )
diff --git a/leetcode/cpp/1090.largest-values-from-labels/main.c b/leetcode/cpp/1090.largest-values-from-labels/main.c
new file mode 100644
index 00000000..e69de29b
diff --git a/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/CMakeLists.txt b/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/CMakeLists.txt
new file mode 100644
index 00000000..b344be06
--- /dev/null
+++ b/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/CMakeLists.txt
@@ -0,0 +1,4 @@
+
+add_executable(lc-1423-maximum-points-you-can-obtain-from-cards
+ main.c
+ )
diff --git a/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/main.c b/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/main.c
new file mode 100644
index 00000000..e69de29b
diff --git a/leetcode/cpp/CMakeLists.txt b/leetcode/cpp/CMakeLists.txt
new file mode 100644
index 00000000..6173afba
--- /dev/null
+++ b/leetcode/cpp/CMakeLists.txt
@@ -0,0 +1,63 @@
+
+cmake_minimum_required(VERSION 3.10)
+project(leetcode-cpp)
+
+add_subdirectory(0002.add-two-numbers)
+add_subdirectory(0003.longest-substring-without-repeating-characters)
+add_subdirectory(0005.longest-palindromic-substring)
+add_subdirectory(0007.reverse-integer)
+add_subdirectory(0008.string-to-integer-atoi)
+add_subdirectory(0011.container-with-most-water)
+add_subdirectory(0014.longest-common-prefix)
+add_subdirectory(0015.3sum)
+add_subdirectory(0016.3sum-closest)
+add_subdirectory(0018.4sum)
+add_subdirectory(0019.remove-nth-node-from-end-of-list)
+add_subdirectory(0020.valid-parentheses)
+add_subdirectory(0021.merge-two-sorted-lists)
+#add_subdirectory(0025.reverse-nodes-in-k-group)
+add_subdirectory(0026.remove-duplicates-from-sorted-array)
+add_subdirectory(0027.remove-element)
+#add_subdirectory(0031.next-permutation)
+add_subdirectory(0034.find-first-and-last-position-of-element-in-sorted-array)
+#add_subdirectory(0035.search-insert-position)
+#add_subdirectory(0053.maximum-subarray)
+add_subdirectory(0061.rotate-list)
+#add_subdirectory(0062.unique-paths)
+add_subdirectory(0066.plus-one)
+add_subdirectory(0070.climbing-stairs)
+add_subdirectory(0082.remove-duplicates-from-sorted-list-ii)
+add_subdirectory(0083.remove-duplicates-from-sorted-list)
+add_subdirectory(0086.partition-list)
+add_subdirectory(0088.merge-sorted-array)
+add_subdirectory(0092.reverse-linked-list-ii)
+add_subdirectory(0093.restore-ip-addresses)
+add_subdirectory(0098.validate-binary-search-tree)
+add_subdirectory(0101.symmetric-tree)
+add_subdirectory(0102.binary-tree-level-order-traversal)
+add_subdirectory(0103.binary-tree-zigzag-level-order-traversal)
+add_subdirectory(0107.binary-tree-level-order-traversal-ii)
+add_subdirectory(0108.convert-sorted-array-to-binary-search-tree)
+add_subdirectory(0114.flatten-binary-tree-to-linked-list)
+add_subdirectory(0139.word-break)
+add_subdirectory(0141.linked-list-cycle)
+add_subdirectory(0142.linked-list-cycle-ii)
+add_subdirectory(0144.binary-tree-preorder-traversal)
+add_subdirectory(0160.intersection-of-two-linked-lists)
+#add_subdirectory(0162.find-peak-element)
+add_subdirectory(0191.number-of-1-bits)
+#add_subdirectory(0199.binary-tree-right-side-view)
+#add_subdirectory(0200.number-of-islands)
+add_subdirectory(0203.remove-linked-list-elements)
+add_subdirectory(0206.reverse-linked-list)
+#add_subdirectory(0215.kth-largest-element-in-an-array)
+add_subdirectory(0234.palindrome-linked-list)
+add_subdirectory(0237.delete-node-in-a-linked-list)
+add_subdirectory(0308.odd-even-linked-list)
+#add_subdirectory(0443.string-compression)
+add_subdirectory(0445.add-two-numbers-ii)
+#add_subdirectory(0695.max-area-of-island)
+add_subdirectory(0704.binary-search)
+#add_subdirectory(0994.rotting-oranges)
+#add_subdirectory(1090.largest-values-from-labels)
+#add_subdirectory(1423.maximum-points-you-can-obtain-from-cards)
diff --git a/leetcode/rust/0001.two-sum/Cargo.toml b/leetcode/rust/0001.two-sum/Cargo.toml
new file mode 100644
index 00000000..9571a5d2
--- /dev/null
+++ b/leetcode/rust/0001.two-sum/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "lc-0001-two-sum"
+version = "0.1.0"
+edition = "2021"
+publish = false
+
+[dependencies]
diff --git a/leetcode/rust/0001.two-sum/src/main.rs b/leetcode/rust/0001.two-sum/src/main.rs
new file mode 100644
index 00000000..7aae6857
--- /dev/null
+++ b/leetcode/rust/0001.two-sum/src/main.rs
@@ -0,0 +1,86 @@
+// Copyright (c) 2023 Xu Shaohua . All rights reserved.
+// Use of this source is governed by General Public License that can be
+// found in the LICENSE file.
+
+use std::collections::HashMap;
+
+fn solution1(nums: Vec, target: i32) -> Vec {
+ let mut result = Vec::new();
+ for (i, m) in nums.iter().enumerate() {
+ for (j, n) in nums.iter().enumerate() {
+ if i != j && m + n == target {
+ result.push(i as i32);
+ result.push(j as i32);
+ return result;
+ }
+ }
+ }
+ result
+}
+
+fn solution2(nums: Vec, target: i32) -> Vec {
+ let len = nums.len();
+ for i in 0..(len - 1) {
+ for j in (i + 1)..len {
+ if i != j && nums[i] + nums[j] == target {
+ return vec![i as i32, j as i32];
+ }
+ }
+ }
+ Vec::new()
+}
+
+fn solution3(nums: Vec, target: i32) -> Vec {
+ let mut visited = HashMap::with_capacity(nums.len());
+ for (i, item) in nums.iter().enumerate() {
+ if let Some(&j) = visited.get(&(target - item)) {
+ return vec![j as i32, i as i32];
+ } else {
+ visited.insert(item, i);
+ }
+ }
+
+ Vec::new()
+}
+
+pub type SolutionFn = fn(Vec