• LeetCode Online Judge 1. Two Sum


    刷个题,击败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:

     1 public class Solution {
     2     public int[] TwoSum(int[] nums, int target) {
     3             int[] result=null;
     4             int i;
     5             for (i = 0; i < nums.Length; i++)
     6             {
     7                 int j;
     8                 for (j = 0; j < nums.Length; j++)
     9                 {
    10                     if(i != j)
    11                     {
    12                         if (nums[i] + nums[j] == target)
    13                         {
    14                             result = new int[] {j, i};
    15                         }
    16                     }
    17                 }
    18             }
    19            return result;
    20     }
    21 }

    修改一下:

    3.63%了...

     1     public int[] TwoSum(int[] nums, int target) {
     2             int[] result=null;
     3             int i;
     4             bool finishLoop = false;
     5             for (i = 0; i < nums.Length; i++)
     6             {
     7                 int j;
     8                 for (j = 0; j < nums.Length; j++)
     9                 {
    10                     if(i != j)
    11                     {
    12                         if (nums[i] + nums[j] == target)
    13                         {
    14                             result = new int[] {i, j};
    15                             finishLoop = true;
    16                             break;
    17                         }
    18                     }
    19                 }
    20                 if(finishLoop == true)
    21                     break;
    22             }
    23            return result;
    24     }

     再改一下:

    7.26%

     1             int[] result = null;
     2             int i;
     3             bool finishLoop = false;
     4             for (i = 0; i < nums.Length; i++)
     5             {
     6                 int j;
     7                 for (j = 0; j < nums.Length; j++)
     8                 {
     9                     if (i != j)
    10                     {
    11                         if (nums[i] + nums[j] == target)
    12                         {
    13                             result = new[] { i, j };
    14                             finishLoop = true;
    15                             break;
    16                         }
    17                     }
    18                 }
    19                 if (finishLoop)
    20                     break;
    21             }
    22             return result;

    试试两个continue:

     1     public int[] TwoSum(int[] nums, int target) {
     2             int[] result = null;
     3             int i;
     4             bool finishLoop = false;
     5             for (i = 0; i < nums.Length; i++)
     6             {
     7                 int j;
     8                 for (j = 0; j < nums.Length; j++)
     9                 {
    10                     if (i == j) continue;
    11                     if (nums[i] + nums[j] != target) continue;
    12                         result = new[] { i, j };
    13                         finishLoop = true;
    14 
    15                 }
    16                 if (finishLoop)
    17                     break;
    18             }
    19             return result;
    20         }

    试试一个continue:

     1     public int[] TwoSum(int[] nums, int target) {
     2             int[] result = null;
     3             int i;
     4             bool finishLoop = false;
     5             for (i = 0; i < nums.Length; i++)
     6             {
     7                 int j;
     8                 for (j = 0; j < nums.Length; j++)
     9                 {
    10                     if (i == j) continue;
    11                     if (nums[i] + nums[j] == target)
    12                     {
    13                         result = new[] { i, j };
    14                         finishLoop = true;
    15                         break;                        
    16                     }
    17 
    18                 }
    19                 if (finishLoop)
    20                     break;
    21             }
    22             return result;
    23         }

  • 相关阅读:
    JAVA中获取当前系统时间
    struts2文件下载及 <param name="inputName">inputStream</param>的理解
    struts2文件下载,动态设置资源地址
    IE8上传文件时文件本地路径变成"C:fakepath"的问题
    Java设置session超时(失效)的三种方式
    学一点Git--20分钟git快速上手
    关于服务器响应,浏览器请求的理解以及javaWeb项目的编码问题
    GBK、GB2312、iso-8859-1之间的区别
    mysql的多表查询
    mysql错误:“ Every derived table must have its own alias”(每个派生出来的表都必须有一个自己的别名)
  • 原文地址:https://www.cnblogs.com/ly7576/p/6253861.html
Copyright © 2020-2023  润新知