Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ffce57

Browse files
committed
add 83, 118, 119, 989, 1002, 1184, 1221, 2016 at swift
1 parent 0fb599f commit 8ffce57

8 files changed

+182
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 83. Remove Duplicates from Sorted List
3+
* https://leetcode.com/problems/remove-duplicates-from-sorted-list/
4+
**/
5+
6+
/*
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* public var val: Int
10+
* public var next: ListNode?
11+
* public init() { self.val = 0; self.next = nil; }
12+
* public init(_ val: Int) { self.val = val; self.next = nil; }
13+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
14+
* }
15+
*/
16+
class Solution {
17+
func deleteDuplicates(_ head: ListNode?) -> ListNode? {
18+
var node = head
19+
while node?.next != nil {
20+
if node!.val == node!.next!.val {
21+
node!.next = node!.next!.next
22+
} else {
23+
node = node?.next
24+
}
25+
}
26+
return head
27+
}
28+
}

‎swift/0118-pascals-triangle.swift‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 118. Pascal's Triangle
3+
* https://leetcode.com/problems/pascals-triangle/
4+
**/
5+
6+
class Solution {
7+
func generate(_ numRows: Int) -> [[Int]] {
8+
var triangle = [[1]], rows = 1
9+
while rows < numRows {
10+
rows += 1
11+
triangle.append( [1] )
12+
for i in 1 ..< rows-1 {
13+
triangle[rows-1].append( triangle[rows-2][i-1] + triangle[rows-2][i] )
14+
}
15+
triangle[rows-1].append(1)
16+
}
17+
return triangle
18+
}
19+
}

‎swift/0119-pascals-triangle-ii.swift‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 119. Pascal's Triangle II
3+
* https://leetcode.com/problems/pascals-triangle-ii/
4+
**/
5+
6+
class Solution {
7+
func getRow(_ rowIndex: Int) -> [Int] {
8+
var row = [1], nowIndex = 0
9+
while nowIndex < rowIndex {
10+
nowIndex += 1
11+
var temp = [1]
12+
for i in 1 ..< nowIndex {
13+
temp.append( row[i-1] + row[i] )
14+
}
15+
temp.append(1)
16+
row = temp
17+
}
18+
return row
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 989. Add to Array-Form of Integer
3+
* https://leetcode.com/problems/add-to-array-form-of-integer/
4+
**/
5+
6+
class Solution {
7+
func addToArrayForm(_ num: [Int], _ k: Int) -> [Int] {
8+
var num = num, k = k, i = num.count-1, prev = 0
9+
while (k > 0 || prev > 0) && i >= 0 {
10+
var v: Int = k % 10 + prev + num[i]
11+
prev = 0
12+
if v >= 10 {
13+
v %= 10
14+
prev = 1
15+
}
16+
num[i] = v
17+
i -= 1
18+
k /= 10
19+
}
20+
while k > 0 || prev > 0 {
21+
var v: Int = k % 10 + prev
22+
prev = 0
23+
if v >= 10 {
24+
v %= 10
25+
prev = 1
26+
}
27+
num.insert(v, at: 0)
28+
k /= 10
29+
}
30+
return num
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 1002. Find Common Characters
3+
* https://leetcode.com/problems/find-common-characters/
4+
**/
5+
6+
class Solution {
7+
func commonChars(_ words: [String]) -> [String] {
8+
var mainDict: [Character: Int] = [:] // ch : count
9+
for ch in Array(words[0]) {
10+
mainDict[ch, default: 0] += 1
11+
}
12+
13+
for i in 1 ..< words.count {
14+
var wordDict: [Character: Int] = [:]
15+
for ch in Array(words[i]) {
16+
wordDict[ch, default: 0] += 1
17+
}
18+
for key in mainDict.keys where mainDict[key]! > 0 {
19+
mainDict[key] = min(mainDict[key]!, wordDict[key] ?? 0)
20+
}
21+
}
22+
23+
var ans: [String] = []
24+
for key in mainDict.keys where mainDict[key]! > 0 {
25+
while mainDict[key]! > 0 {
26+
ans.append( String(key) )
27+
mainDict[key]! -= 1
28+
}
29+
}
30+
31+
return ans
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* 1184. Distance Between Bus Stops
3+
* https://leetcode.com/problems/distance-between-bus-stops/
4+
**/
5+
6+
class Solution {
7+
func distanceBetweenBusStops(_ distance: [Int], _ start: Int, _ destination: Int) -> Int {
8+
let left = min(start, destination)
9+
let right = max(start, destination)
10+
let first = distance.enumerated().filter { 0ドル.offset >= left && 0ドル.offset < right }.map { 0ドル.element }.reduce(0, +)
11+
let second = distance.reduce(0, +) - first
12+
return min(first, second)
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* 1221. Split a String in Balanced Strings
3+
* https://leetcode.com/problems/split-a-string-in-balanced-strings/
4+
**/
5+
6+
class Solution {
7+
func balancedStringSplit(_ s: String) -> Int {
8+
var ans = 0, balance = 0
9+
for ch in s {
10+
balance += ch == "R" ? 1 : -1
11+
if balance == 0 {
12+
ans += 1
13+
}
14+
}
15+
return ans
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 2016. Maximum Difference Between Increasing Elements
3+
* https://leetcode.com/problems/maximum-difference-between-increasing-elements/
4+
**/
5+
6+
class Solution {
7+
func maximumDifference(_ nums: [Int]) -> Int {
8+
var ans = -1
9+
var min = nums[0]
10+
for num in nums {
11+
if num <= min {
12+
min = num
13+
} else {
14+
ans = max(ans, num - min)
15+
}
16+
}
17+
return ans
18+
}
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /