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:
public class Solution {
public int[] TwoSum(int[] nums, int target) {
int[] result=null;
int i;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if(i != j)
{
if (nums[i] + nums[j] == target)
{
result = new int[] {j, i};
}
}
}
}
return result;
}
}
修改一下:
3.63%了...
public int[] TwoSum(int[] nums, int target) {
int[] result=null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if(i != j)
{
if (nums[i] + nums[j] == target)
{
result = new int[] {i, j};
finishLoop = true;
break;
}
}
}
if(finishLoop == true)
break;
}
return result;
}
再改一下:
7.26%
int[] result = null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if (i != j)
{
if (nums[i] + nums[j] == target)
{
result = new[] { i, j };
finishLoop = true;
break;
}
}
}
if (finishLoop)
break;
}
return result;
试试两个continue:
public int[] TwoSum(int[] nums, int target) {
int[] result = null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if (i == j) continue;
if (nums[i] + nums[j] != target) continue;
result = new[] { i, j };
finishLoop = true; }
if (finishLoop)
break;
}
return result;
}
试试一个continue:
public int[] TwoSum(int[] nums, int target) {
int[] result = null;
int i;
bool finishLoop = false;
for (i = ; i < nums.Length; i++)
{
int j;
for (j = ; j < nums.Length; j++)
{
if (i == j) continue;
if (nums[i] + nums[j] == target)
{
result = new[] { i, j };
finishLoop = true;
break;
} }
if (finishLoop)
break;
}
return result;
}
LeetCode Online Judge 1. Two Sum的更多相关文章
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [leetcode]364. Nested List Weight Sum II嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- leetcode@ [327] Count of Range Sum (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- leetcode–Binary Tree Maximum Path Sum
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...
随机推荐
- ASP.NET Core的路由[5]:内联路由约束的检验
当某个请求能够被成功路由的前提是它满足某个Route对象设置的路由规则,具体来说,当前请求的URL不仅需要满足路由模板体现的路径模式,请求还需要满足Route对象的所有约束.路由系统采用IRouteC ...
- [C#] 走进异步编程的世界 - 开始接触 async/await
走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async/await,但在控制台输出示例时经常会采用 C# 6.0 的 $&qu ...
- javascript表单的Ajax 提交插件的使用
Ajax 提交插件 form.js 表单的下载地址:官方网站:http://malsup.com/jquery/form/ form.js 插件有两个核心方法:ajaxForm()和ajaxSubmi ...
- CSS垂直居中的11种实现方式
今天是邓呆呆球衣退役的日子,在这个颇具纪念意义的日子里我写下自己的第一篇博客,还望前辈们多多提携,多多指教! 接下来,就进入正文,来说说关于垂直居中的事.(以下这11种垂直居中的实现方式均为笔者在日常 ...
- C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置
用于永久化对象,什么程序都行,依赖NewtonSoft.用于json序列化和反序列化. using Newtonsoft.Json; using System; using System.Collec ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- Android 在Android代码中执行命令行
1.路径最好不要是自己拼写的路径/mnt/shell/emulated/0/wifidog.conf 最好是通过方法获取的路径,不然可能导致命令无效 (挂载点的原因) public static f ...
- IT雇员及外包商选择:人品第一
最近,苹果iOS操作系统和智能手机爆出了一个奇葩故障,在播放特定一段五秒钟的视频时能导致手机死机.唯一的解决办法是按住电源键和Home按键进行手机的重启. 第十八届中国国际高新技术成果交易会在深圳举办 ...
- (一)Spark简介-Java&Python版Spark
Spark简介 视频教程: 1.优酷 2.YouTube 简介: Spark是加州大学伯克利分校AMP实验室,开发的通用内存并行计算框架.Spark在2013年6月进入Apache成为孵化项目,8个月 ...