본문 바로가기

Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice.Example:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. My solution: class Solution: def twoSum(self, nums, target): """ :type nums: List[i.. 더보기
Move Zeroes Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12] Output: [1,3,12,0,0]Note:You must do this in-place without making a copy of the array.Minimize the total number of operations. My solution: class Solution: def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return .. 더보기
Plus One Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.You may assume the integer does not contain any leading zero, except the number 0 itself.Example 1:Input: [1,2,3] Output: [1,2,4] Explanation: The array represents.. 더보기
Intersection of Two Arrays II Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2] Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9]Note:Each element in the result should appear as many times as it shows in both arrays.The result can be in any order.Follow up:What if the given array is already sorted? How would you optimize your alg.. 더보기
Single Number Given a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?Example 1:Input: [2,2,1] Output: 1 Example 2:Input: [4,1,2,1,2] Output: 4 My solution:class Solution: def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ distinct = .. 더보기
Contains Duplicate Given an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.Example 1:Input: [1,2,3,1] Output: trueExample 2:Input: [1,2,3,4] Output: falseExample 3:Input: [1,1,1,3,3,4,3,2,4,2] Output: true My solution:class Solution: def containsDuplicate(self, nu.. 더보기
Rotate Array Given an array, rotate the array to the right by k steps, where k is non-negative.Example 1:Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2:Input: [-1,-100,3,99] and k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the r.. 더보기
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).Example 1:Input: [7,1,5,3,6,4].. 더보기
Reverse Linked List Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up:A linked list can be reversed either iteratively or recursively. Could you implement both? My Solution:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def reverseList(self, head): """ :type head: ListNode :rtype: L.. 더보기
Remove Nth Node From End of List Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n will always be valid.Follow up:Could you do this in one pass? My solution:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x.. 더보기