Leetcode 1 two sum 难度:0

https://leetcode.com/problems/two-sum/

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> index(2,0);
        vector<int> n2;
        int numsSize = nums.size();
        int f = -1;
        for(int i = 0;i < numsSize;i++){
            n2.push_back(nums[i]);
            if(nums[i] == target/2 && target%2 ==0){
                if(f != -1){
                    index[0] = min(f,i + 1);
                    index[1] = max(f,i + 1);
                    return index;
                }
                else f = i + 1;
            }
        }
        sort(nums.begin(),nums.end());
        for(int i = 0;i < numsSize;i++){
            int j = lower_bound(nums.begin(),nums.end(),target - nums[i]) - nums.begin();
            int a,b;
            if(nums[i] + nums[j] == target && j < numsSize){
                for(int k = 0;k < numsSize;k++){
                    if(nums[i] == n2[k]) a = k + 1;
                    if(nums[j] == n2[k]) b = k + 1;
                }
                index[0] = min(a,b);
                index[1] = max(a,b);
            }
        }
        return index;
    }
};

  

 

时间: 2024-09-16 09:42:51

Leetcode 1 two sum 难度:0的相关文章

LeetCode --- 1. Two Sum

题目链接:Two Sum Given an array of integers, 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 must be less than index2. Plea

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

【LeetCode】Path Sum

题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 解题分析: 每次只能向下或者向

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or r

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

[leetCode][013] Two Sum 2

题目: 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 inde