본문 바로가기

내 맘대로 알고리즘

(59)
Leetcode[day15] - Product of Array Except Self Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer. Note: Please solve it without division and in O(n). Follow up:Could you solve it with co..
Leetcode[day14] - Perform String Shifts You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [direction, amount]: - direction can be 0 (for left shift) or 1 (for right shift). - amount is the amount by which string s is to be shifted. - A left shift by 1 means remove the first character of s and append it to the end. - Similarly, a right shift by 1 means remove the last character of s and..
Leetcode[day13] - Contiguous Array Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Note: The length of the given binary array will not exceed 50,000. Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Input: [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. 1...
Leetcode[day12] - Last Stone Weight We have a collection of stones, each stone has a positive integer weight. Each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights x and y with x
LeetCode[day11] - Diameter of Binary Tree Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them 1. 알고리즘을 해결하기 위해서 생각한 방법 - 우선, 해당 문제는 이진 트리가 input으로 주어진다. - 이진트리란 무엇일까? 이진트리는..
LeetCode[day10] - Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. Methods pop, top and getMin operations will always be called on non-empty stacks. Input ["MinStack","push","push","push","getMi..
LeetCode[day9] - Backspace String Compare Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Note that after backspacing an empty text, the text will continue empty. Note: 1 0) {skipS--; i--;} else break; } while (j >= 0) { // Find position of next possible char in build(T) if (T.charAt(j) == '#') {skipT++; j--;} else if (skipT > 0) {skipT--; j--;} else break; ..
Leetcode[day8] - Middle of the Linked List Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. Note: The number of nodes in the given list will be between 1 and 100. Input: [1,2,3,4,5] Output: Node 3 from this list (Serialization: [3,4,5]) The returned node has value 3. (The judge's serialization of this node is [3,4,5]). Note that w..