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         }

  • 相关阅读:
    Sum Root to Leaf Numbers 解答
    459. Repeated Substring Pattern
    71. Simplify Path
    89. Gray Code
    73. Set Matrix Zeroes
    297. Serialize and Deserialize Binary Tree
    449. Serialize and Deserialize BST
    451. Sort Characters By Frequency
    165. Compare Version Numbers
    447. Number of Boomerangs
  • 原文地址:https://www.cnblogs.com/ly7576/p/6253861.html
Copyright © 2011-2022 走看看