3/19 leetcode 문제 풀이

3072 단어 leetcodeleetcode

사전 학습!

Set 객체는 자료형에 관계 없이 원시값과 객체 참조 모두 유일한 값을 저장할 수 있다.

var set = Set();

set.add(1); // Set {1}

set.has(1); // true

set.delete(1); // 1 delete

연결 리스트정리
링크텍스트

#141

참고: 승지니어

var hasCycle = function(head) {

let walker = head;
let runner = head;
    
while(runner !=null){
    runner = runner.next;
    if(runner!=null){
        runner = runner.next;
        walker = walker.next;
        if(runner == walker) break;
        }else break;
    }
    if(runner==null) return false;
    else return true;

};


#155

참고: 승지니어 & https://leetcode.com/problems/min-stack/discuss/204269/javascript

/**

  • initialize your data structure here.
    */
    var MinStack = function() {
    this.stack= [];
    };

/**

  • @param {number} val
  • @return {void}
    */
    MinStack.prototype.push = function(val) {
    let min = this.stack.length === 0? val: this.stack[this.stack.length -1].min;
    this.stack.push({val:val, min: Math.min(min,val)});
    };

/**

  • @return {void}
    */
    MinStack.prototype.pop = function() {
    if(this.stack.length > 0){
    this.stack.pop();
    }

};

/**

  • @return {number}
    */
    MinStack.prototype.top = function() {
    if(this.stack.length > 0){
    return this.stack[this.stack.length-1].val;
    }
    };

/**

  • @return {number}
    */
    MinStack.prototype.getMin = function() {
    if(this.stack.length>0){
    return this.stack[this.stack.length-1].min;
    }
    };

/**

  • Your MinStack object will be instantiated and called as such:
  • var obj = new MinStack()
  • obj.push(val)
  • obj.pop()
  • var param_3 = obj.top()
  • var param_4 = obj.getMin()
    */

#160

참고: https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/802204/Javascript-Solution%3A160

var getIntersectionNode = function(headA, headB) {

let a = headA;
let b = headB;

while(a != null && b != null){
    a = a.next;
    b = b.next;
    
    if(a === b){
        return a;
    }
    if(a === null){
        a = headB;
    }
    if(b === null){
        b = headA;
    }

}
return a ;

};

참고자료

-javascript Set() 객체
링크텍스트
-linked list
링크텍스트

좋은 웹페이지 즐겨찾기