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         }

  • 相关阅读:
    Windows10切换其他用户身份运行程序
    管理Windows功能
    如何暂时锁定您的键盘
    判断远程计算机基于x64或x86处理器
    复制文件而不在命令行中覆盖它们
    解决IDEA Gradle工程控制台输出乱码
    jquery 选择器、属性和CSS、文档处理、筛选、事件、动画效果
    IDEA炫酷主题推荐!(转)
    Windows 查看端口占用进程并关闭(转)
    JVM(二)--运行时数据区
  • 原文地址:https://www.cnblogs.com/ly7576/p/6253861.html
Copyright © 2011-2022 走看看