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 ..
더보기
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..
더보기
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..
더보기