Giter VIP home page Giter VIP logo

leetcode-swift's Introduction

LeetCode Solution Swift

language badge progress license badge

About

This project aims to provide swift version solutions for algorithm problems on LeetCode Online Judge.

For current stage, the main target is to provide solutions for all problems, so there will be no well-considered test cases provided, also Xcode unit test is not included. But all the solutions here can pass the related problems on LeetCode OJ.

Some of the algorithm analyses will be provide alongside the solutions, some may not, and will be updated in the future.

HowTo

To run a specific solution, please change the question number in main.swift, e.g. q371.getSolution() to run solution for question 371 "Sum Of Two Integers".

And modify static func getSolution() in struct Q0371 to provide test cases

struct q371 {
    
    class Solution {
        fun getSum(a: Int, _ b: Int) -> Int {
            
            var sum = a
            var carry = b
            while carry != 0 {
                let t = sum ^ carry
                carry = (sum & carry) << 1
                sum = t
            }
            return sum
            
        }
    }
    
    static fun getSolution() -> Void {
        print(Solution().getSum(-1203940,1230912848102))
    }
}

Notes

Helper Class

There are lots of problems on LeetCode OJ using two data structures: Linked List and Binary Tree. LeetCode OJ use following serialized format to describe a binary tree.

The input [1,nil,2,3] represents the serialized format of a binary tree using level order traversal, where nil signifies a path terminator where no node exists below.

Some examples:

  • []
Empty tree. The root is a reference to nil
  • [1,2,3]
       1
      / \
     2   3
  • [1,nil,2,3]
       1
        \
         2
        /
       3
  • [5,4,7,3,nil,2,nil,-1,nil,9]
       5
      / \
     4   7
    /   /
   3   2
  /   /
 -1  9

The helper class: LinkedListHelper and BinaryTreeHelper comply to this serialized format and is very useful to deserialize and visualize these two data structures.

They can be used to build linked list and complicated binary tree in just one line:

let root = BinaryTreeHelper.buildTree(withNodes: [10,7,12,6,9,11,15,nil,3,8,nil,nil,nil,13,16])

let head = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,4,5,6,7])

Print linked list and binary tree visually for easy debugging:

print(head)

1-->2-->3-->4-->5-->6-->7

print(root)

         ┌───── 15
 ┌───── 14
               ┌───── 13
        └───── 12
                └───── 11
10
        ┌───── 8
               └───── 7
 └───── 5
                ┌───── 4
                       └───── 2
         └───── 1

Utility Script

Solution Symlink

The solution source codes in leetcode-solution-swift Xcode project is structured based on the problem difficulty: Easy, Medium and Hard.

But the utility python script generate-solution-symlink.py can be used to generate source code symlinks in centralized(../solution-links/centralized) and category based structure(../solution-links/category). Simply follow commands below after clone the repo to local disk.

cd leetcode-solution-swift/util
./generate-solution-symlink.py

The category information is stored in each source code file, generally at 5th line after "// Category* : ", one solutions can relate to more than one algorithm categories which separated by white space.

README.md

This README.md is auto generated by script generate-readme.py. Beacause manually update the solution list below is a wast of time. This script use readme-header.md as its datasource, so any change to README.md should be made in readme-header.md, and auto generate to README.md

Solution List

The problems listed here only contain those whose solution is already provided by this project. And the list is auto genarated by generate-readme.py.

If you want a full list of questions, please check leetcode.com

# Title Solution Difficulty Note
7 Reverse Integer swift Easy
9 Palindrome Number swift Easy
13 Roman To Integer swift Easy
14 Longest Common Prefix swift Easy
19 Remove Nth Node From End Of List swift Easy
20 Valid Parentheses swift Easy
21 Merge Two Sorted Lists swift Easy
24 Swap Nodes In Pairs swift Easy
26 Remove Duplicates From Sorted Array swift Easy
27 Remove Element swift Easy
28 Implement Strstr swift Easy
36 Valid Sudoku swift Easy
38 Count And Say swift Easy
58 Length Of Last Word swift Easy
66 Plus One swift Easy
67 Add Binary swift Easy
70 Climbing Stairs swift Easy
83 Remove Duplicates From Sorted List swift Easy
88 Merge Sorted Array swift Easy
100 Same Tree swift Easy
101 Symmetric Tree swift Easy
102 Binary Tree Level Order Traversal swift Easy
103 Binary Tree Zigzag Level Order Traversal swift Medium
104 Maximum Depth Of Binary Tree swift Easy
107 Binary Tree Level Order Traversal II swift Easy
110 Balanced Binary Tree swift Easy
111 Minimum Depth Of Binary Tree swift Easy
112 Path Sum swift Easy
118 Pascals Triangle swift Easy
119 Pascals Triangle II swift Easy
121 Best Time To Buy And Sell Stock swift Easy
122 Best Time To Buy And Sell Stock II swift Medium
141 Linked List Cycle swift Easy
160 Intersection Of Two Linked Lists swift Easy
160 Majority Element swift Easy
171 Excel Sheet Column Number swift Easy
172 Factorial Trailing Zeroes swift Easy
190 Reverse Bits swift Easy
191 Number Of 1 Bits swift Easy
198 House Robber swift Easy
202 Happy Number swift Easy
203 Remove Linked List Elements swift Easy
204 Count Primes swift Easy
205 Isomorphic Strings swift Easy
206 Reverse Linked List swift Easy
213 House Robber II swift Medium
217 Contains Duplicate swift Easy
219 Contains Duplicate II swift Easy
223 Rectangle Area swift Easy
225 Implement Stack Using Queues swift Easy
226 Invert Binary Tree swift Easy
231 Power Of Two swift Easy
232 Implement Queue Using Stacks swift Easy
234 Palindrome Linked List swift Easy
235 Lowest Common Ancestor Of A Binary Search Tree swift Easy
237 Delete Node In A Linked List swift Easy
242 Valid Anagram swift Easy
257 Binary Tree Paths swift Easy
258 Add Digits swift Easy
263 Ugly Number swift Easy
283 Move Zeroes swift Easy
290 Word Pattern swift Easy
292 Nim Games swift Easy
299 Bulls And Cows swift Easy
303 Range Sum Query Immutable swift Easy
326 Power Of Three swift Easy
334 Reverse String swift Easy
342 Power Of Four swift Easy
345 Reverse Vowels Of A String swift Easy
349 Intersection Of Two Arrays swift Easy
350 Intersection Of Two Arrays II swift Easy
371 Sum Of Two Integers swift Easy

leetcode-swift's People

Contributors

wty21cn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.