刷个题,击败0.17%...

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].

code:

 public class Solution {
public int[] TwoSum(int[] nums, int target) {
int[] result=null;
int i;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if(i != j)
{
if (nums[i] + nums[j] == target)
{
result = new int[] {j, i};
}
}
}
}
return result;
}
}

修改一下:

3.63%了...

     public int[] TwoSum(int[] nums, int target) {
int[] result=null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if(i != j)
{
if (nums[i] + nums[j] == target)
{
result = new int[] {i, j};
finishLoop = true;
break;
}
}
}
if(finishLoop == true)
break;
}
return result;
}

再改一下:

7.26%

             int[] result = null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if (i != j)
{
if (nums[i] + nums[j] == target)
{
result = new[] { i, j };
finishLoop = true;
break;
}
}
}
if (finishLoop)
break;
}
return result;

试试两个continue:

     public int[] TwoSum(int[] nums, int target) {
int[] result = null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if (i == j) continue;
if (nums[i] + nums[j] != target) continue;
result = new[] { i, j };
finishLoop = true; }
if (finishLoop)
break;
}
return result;
}

试试一个continue:

     public int[] TwoSum(int[] nums, int target) {
int[] result = null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if (i == j) continue;
if (nums[i] + nums[j] == target)
{
result = new[] { i, j };
finishLoop = true;
break;
} }
if (finishLoop)
break;
}
return result;
}

LeetCode Online Judge 1. Two Sum的更多相关文章

  1. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  2. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  3. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  4. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  5. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  6. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  7. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  8. leetcode@ [327] Count of Range Sum (Binary Search)

    https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...

  9. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

随机推荐

  1. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

    Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...

  2. 免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)

    很多的软件项目几乎都包含着对文档的操作,前面已经介绍过两款操作文档的组件,现在介绍一款文档操作的组件NPOI. NPOI可以生成没有安装在您的服务器上的Microsoft Office套件的Excel ...

  3. spring无法读取properties文件数据

    只讲述异常点,关于怎么配置文件,这里不做说明.   1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-cont ...

  4. TFS 2015 敏捷开发实践 – 看板的使用

    看板在现代应用开发过程中使用非常广泛,不管是使用传统的瀑布式开发还是敏捷开发,都可以使用看板管理.因为看板拥有简单的管理方法,直观的显示方式,所以很多软件开发团队选择使用看板进行软件开发管理.本文不在 ...

  5. ASP.NET MVC 5 系列 学习笔记 目录 (持续更新...)

    前言: 记得当初培训的时候,学习的还是ASP.NET,现在回想一下,图片水印.统计人数.过滤器....HttpHandler是多么的经典! 不过后来接触到了MVC,便立马爱上了它.Model-View ...

  6. FineReport如何用JDBC连接阿里云ADS数据库

    在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...

  7. 【python之路3】if 语句

    1.if语句用法(if....else....) #!/usr/bin/env python # -*- coding:utf-8 -*- my_name = raw_input("plea ...

  8. 第14章 Linux启动管理(1)_系统运行级别

    1. CentOS 6.x 启动管理 (1)系统运行级别 ①运行级别 运行级别 含义 0 关机 1 单用户模式,可以想象为Windows的安全模式,主要用于系统修复.(但不是Linux的安全模式) 2 ...

  9. 【腾讯Bugly干货分享】动态链接库加载原理及HotFix方案介绍

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57bec216d81f2415515d3e9c 作者:陈昱全 引言 随着项目中动 ...

  10. 【Knockout.js 学习体验之旅】(3)模板绑定

    本文是[Knockout.js 学习体验之旅]系列文章的第3篇,所有demo均基于目前knockout.js的最新版本(3.4.0).小茄才识有限,文中若有不当之处,还望大家指出. 目录: [Knoc ...