Skip to content

Latest commit

 

History

History
142 lines (109 loc) · 4.99 KB

readme-header.md

File metadata and controls

142 lines (109 loc) · 4.99 KB

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