Git Product home page Git Product logo

leetcode-swift's People

Contributors

adrianbindc avatar antranapp avatar cee avatar charlenejiang avatar codetp avatar daiyue avatar demonkoo avatar desgard avatar dingwilson avatar gannasong avatar ihacksubhodip avatar ilyarmnzhdn avatar imfaker avatar jindulys avatar kkj1996 avatar maligh avatar mssjappjwt avatar pepsikirk avatar quaggie avatar readmecritic avatar rockbruno avatar shawnma16 avatar soapyigu avatar sunsetwan avatar wamaker avatar wanghaiqian avatar wenchaod avatar wqfan avatar xieweialex avatar zj9205 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leetcode-swift's Issues

Link to problem #22 's solution is incorrect

The link to solution in README.md is off. Currently it's https://github.com/soapyigu/LeetCode_Swift/blob/master/Math/GenerateParentheses.swift, seems that it should be https://github.com/soapyigu/LeetCode_Swift/blob/master/DP/GenerateParentheses.swift instead.

Number of Islands complexity

Currently complexity: O((n^2)!)

  1. Tiles are switched to 0 once visited, so they cannot be visited again. Although the search can look into 4 directions for each tile, it doesn't keep going.
  2. The instructions don't say that the board is a square (although it is in the example).

Suggested complexity: O(mn)

File and folder structure

When I cloned this project I had to individually select each playground file to view the implementation. I took all your LeetCode_Swift .swift files and neatly put them in one Xcode project folder so it will be easier for anyone cloning it to have it in one place. I also added .playground files to each one. Is that something thats you are willing to consider pulling?

Your Reverse String Solution Time Complexity is O(1)

as Apple API Reference say:

Swift Standard Library - AnyBidirectiona - lCollection

reversed()

Instance Method
reversed()
Returns a view presenting the elements of the collection in reverse order.
...
...
If you need a reversed collection of the same type, you may be able to use the collection’s sequence-based or collection-based initializer. For example, to get the reversed version of a string, reverse its characters and initialize a new String instance from the result.

let reversedWord = String(word.characters.reversed())
print(reversedWord)
// Prints "sdrawkcaB"

Complexity: O(1)

[Solution] 303. Range Sum Query - Immutable

/**

*/

class NumArray {
	var sums: [Int] = []

	init(_ nums: [Int]) {
		var currentSum = 0

		for num in nums {
			currentSum += num
			sums.append(currentSum)
		}
		print(sums)
	}

	func sumRange(_ left: Int, _ right: Int) -> Int {
		if left == 0 {
			return sums[right]
		}
		return sums[right] - sums[left-1]
	}
}

"Longest Substring Without Repeating Characters", answer is wrong!

use "abcdefghiepoiuq" to test, print 9. but 10 Actually.

My code prints correctly:

    func lengthOfLongestSubstring(_ string: String) -> Int {
        var flags = Array(repeating: -1, count: 128)
        var maxLength = 0
        var startIndex = -1
        
        for (index, char) in string.enumerated() {
            let asciiValue = Int(char.asciiValue ?? 0)
            if flags[asciiValue] > startIndex {
                if index - startIndex > maxLength {
                    maxLength = index - startIndex
                }
                startIndex = flags[asciiValue] + 1
                flags[asciiValue] = index
                continue
            }
            flags[asciiValue] = index
        }
        
        if string.count - startIndex > maxLength {
            maxLength = string.count - startIndex
        }
        return maxLength
    }

README for explanation

I think it would be cool if there was an option for each contributor to be able to explain in a REAME file their thought process and how they came up with their solutions. I think it would help people understand someone else's logic. It will also help it read better on github. I can add additional changes to the Architect branch. What do you think?

Improvements for Trapping Rain water Problem.

Here I just tried to improve the space complexity.

your Solution ->

https://github.com/soapyigu/LeetCode-Swift/blob/master/Math/TrappingRainWater.swift

Mine with -> O(n) TC and O(1) SC

`
class Solution {
func trap(_ height: [Int]) -> Int {
var waterContainerResult = 0
var left = 0, right = height.count - 1, leftMax = 0, rightMax = 0

    while left < right{
        if height[left] < height[right]{
            if height[left] >= leftMax{
                leftMax = height[left]
            }else{
                waterContainerResult = waterContainerResult + leftMax - height[left]
            }
            left += 1
        }else{
            if height[right] >= rightMax{
                rightMax = height[right]
            }else{
                waterContainerResult = waterContainerResult + rightMax - height[right]
            }
            right -= 1
        }
    }

    return waterContainerResult
}

}
`

Runtime: 28 ms, faster than 93.62% of Swift online submissions for Trapping Rain Water.
Memory Usage: 20.9 MB, less than 5.01% of Swift online submissions for Trapping Rain Water.

If it looks fine, I will raise a PR for this.

[LongestPalindromicSubstring] Question

Hi there,

Can u give a bit explanation of the following lines:

      for i in 0..<arr.count {
            searchPalindrome(arr,i,i,&start,&maxLength)
            searchPalindrome(arr,i,i+1,&start,&maxLength)
        }

and:

      if maxLength < r - l - 1 {
            start = l + 1
            maxLength = r - l - 1
        }

Best Regards

Majority Element [Array Problem] algorithm issues.

The current algorithm will always return the last integer to displace the major as the new majority element. Take var array = [1,1,1,2,2,3] for example. The answer should be 1 but the algorithm returns 3.

Also, majority element must appear more than number of elements/2 times. The algorithm doesn't take this in to account.

Thanks for all your hard work!

Code is not getting complied on Xcode 9.4.1 for "https://github.com/soapyigu/LeetCode-Swift/blob/master/String/ValidPalindrome.swift"

Slight Correction..

`func isPalindrome(_ s: String) -> Bool {
// Make String into an array of lowercase Characters
let characters = s.lowercased()

// Only keep alphanumeric characters.
let cleanedCharacter = characters.filter { character in
    return character.description.rangeOfCharacter(from: CharacterSet.alphanumerics) != nil
}

// Making array of cleaned characters
let cleaned = Array(cleanedCharacter)

// Compare values at mirroring indices.
let total = cleaned.count
for i in 0 ..< total/2 {
    if cleaned[i] != cleaned[total - 1 - i] {
        return false
    }
}
return true

}
`

“SlidingWindowMaximum” error

https://github.com/soapyigu/LeetCode-Swift/blob/master/Array/SlidingWindowMaximum.swift

testcase: [ 2, 3, 4, 2, 6, 2, 5, 1 ]
expected: [4, 4, 6, 6, 6, 5]
errorResult: [4, 4, 6, 6, 5, 5]

Modify

func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
        guard k > 0 && k <= nums.count else {
            return []
        }

        var maxIdx = [Int]()
        var res = [Int]()

        for i in 0..<nums.count {
            while maxIdx.count > 0 && nums[maxIdx.last!] <= nums[i] {
                maxIdx.removeLast()
            }
            maxIdx.append(i)
            if i >= k - 1 {
                if maxIdx.first! + k == i {
                    maxIdx.removeFirst()
                }
                res.append(nums[maxIdx.first!])
            }
        }

        return res
    }

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.