## [1. 两数之和](https://leetcode.cn/problems/two-sum/) ### 解法1,利用HashMap(map.get(Key))实现数的存储和输出 ```java class Solution { public int[] twoSum(int[] nums, int target) { Map<integer,integer> map = new HashMap<>(); int[] ints = new int[2]; for (int i = 0; i < nums.length; i++) { int goal_num=target-nums[i]; if (map.containsKey(goal_num)){ ints[0]=i; ints[1]=map.get(goal_num); break; } map.put(nums[i],i); } return ints; } } ``` #### 思路 1. 是对于hashmap的利用,即利用其key和value的特性,达到二维数组的存储效果,存储两个数值 2. 是哈希表的优势就在于O(1)的查找,利用map.get(Key)函数,可以直接实现目标数的查找 3. 就是对于题意的理解,寻找两数之和翻译过后的意思其实是寻找目标值target作差后值的数字 即int goal_num=target-nums[i]; #### 参考资料 [ACM 选手图解 LeetCode 两数之和 | 编程文青李狗蛋](https://leetcode.cn/problems/two-sum/solution/acm-xuan-shou-tu-jie-leetcode-liang-shu-3rf9r/) [「代码随想录」梦开始的地方!](https://leetcode.cn/problems/two-sum/solution/by-carlsun-2-sarb/) ### 解法2:暴力解法 思路:两层,外层取遍历nums[i],内层遍历nums[i+1],再一个if,直接解决……(够无脑哈哈……) ```java class Solution { public int[] twoSum(int[] nums, int target) { int[] ints = new int[2]; for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target) { ints[0] = i; ints[1] = j; break; } } } return ints; } } ``` ## [242. 有效的字母异位词](https://leetcode.cn/problems/valid-anagram/) ### 解法1: 利用哈希映射将存储元素进行比较 ```java class Solution { public boolean isAnagram(String s, String t) { int[] record = new int[26]; if (s.length()!=t.length()){ return false; } //basecase for (int i = 0; i <s.length() ;="" i++)="" {="" char="" c="s.charAt(i);//String转char" record[c-'a']++;="" char转int="" }="" for="" (int="" i="0;" <t.length()="" record[c-'a']--;="" (integer="" :="" record)="" if="" (i!="0){" return="" false;="" true;="" ```="" ###="" 解法2:排序,然后直接遍历="" ```java="" class="" solution="" public="" boolean="" isanagram(string="" s,="" string="" t)="" if(s.length()!="t.length()){" char[]="" char_s="s.toCharArray();" char_t="t.toCharArray();" arrays.sort(char_s);="" arrays.sort(char_t);="" int="" for_length="Math.min(s.length(),t.length());" <for_length;="" (char_s[i]!="char_t[i]){" ####="" 思路:="" ![image-20230123180140059](http:="" 150154.xyz="" 2023="" 1="" image-20230123180140059.png)="" 资料:="" #="" <img="" src=""> ![image-20230123180313012](http://150154.xyz/2023/1/image-20230123180313012.png) [「代码随想录」有效的字母异位词](https://leetcode.cn/problems/valid-anagram/solution/by-carlsun-2-cph7/) [我自己的草稿](https://flowus.cn/share/22429806-7be9-4834-94e3-2f182d2d87d7 【FlowUs 息流】Untitled (Draft).pdf) ![image-20230123180636386](http://150154.xyz/2023/1/image-20230123180636386.png) ![image-20230123181230668](http://150154.xyz/2023/1/image-20230123181230668.png) ![image-20230123181602271](http://150154.xyz/2023/1/image-20230123181602271.png) ## Test1 # test2 ### test3 ![image-20230123181626121](http://150154.xyz/2023/1/image-20230123181626121.png) ![image-20230123181642438](http://150154.xyz/2023/1/image-20230123181642438.png) ![image-20230123181646242](http://150154.xyz/2023/1/image-20230123181646242.png) ![image-20230123192155839](http://150154.xyz/2023/1/image-20230123192155839.png) ![image-20230123200639523](http://150154.xyz/2023/1/image-20230123200639523.png) ![image-20230123200948773](http://150154.xyz/2023/1/image-20230123200948773.png) ![img](E:%5CAAA%20SZW%5C%E8%85%BE%E8%AE%AF%5C1653967869%5CImage%5CC2C%5C47$DV1OLCBJEC)NBV~0){8C.jpg) ![img](E:%5CAAA%20SZW%5C%E8%85%BE%E8%AE%AF%5C1653967869%5CImage%5CC2C%5C47$DV1OLCBJEC)NBV~0){8C.jpg) ![image-20230123203527618](http://150154.xyz/2023/1/image-20230123203527618.png) ![image-20230123203909269](http://150154.xyz/2023/1/image-20230123203909269.png) ![image-20230123204454799](http://150154.xyz/2023/1/image-20230123204454799.png) https://flowus.cn/share/dcbbbdb5-fac4-4948-9ecb-e3a836e3d081 计划本
标签:150154,01,int,image,随想录,png,2023,xyz,两数 From: https://www.cnblogs.com/szwsama/p/17065945.html