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         }

  • 相关阅读:
    测试用例的优先级的概念
    Day02.测试用例和测试方法
    day01.测试理论
    开发python 面试题
    4.路径页面接口开发
    ps命令没有显示路径找到命令真实路径
    Linux软链接和硬链接
    Linux文件元数据和节点表结构
    jinjia2语言
    Ansible之YAML语言
  • 原文地址:https://www.cnblogs.com/ly7576/p/6253861.html
Copyright © 2011-2022 走看看