求两数之和的对应下标

package com.mjm.demo.codePractice;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * the subScript about Sum of two nums
 * 两数之和,求下标
 * num=【2,7,11,15】,target=9
 * 因为num【0】+num【1】=9 ,所以返回【0,1】
 * 数组中同一个元素不能使用两遍
 */
public class SumOfTwoNums {
    public static void main(String[] args) {
        int[] nums = new int[]{2, 7, 11, 1, 7, 3, 6};
        int target = 9;
        new MyMap().towSum(nums, target);
        System.out.println("------");
        new MyMap().towSumNew(nums, target);
        System.out.println("------");
        allPrint(nums, target);
    }

    static void allPrint(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            int num = target - nums[i];
            if (num > 0) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] == num) {
                        System.out.println(Arrays.toString(new int[]{i, j}));
                    }
                }
            }
        }
    }

    static void putMap(int[] nums, int target) {
        MyHashMap<Integer, Integer> myHashMap = new MyHashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num = target - nums[i];
            if (num > 0) {
                //i+1不拿重复的起始数
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] == num) {
                        myHashMap.put(i, j);
                    }
                }
            }
        }
        myPrint(myHashMap);

    }

    static void putMap2(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>(16);
        for (int i = 0; i < nums.length; i++) {
            int num = target - nums[i];
            if (num > 0) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] == num) {
                        map.put(i, j);
                    }
                }
            }
        }
        myPrint(map);

    }

    /**
     * map如何取最新key进行覆盖
     * hash(key)判断key是否重复
     */
    static class MyMap {
        public void towSum(int[] nums, int target) {
            putMap(nums, target);
        }

        public void towSumNew(int[] nums, int target) {
            putMap2(nums, target);
        }
    }

    /**
     * 遍历打印
     */
    static void myPrint(HashMap<Integer, Integer> myHashMap) {
        for (Map.Entry<Integer, Integer> entry : myHashMap.entrySet()) {
            String str = "[" + entry.getKey() + "," + entry.getValue() + "]";
            ArrayList<String> list = new ArrayList<>();
            list.add(str);
            for (String s : list) {
                System.out.println(s);
            }
        }
    }
}

/**
 * 重写hashMap的put方法
 * 如果key重复不保存
 */
class MyHashMap<K, V> extends HashMap<K, V> {
    @Override
    public V put(K key, V value) {
        if (!containsKey(key)) {
            return super.put(key, value);
        }
        return null;
    }
}
输出
[0,1]
[5,6]
------
[0,4]
[5,6]
------
[0, 1]
[0, 4]
[5, 6]

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值