zoukankan      html  css  js  c++  java
  • 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         }

  • 相关阅读:
    Servlet的生命周期
    Servlet中的请求与响应
    Servlet中的相关的配置文件信息:
    转发与重定向的区别,笔记无法转过来,所以直接放链接了,可以直接查看
    JSP入门中的一些案例代码:
    一些已经有些模糊的小知识(一)
    JSP入门五之request,out,response对象的应用
    JSP入门四
    JSP入门三 不知道如何将笔记同步过来只能这样了
    来自(乐唯科技)的面试问题..
  • 原文地址:https://www.cnblogs.com/ly7576/p/6253861.html
Copyright © 2011-2022 走看看