前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >No.001 Two Sum

No.001 Two Sum

作者头像
mukekeheart
发布2018-02-27 12:18:36
3540
发布2018-02-27 12:18:36
举报
文章被收录于专栏:mukekeheart的iOS之旅

Two Sum

  • Total Accepted: 262258
  • Total Submissions: 1048169
  • Difficulty: Easy

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:

代码语言:javascript
复制
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

暴力解法,没有优化思路。
代码语言:javascript
复制
 1 public class Num1 {
 2     public int[] twoSum(int[] nums, int target) {
 3         int [] res = new int [2] ;
 4         for(int i = 0 ; i < nums.length ; i++){
 5             if(nums[i] > target){
 6                 continue ;
 7             }else{
 8                 res[0] = i ;
 9             }
10             for(int j = i+1 ; j < nums.length ; j++){
11                 if((nums[i]+nums[j]) == target){
12                     res[1] = j ;
13                     return res ;
14                 }else{
15                     continue ;
16                 }
17             }
18         }
19         
20         return res ;
21     }
22 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-07-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Two Sum
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档