The sum of two Numbers And The sum of three number

The sum of two Numbers

1、n*(n-1)/2 Method to iterate through,Call each other  Time complexity:O(n^2)  Spatial complexityO(1)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        
        int s = nums.length - 1;
  
        for (int i = 0; i < s ; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if (target == nums[i] + nums[j]) {
                    return new int[] {i , j};
                }
            }
        }

        throw new IllegalArgumentException("no solution");

    }
}

2、The hash table holds all the information,Time complexityO(n)Spatial complexityO(n)

    // 定义一个hash表
    HashMap<Integer, Integer> map = new HashMap();

    // 1. 遍历数组,将数据全部保存入hash表
    for (int i = 0, l = nums.length; i < l ; i++){

      map.put(nums[i], i);

    }

    // 2. 再次遍历数组,寻找每个数对应的那个数是否存在
    for (int i = 0, l = nums.length; i < l ; i++) {
      int thatNum = target - nums[i];

      // 如果那个数存在,并且不是当前数自身,就直接返回结果
      if (map.containsKey(thatNum) && map.get(thatNum) != i) {
        return new int[] {i, map.get(thatNum)};
      }
    }



    throw new IllegalArgumentException("no solution");

3、A traversal , Time complexityO(n)Spatial complexityO(n)  faster

    // 定义一个hash表
    HashMap<Integer, Integer> map = new HashMap();

    // 1. 遍历数组,将数据全部保存入hash表
    for (int i = 0, l = nums.length; i < l ; i++){


      int thatNum = target - nums[i];

      if (map.containsKey(thatNum) && map.get(thatNum) != i) {
        return new int[] {map.get(thatNum),i};
      }

      map.put(nums[i], i);   //O(1)

    }




    throw new IllegalArgumentException("no solution");

If you want to calculate time ,This can be done in the following ways

  

 The sum of three number

Double pointer

package ThreeNumbers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThreeWay {


    public static void main(String[] args) {

      int[] nums = {0,0,0};

      System.out.println(gather(nums));

    }


    public static List<List<Integer>> gather(int[] nums) {



      Arrays.sort(nums);

      List<List<Integer>> bigList = new ArrayList<List<Integer>>();

      int numsLength = nums.length;

      for (int i = 0 , l = numsLength; i < l; i++ ) {

        int j = i + 1;
        int k = l - 1;

        if (nums[i] > 0) {
          break;
        }

        if (i > 0 && nums[i - 1] == nums[i]) {
          continue;
        }

        while (j < k) {

          int thatNum = nums[i] + nums[j] + nums[k];

          if (thatNum == 0) {


            bigList.add(Arrays.asList(nums[i], nums[j], nums[k]));

            j++;
            k--;

            while (j < k && nums[j-1] == nums[j]) j++;
            while (j < k && nums[k+1] == nums[k]) k--;

          }else if (thatNum < 0) {
            j++;
          }else if (thatNum > 0) {
            k--;
          }


        }

      }




      return bigList;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值