diff options
Diffstat (limited to 'leetcode')
| -rw-r--r-- | leetcode/1-two-sum/solution.rkt | 16 | ||||
| -rw-r--r-- | leetcode/20-valid-parentheses/solution.rkt | 33 |
2 files changed, 0 insertions, 49 deletions
diff --git a/leetcode/1-two-sum/solution.rkt b/leetcode/1-two-sum/solution.rkt deleted file mode 100644 index e155160..0000000 --- a/leetcode/1-two-sum/solution.rkt +++ /dev/null @@ -1,16 +0,0 @@ -#lang racket - -;; https://leetcode.com/problems/two-sum/description/ - -(define/contract (two-sum nums target) - (-> (listof exact-integer?) exact-integer? (listof exact-integer?)) - (define h (make-hash)) - (define (loop nums index) - (define n (car nums)) - (define diff (- target n)) - (if (hash-has-key? h diff) - (list index (hash-ref h diff)) - (let () - (hash-set! h n index) - (loop (cdr nums) (+ 1 index))))) - (loop nums 0)) diff --git a/leetcode/20-valid-parentheses/solution.rkt b/leetcode/20-valid-parentheses/solution.rkt deleted file mode 100644 index 559b90c..0000000 --- a/leetcode/20-valid-parentheses/solution.rkt +++ /dev/null @@ -1,33 +0,0 @@ -#lang racket - -;; https://leetcode.com/problems/valid-parentheses/ - -(define (is-left-paren c) - (or (eq? c #\u28) - (eq? c #\[) - (eq? c #\{))) - -(define (right-paren c) - (cond ((eq? c #\u28) #\u29) - ((eq? c #\[) #\]) - ((eq? c #\{) #\}))) - -(define/contract (is-valid s) - (-> string? boolean?) - (define (loop s stack) - (if (= 0 (string-length s)) - (null? stack) - (let () - (define c (string-ref s 0)) - (define remain (substring s 1)) - (if (is-left-paren c) - (loop remain (cons c stack)) - (if (null? stack) - #f - (let () - (define stack-top (car stack)) - (if (eq? c (right-paren stack-top)) - (loop remain (cdr stack)) - #f))))))) - (loop s '())) - |
