刷个题,击败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. 一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库

    说起Oracle数据库,以前没用过Oracle不知道,但是这1年用Oracle后,发现真的是想狂吐槽,特别是那个.NET驱动和链接字符串,特别奇葩.总归是和其他数据库不一样,标新立异,不知道为何.另外 ...

  2. jQuery学习之路(3)- 事件

    ▓▓▓▓▓▓ 大致介绍 jQuery增加了并扩展了基本的事件处理机制,不但提供了更加优雅的事件处理语法,而且极大地增强了事件处理能力 ▓▓▓▓▓▓ jQuery中的事件 ▓▓▓▓▓▓ 加载DOM 在j ...

  3. .net 分布式架构之分布式缓存中间件

    开源git地址: http://git.oschina.net/chejiangyi/XXF.BaseService.DistributedCache 分布式缓存中间件  方便实现缓存的分布式,集群, ...

  4. BPM配置故事之案例14-数据字典与数据联动

    小明遇到了点麻烦,他昨天又收到了行政主管发来的邮件,要求把出差申请单改由H3 BPM进行,表单如下 行政主管的出差申请表 小明对表单进行了调整,设计出了一份适合在系统中使用的表单,但在"出差 ...

  5. Spring MVC类型转换器

    类型转换器引入 为什么页面上输入"12",可以赋值给Handler方法对应的参数?这是因为框架内部帮我们做了类型转换的工作.将String转换成int 但默认类型转换器并不是可以将 ...

  6. 运用Mono.Cecil 反射读取.NET程序集元数据

    CLR自带的反射机智和API可以很轻松的读取.NET程序集信息,但是不能对程序集进行修改.CLR提供的是只读的API,但是开源项目Mono.Cecil不仅仅可以读取.NET程序集的元数据,还可以进行修 ...

  7. 页面与ViewModel(下)

    在上一篇博客中,笔者分享了一些从页面整体的角度对页面与ViewModel的思考.在本文中笔者希望从相对细节的角度分享一些对页面与ViewModel的思考. 比如,当我们在更新View Model中的绑 ...

  8. Entity Framework 6 Recipes 2nd Edition(12-7)译 -> 设定默认值

    12-7. 设定默认值 问题 在把一个实体保存到数据库之前,设置该实体属性的默认值 解决方案 假设你有一个如Figure 12-9所示的表, 它保存采购订单(purchase order). 主键Pu ...

  9. mysql 列名中 包含斜杠或者空格的处理方式

    今天客户那边遇到了一个比较奇葩的问题跑来问我,这个问题比较冷门,所以特别记录下. 问题描述 数据库的字段存在斜杠或者空格的时候,怎么用sql进行insert或者select操作. 问题解答 对于这种特 ...

  10. 给 Android 研发的一些的建议

    作为应用程序开发人员,我们需要注意在开发应用程序时的一些问题. 这些问题的安全级别是取决于应用程序的类型和使用域. 在这里列举了一些我们在开发中需要注意的一些问题: 开发日志输出相关: 1. 不要在 ...