代码随想录刷题——day6

64 阅读3分钟

day5是预定的休息。。。 终于是回到家了。今天把落下的两天的刷题给补上。

春运期间的高铁真的坐的我头疼,努力补上进度。

242.有效的字母异位词

class Solution {
        public boolean isAnagram(String s, String t) {
            Map<Character,Integer> mapS=new HashMap<>();
            Map<Character,Integer> mapT=new HashMap<>();
            if(s.length()!=t.length())
                return false;
            int len= s.length();
            for(int i=0;i<len;i++){
                char tempS=s.charAt(i);
                char tempT=t.charAt(i);
                if(mapS.containsKey(tempS)){
                    int countS=mapS.get(tempS)+1;
                    mapS.put(tempS,countS);
                }
                else{
                    mapS.put(tempS,1);
                }
                if(mapT.containsKey(tempT)){
                    int countT=mapT.get(tempT)+1;
                    mapT.put(tempT,countT);
                }
                else{
                    mapT.put(tempT,1);
                }
            }
    
            for(int j=0;j<len;j++){
                char tempChar=s.charAt(j);
                if(!mapT.containsKey(tempChar) || mapS.get(tempChar)-mapT.get(tempChar)!=0)
                    return false;
    
            }
            return true;
    
        }
    
    }

349. 两个数组的交集

 class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            //因为题目说 0 <= nums1[i], nums2[i] <= 1000 这个条件
            //所以设置check数字,0~1000中 谁出现过,就将其作为下标,对应的数置1
            int[] check=new int[1001];
            List<Integer> result=new ArrayList<>();
            for(int i=0;i<nums1.length;i++){
                check[nums1[i]]=1;
            }
            for(int i=0;i<nums2.length;i++){
                int count=0;
                if(check[nums2[i]]==1){
                    result.add(nums2[i]);
                    //因为相交的数只用输出一次,所以nums2一旦已经加入到result中后,就必须要将check中对应的置0
                    check[nums2[i]]=0;
                }
            }
            return result.stream().mapToInt(Integer::intValue).toArray();
        }
    }

202. 快乐数

这道题其实给我的感觉反而有点像链表中找环的题。

「快乐数」 定义为:

  • 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
  • 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
  • 如果这个过程 结果为 1,那么这个数就是快乐数。

那么就类似于:

  • 每次计算出来的快乐数,用集合存储起来。

    • 一旦计算的下一个数是已经出现过的,那么它一定是“有环”的,因为它此前已经出现过一次,这个环绕了多大我们不在乎,但是它一定是存在于这个环圈中的一部分了。
  • 一旦计算出来结果为1,那就有点类似于链表遍历到最后,为null。

class Solution {
        public boolean isHappy(int n) {
            if(n==1){
                return true;
            }
            //使用set将出现过的数存储起来
            Set<Integer> set=new HashSet<>();
            set.add(n);
            int num=calcHappyNum(n);
            //循环:如果当前计算出来的数尚未出现过,进入循环
            while(!set.contains(num)){
                //根据题意,如果计算结果是1,是快乐数,返回true
                if(num==1){
                    return true;
                }
                //否则将其加入到set集合中
                set.add(num);
                num=calcHappyNum(num);
            }
            return false;
    
        }
        public int calcHappyNum(int n){
            int num=0;
            while(n!=0){
                num+=(n%10)*(n%10);
                n=n/10;
            }
            return num;
        }
    }

1. 两数之和

  class Solution {
        public int[] twoSum(int[] nums, int target) {
            //用于记录最终的答案
            int[] result=new int[2];
            //创建一个map用于记录每个元素对应的下标,由于可能有相同元素,故使用ArrayList记录下标
            Map<Integer,ArrayList<Integer>> map= new HashMap<>();
            for(int i=0;i<nums.length;i++){
                int num = nums[i];
                if (!map.containsKey(num)) {
                    map.put(num, new ArrayList<>());
                }
                map.get(num).add(i);
            }
            //将Nums数组升序排列
            Arrays.sort(nums);
            for(int i=0;i<nums.length;i++){
                //searchedNum指的是在nums数组中看是否能够找到和nums[i]相加等于target的数
                int searchedNum=target-nums[i];
                //如果两个数应当是相同的
                if(searchedNum==nums[i]){
                    //看map中此数对应的下标是否存了2次(即是否确实数组中有两个这个数)
                    if(map.get(nums[i]).size()>1){
                        //因为题目说了 ,只会存在一个有效答案,所以直接赋值就行。
                        ArrayList<Integer> tempArray=map.get(nums[i]);
                        result[0]=tempArray.get(0);
                        result[1]=tempArray.get(1);
                        return result;
                    }
                }
                else{
                    //如果二分查找 在nums中找到了
                    if(binarySearch(nums,searchedNum)){
                        result[0]=map.get(nums[i]).get(0);
                        result[1]=map.get(searchedNum).get(0);
                        return result;
                    }
                }
            }
            return result;
    
        }
        public boolean binarySearch(int[] nums,int target){
            int left=0;
            int right=nums.length-1;
            while(left<=right){
                int mid=left+(right-left)/2;
                if(target>nums[mid]){
                    left=mid+1;
                }
                if(target<nums[mid]){
                    right=mid-1;
                }
                if(target==nums[mid]){
                    return true;
                }
            }
            return false;
        }
    }