主页

16. 3Sum Closest

Promble link: https://leetcode.com/problems/3sum-closest/ Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Input: nums = [-1,2,1,-4], target = 1 Output: 2 Exp...

阅读更多

15. 3Sum

Promble link: https://leetcode.com/problems/3sum/ Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanatio...

阅读更多

1. Two Sum

Promble link: https://leetcode.com/problems/two-sum/description/ Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Input: ...

阅读更多

69. Sqrt(x)

Promble Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator. For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Input: x = 4 Output: 2 Explanation: The square root of 4 i...

阅读更多

35. Search Insert Position

Promble Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. Input: nums = [1,3,5,6], target = 5 Output: 2 Input: nums = [1,3,5,6], target = 2 Output: 1 In...

阅读更多

34. Find First and Last Position of Element in Sorted Array

Promble Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Input: nums = [5,7,7,8,8,10], targe...

阅读更多

119. Pascal's Triangle II

Promble Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal’s triangle. In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown: Input: rowIndex = 3 Output: [1,3,3,1] Input: rowIndex = 0 Output: [1] Input: rowIndex = 1 Output: [1,1] Approach Code class Solution: ...

阅读更多

746. Min Cost Climbing Stairs

Promble You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0, or the step with index 1. Return the minimum cost to reach the top of the floor. Input: cost = [10,15,20] Output: 15 Explanation: You ...

阅读更多