본문 바로가기

알고리즘/LeetCode7

[LeetCode] Single Number III Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is not important. So in the above example, [5, 3] is also correct. Your algorithm should run in linear runtime complexity. Could you implement it using.. 2020. 7. 24.
[LeetCode] Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree [3,9,20,null,null,15,7], return its zigzag level order traversal as: [ [3], [20,9], [15,7] ] Solution 1. root를 담은 q가 있는동안 반복한다. 2. q의 길이를 cnt로 놓고 cnt가 있는 동안 반복문을 실행한다. 3. q의 가장 첫번째를 pop하여 그 value값을.. 2020. 7. 23.
[LeetCode] Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. Example: board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] Given word = "ABCCED", return true. Given word = "SEE", r.. 2020. 7. 22.
[LeetCode] Remove Linked List Elements Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 Solution 1. 사실 아직 잘 이해가 가지않는다. LinkedList를 공부하고 다시풀어보자. #(3) Remove Linked List Elements # Definition for singly-linked list. class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def removeElements(self, head: .. 2020. 7. 21.
[LeetCode] Add Binary Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010", b = "1011" Output: "10101" Constraints: Each string consists only of '0' or '1' characters. 1 2020. 7. 20.
[LeetCode] Course Schedule II There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses. There may be multiple correct orders, you just need t.. 2020. 7. 19.