1. Given an array of integers, return indices of the two numbers such that they add up to a specific target.

问题:

1.数组是否有序

2. 数组排序

  1. // sorting array
  2. Arrays.sort(iArr)

方法1:O(n^2)

  1. public class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. int[] result = new int[2];
  4. for (int i = 0; i < nums.length; i++){
  5. for (int j = i+1; j < nums.length; j++){
  6. if(nums[i] + nums[j] == target){
  7. result [0] = i;
  8. result [1] = j;
  9. }
  10. }
  11. }
  12. return result;
  13. }
  14. }

2. Two sum II  Input array is sorted (HashMap, 无相同元素

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

 

  1. public class Solution {
  2. public int[] twoSum(int[] nums, int target) {
    int [] result = new int[2];
  3. HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
  4. for(int i = 0; i < nums.length; i++){
  5. hm.put(nums[i], i);
  6. }
  7. for(int i = 0; i < nums.length; i++){
  8. if(hm.containsKey(target-nums[i]) && target != 2 * nums[i]){
  9. result[0] = i;
  10. result[1] = hm.get(target-nums[i]);
  11. break;
  12. }
  13. }
  14. return result;
  15. }
    }

3. Two sum III

1. Two Sum I & II & III的更多相关文章

  1. Leetcode 39 40 216 Combination Sum I II III

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  2. Path Sum I && II & III

    Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ad ...

  3. Two Sum I & II & III & IV

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

  4. combination sum(I, II, III, IV)

    II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...

  5. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  6. Leetcode 137. Single Number I/II/III

    Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...

  7. 买卖股票的最佳时机I II III IV

    I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...

  8. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  9. Two Sum I & II

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

随机推荐

  1. 如何用Java代码列出一个目录下所有的文件?

    目录文件夹 File file=new File("H:\\"); for(File temp:file.listFiles()){//Java5的新特性之一就是增强的for循环. ...

  2. vs2010取消显示所有文件 崩溃

    这属于vs2010的bug,以下是微软官方对些问题的回应: i am still investigating the problem, but looks like the issue is caus ...

  3. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  4. [TCPIP] IP路由表及选路 Note

    TCP/IP IP路由表及选路 1.路由表信息 路由表一般包含信息:目的IP地址.下一站路由器的IP地址.标志. 为数据报传送指定的一个网络接口. 查看路由表信息mac-abeen:~ abeen$ ...

  5. 移动端重构系列-移动端html页面优化

    对于访问量大的网站来说,前端的优化是必须的,即使是优化1KB的大小对其影响也很大,下面来看看来自ISUX的米随随讲讲移动手机平台的HTML5前端优化,或许对你有帮助和启发. 概述 1. PC优化手段在 ...

  6. UUID工具

    package com.tech.jin.util; import java.util.UUID; public class UuidUtil { public static String get32 ...

  7. Python的SublimeText开发环境配置

    1.完成Python的默认安装之后要设置环境变量,系统环境变量Path中加入Python的安装目录";C:/Python27" 2.配置Python编译环境,修改[C:\Progr ...

  8. 解决yum update失败

    1.yum update .yum clean.yum install操作提示 Loaded plugins: fastestmirror, langpacks Loading mirror spee ...

  9. hdu-5525 Product(费马小定理)

    题目来源:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=644&pid=1003 前面用奇偶性约掉2, ...

  10. 回调函数及数组中sort()方法实现排序的原理

    1.回调函数:把一个方法A当一个参数值传递到另外一个函数B中,在B执行的过程当中我们随时根据需求让A方法执行:   什么是回调 :它是异步编程基本的方法,需要异步处理的时候一般采用后续传递的方式,将后 ...