leetcode167】的更多相关文章

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m…
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1…
public class Solution { public int[] TwoSum(int[] numbers, int target) { Dictionary<int, int> dic = new Dictionary<int, int>(); int count = numbers.Count(); List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int&g…
题目描述; 自己的提交:超时 class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: m,n = len(mat),len(mat[0]) res = 0 for i in range(m): for j in range(n): i_,j_ = i+res,j+res while i_ < m and j_ < n: pre = sum(sum(mat[x][j:j_])…
题目描述: 提交: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def getDecimalValue(self, head: ListNode) -> int: if not head: return 0 l = [] cur = head while cur: l.append(…
题目描述: 自己的提交: class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: l,h = len(str(low)),len(str(high)) res = [] def helper(num,length): if length == 0 and low <= num <= high: res.append(num) return if num % 10 == 9: return…
题目描述: 自己的提交:广度优先 O(mn*min(k,m+n)) class Solution: def shortestPath(self, grid, k: int) -> int: visited = {} queue = [[0,0,k-1]]if grid[0][0] == 1 and k > 0 else [[0,0,k]] if k == 0 and grid[0][0] == 1: return 0 m,n = len(grid),len(grid[0]) k = min(m…
题意:对于一个有序数组,输出和为target的两个元素的下标.题目保证仅有唯一解. 分析: 法一:二分.枚举第一个元素,二分找另一个元素,时间复杂度O(nlogn),非最优解. class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { int len = numbers.size(); vector<int> ans; for(int i = 0; i <…
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1…
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> ls = new LinkedList<>(); for (int i = 0; i < nums.length - 2; i++) { if (i == 0 || (i > 0 && nums[i] != n…