Easy-题目68:1. Two Sum(增补1)
来源:互联网 发布:下载instagram软件下载 编辑:程序博客网 时间:2024/06/03 19:16
题目原文:
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
题目大意:
给出一个数组和一个目标值target,寻找两个下标i和j,使得nums[i]+nums[j]=target。
题目分析:
这道题先无脑水过去。复杂度O(n2)
源码:(language:java)
public class Solution { public int[] twoSum(int[] nums, int target) { for(int i = 0;i<nums.length;i++) { for(int j=i+1;j<nums.length;j++) { if(nums[i]+nums[j]==target) return new int[]{i,j}; } } return new int[]{-1,-1}; }}
成绩:
42ms,beats 19.97%,众数6ms,35.54%
Cmershen的碎碎念:
这道题不知道何时被改成Easy了,其实可以用HashMap做,应该能在线性时间解决。
0 0
- Easy-题目68:1. Two Sum(增补1)
- Easy-题目72:349. Intersection of Two Arrays(增补5)
- Easy-题目73:350. Intersection of Two Arrays II(增补6)
- 【Easy】1. Two Sum
- 1. Two Sum easy
- 1. Two Sum (Easy)
- 1.[easy] Two Sum
- #1 Two Sum(Easy)
- Two Sum(easy)
- Easy-题目69:342. Power of Four(增补2)
- Easy-题目70:344. Reverse String(增补3)
- Easy 1 Two Sum(1)
- 1. Two Sum [easy] (Python)
- 1.Two Sum 难度:easy
- leetcode 1. Two Sum [Easy]
- LeetCode 1. Two Sum (Easy)
- Leetcode #1 Two Sum (easy)
- Leetcode #1 - Two Sum - Easy
- ThreadPoolExecutor机制
- ThinkPHP(6)——模板技术
- 加载包含控件类的dll
- android-----Volley框架源码分析
- webpack 入门教程
- Easy-题目68:1. Two Sum(增补1)
- Mac OS 简易安装redis
- Easy-题目69:342. Power of Four(增补2)
- iOS之UIView的用法
- ActiveMQ源码解析(一):聊聊broker
- svn教程及下载地址
- hdu2955 robberies题解(01背包)
- $.post与$.ajax的用法区别
- U3D Distortion