✅ [Solution] Swift: Next Greater Element II
swift\nclass Solution {\n    func nextGreaterElements(_ nums: [Int]) -> [Int] {\n        let len = nums.count\n        var arr = Array(repeating: -1, count: len
```swift\nclass Solution {\n    func nextGreaterElements(_ nums: [Int]) -> [Int] {\n        let len = nums.count\n        var arr = Array(repeating: -1, count: len)\n        var stack: [Int] = []\n        for i in 0..<len*2 {\n            while !stack.isEmpty, nums[stack.last!] < nums[i % len] {\n                arr[stack.removeLast()] = nums[i % len]\n            }\n            stack.append(i % len)\n        }\n        return arr\n    }\n}\n```\n\n<hr>\n\n<details>\n<summary><img src="https://git.io/JDblm" height="24"> <b>TEST CASES</b></summary>\n\n<pre>\nResult: Executed 2 tests, with 0 failures (0 unexpected) in 0.016 (0.018) seconds\n</pre>\n\n```swift\nimport XCTest\n\nclass Tests: XCTestCase {\n\n    private let solution = Solution()\n    \n    // Explanation: The first 1\'s next greater number is 2;\n    // The number 2 can\'t find next greater number.\n    // The second 1\'s next greater number needs to search circularly, which is also 2.\n    func test0() {\n        let value = solution.nextGreaterElements([1,2,1])\n        XCTAssertEqual(value, [2,-1,2])\n    }\n    \n    func test1() {\n        let value = solution.nextGreaterElements([1,2,3,4,3])\n        XCTAssertEqual(value, [2,3,4,-1,4])\n    }\n}\n\nTests.defaultTestSuite.run()\n```\n\n</details>
['Swift']