代码随想录算法训练营第六天|242. 有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和

242. 有效的字母异位词

public static bool IsAnagram(string s, string t)
        {
            //method 1
            if (s.Length != t.Length) return false;
            Dictionary<char, int> dict1 = new Dictionary<char, int>();
            for (int i = 0; i < s.Length; i++)
            {
                if (!dict1.ContainsKey(s[i]))
                {
                    dict1.Add(s[i], 0);
                }
                else
                {
                    dict1[s[i]]++;
                }
            }
            Dictionary<char, int> dict2 = new Dictionary<char, int>();
            for (int i = 0; i < t.Length; i++)
            {
                if (!dict2.ContainsKey(t[i]))
                {
                    dict2.Add(t[i], 0);
                }
                else
                {
                    dict2[t[i]]++;
                }
            }
            foreach (KeyValuePair<char, int> pair in dict1)
            {
                if (!dict2.Contains(pair)) return false;
            }
            return true;

            //method 2
            if (s.Length != t.Length) return false;
            int[] data = new int[26];
            for (int i = 0; i < s.Length; i++)
            {
                data[s[i] - 'a']++;
                data[t[i] - 'a']--;
            }
            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] != 0) return false;
            }
            return true;
        }

349. 两个数组的交集

public static int[] Intersection(int[] nums1, int[] nums2)
        {
            //method1
            Dictionary<int,int> dict = new Dictionary<int,int>();
            for (int i = 0; i < nums1.Length; i++)
            {
                if (!dict.ContainsKey(nums1[i]))
                {
                    dict.Add(nums1[i], 0);
                }
                else
                {
                    dict[nums1[i]]++;
                }
            }
            Dictionary<int, int> dict2 = new Dictionary<int, int>();
            for (int i = 0; i < nums2.Length; i++)
            {
                if (!dict2.ContainsKey(nums2[i]))
                {
                    dict2.Add(nums2[i], 0);//The Add method throws an exception if the new key is already in the dictionary.
                }
                else
                {
                    dict2[nums2[i]]++;
                }
            }
            List<int> result = new List<int>();
            foreach (KeyValuePair<int, int> pair in dict)
            {
                if (dict2.ContainsKey(pair.Key)) result.Add(pair.Key);
            }
            return result.ToArray();

            //method 2
            HashSet<int> set1 = new HashSet<int>();
            for (int i = 0; i < nums1.Length; i++)
            {
                set1.Add(nums1[i]);//true if the element is added to the HashSet<T> object; false if the element is already present.
            }

            HashSet<int> set2 = new HashSet<int>();
            for (int i = 0; i < nums2.Length; i++)
            {
                set2.Add(nums2[i]);
            }

            set1.IntersectWith(set2);//first time to use
            return set1.ToArray();

            //method3

            int[] data1 = new int[1001];
            for (int i = 0; i < nums1.Length; i++)
            {
                data1[nums1[i]]++;
            }
            int[] data2 = new int[1001];
            for (int i = 0; i < nums2.Length; i++)
            {
                data2[nums2[i]]++;
            }
            List<int> result = new List<int>();
            for (int i = 0; i < data1.Length; i++)
            {
                if (data1[i] != 0 && data2[i] != 0) result.Add(i);
            }
            return result.ToArray();
        }

202. 快乐数

思路: 这道题虽然是个简单题,但是 得理解无限循环的意思,才可以解题。无限循环指,和重复出现(n重复出现),一旦这样,则肯定不是快乐数。

public static bool IsHappy(int n)
        {
            HashSet<int> set = new HashSet<int>();
            while (!set.Contains(n) && n != 1)
            {
                set.Add(n);
                n = getSum(n);
            }
            return n == 1;
        }

        public static int getSum(int n)
        {
            int sum = 0;
            while (n != 0)
            {
                sum += (n % 10) * (n % 10);
                n = n / 10;
            }
            return sum;
        }

1. 两数之和

public static int[] TwoSum(int[] nums, int target)
        {
            int[] result = new int[2];
            Dictionary<int, int> dict = new Dictionary<int, int>();
            for (int i = 0; i < nums.Length; i++)
            {
                if (!dict.ContainsKey(target - nums[i]))
                {
                    if (!dict.ContainsKey(nums[i]))
                    {
                        dict.Add(nums[i], i);
                    }
                }
                else
                {
                    result[0] = i;
                    result[1] = dict[target - nums[i]];
                    return result;
                }
            }
            
            return result;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值