20210221-leecode中Map集合代码笔记

这篇博客介绍了如何使用Java中的哈希表解决数组相关问题,如两数之和、查找只出现一次的数字和判断快乐数。通过哈希表实现高效查找和避免重复,讲解了containsKey、put、get等方法的用法,并展示了具体代码实现。
摘要由CSDN通过智能技术生成

代码记录:

关键点:

1两数之和
Map<Integer, Integer> map = new HashMap<>();    //新建map类型的对象
map.containsKey()                                                    //containsKey判断是否包含指定的键名    转载:https://www.cnblogs.com/originate918/p/6674511.html
map.put()                    
map.get()

136
map.keySet()       //返回所有键名列表     在题中是 返回[1,2,4]  这个
//getOrDefault(Object key, V defaultValue) //意思就是当Map集合中有这个key时,就使用这个key对应的value值,如果没有就使用默认值

202,217
set,contain()     //set,contain(n)   集合中是否包含 n
set.add()          //在集合里增加元素

 

1两数之和

package test;

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



public class num1 {
	public static void main(String[] args){
		Solution a=new Solution();
		int[] nums=new int[]{2,7,11,2};
		int target=9;
		int[] b=a.twoSum(nums, target);
		for(int t=0;t<b.length;t++){
			System.out.println(b[t]);
		}
//		for(int m:b){
//			System.out.println(m);
//		}
	}
}


class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = nums.length -1; i >= 0; i--) {
            int firstNum = target - nums[i];
//            System.out.println(firstNum);
            if (map.containsKey(firstNum)) {
            	
                 int[] c=new int[]{i, map.get(firstNum)};
                 for(int n:c){
                	 System.out.println(n);
                 }
                
                return c;
            } else {
                map.put(nums[i], i);
//                System.out.println(map.put(nums[i], i));
                
            }
        }

        throw new IllegalArgumentException();
    }
}

 

 

 

136只出过一次数字

package test;

import java.util.HashMap;

public class Num136 {
	public static void main(String[] args){
		Solution136 a=new Solution136();
		int[] nums=new int[]{4,1,2,1,2};

		int b=a.singleNumber(nums);
		System.out.println(b);
//		for(int t=0;t<b.length;t++){
//			System.out.println(b[t]);
//		}

	}
}

class Solution136 {
    public int singleNumber(int[] nums) {
        //特殊情况
        if (nums.length == 1) {
            return nums[0];
        }
        //HashMap
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        //将其存入哈希表中,含义为,若该元素不存在则存入表中,并计数为1,若已经存在获取次数并加1.
        for (int x : nums) {
//        	System.out.println(x);
//        	System.out.println(map.getOrDefault(x,0)+1);
            map.put(x , map.getOrDefault(x,0) + 1);
        }
        //遍历出出现次数为1的情况
        for (int y : map.keySet()) {
        	System.out.println(y);
        	System.out.println(map.keySet());
            if(map.get(y) == 1){
                return y;
            }
        }    
        return 0;
    }
}

//getOrDefault(Object key, V defaultValue)
//意思就是当Map集合中有这个key时,就使用这个key对应的value值,如果没有就使用默认值defaultValue

//作者:yuan-chu-de-suan-fa-xiao-wu
//链接:https://leetcode-cn.com/problems/single-number/solution/dong-hua-dong-tu-yi-ding-hui-by-yuan-chu-vs4p/
//来源:力扣(LeetCode)

 

202快乐数

代码来源:

https://leetcode-cn.com/problems/happy-number/solution/kuai-le-shu-by-leetcode-solution/

class Solution {
    private int getNext(int n) {
        int totalSum = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            totalSum += d * d;
        }
        return totalSum;
    }

    public boolean isHappy(int n) {
        Set<Integer> seen = new HashSet<>();
        while (n != 1 && !seen.contains(n)) {
            seen.add(n);
            n = getNext(n);
        }
        return n == 1;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/happy-number/solution/kuai-le-shu-by-leetcode-solution/
来源:力扣(LeetCode)

 

 

217. 存在重复元素

方法二 使用 set
遍历数组,数字放到 set 中。如果数字已经存在于 set 中,直接返回 true。如果成功遍历完数组,则表示没有重复元素,返回 false。

Java

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num: nums) {
            if (set.contains(num)) {
                return true;
            }
            set.add(num);
        }
        return false;
    }
}

作者:sweetiee
链接:https://leetcode-cn.com/problems/contains-duplicate/solution/chao-xiang-xi-kuai-lai-miao-dong-ru-he-p-sf6e/
来源:力扣(LeetCode)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值