这里给两种方法,一种哈希表,一种是暴力求解,暴力求解就不贴代码了
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
Map<Integer,Integer> map = new HashMap<>();
for(int j =0; j < nums.length;j++){
int temp = target - nums[j];
if(map.containsKey(temp)){
res[1] = j ;
res[0] = map.get(temp);
}
map.put(nums[j],j);
}
return res;
}
}
注意两个问题,1 能否在先将数添加到map里?产生的影响,如果遇到[3,2,4]目标值为6,他还会找到3,得出错误的答案。如何解决呢?
1:可以添加一个判断,if(map.get(temp)!= j)也就是找的这个值不等于本身的索引
2:我先判断是否包含key,然后再添加,这样做的影响,res数组第一个值有可能会放在第二位,因为先判断,后添加,第二轮的时候,index已经成为第二位了。
给你四个整数数组 nums1
、nums2
、nums3
和 nums4
,数组长度都是 n
,请你计算有多少个元组 (i, j, k, l)
能满足:
思路:4数之和为0分成2 个和2个 相加,只要前两个数之和 和 后两个数之和互为相反数就好
那么如何存放前两个数的可能性是关键,用hashmap来解决这个问题
1: key放a和b两数之和,value 放a和b两数之和出现的次数。
2: 定义int变量res,用来统计 a+b+c+d = 0 出现的次数。这里需要记住 出现的情况是排列组合,第一次编写代码,以为找到正确结果,就删除所对应的前两个数之和的次数,导致结果少了很多。
3:在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。
4:最后返回统计值 res就可以了
//的代码后是错误的
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> ab_map = new HashMap<>();
for(int i =0; i < nums1.length;i++){
for(int j =0; j<nums2.length;j++){
int num = nums1[i] + nums2[j];
Integer count = ab_map.get(num);
if(count==null) count = 0;
if(ab_map.containsKey(num)){
count++;
ab_map.put(num,count);
}
else{
ab_map.put(num,1);
}
}
}
//统计完前两个数的和,接下来考虑后面两个和是否是前两个的相反数
int res = 0;
for(int i: nums3){
for(int j : nums4){
temp = i + j;
if (map.containsKey(0 - temp)) {
res += map.get(0 - temp);
}
}
}
// for(int i : nums3){
// for(int j : nums4){
// int temp = i + j;
// Integer count = ab_map.get(-temp);
// if(count == null) ab_map.remove(-temp);
// else if(count >= 1){
// ab_map.put(-temp,count--);
// res ++;
// }
// else{
// ab_map.remove(-temp);
// }
// }
// }
return res;
}
}
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。 magazine 中的每个字符只能在 ransomNote 中使用一次。
思路:先hashmap 遍历第一个字符串,key 是每个字符,value 是字符出现的次数。然后用hashmap删除第二个字符串的内容,如果最后的isEmpty()为真,
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character, Integer> map = new HashMap<>();
for(char c : ransomNote.toCharArray()){
if(map.containsKey(c)){
map.put(c,map.get(c)+1);
}else{
map.put(c,1);
}
}
for(char c : magazine.toCharArray()){
Integer count = map.get(c);
if(count == null){
count = 0;
}
if(count > 1 ){
map.put(c,count-1);
}
else{
map.remove(c);
}
}
return map.isEmpty();
}
}
这里注意:foreach循环,遍历字符串数组,需要转成数组形式
15. 三数之和 (这道题思考的细节好多)
本题思路:双指针 加一个前行的小帮手,其实也是三个指针各司其职,第一个指针为每次前进1个单位的i,第二个指针为left在i的后一个,第三个指针为right在数组的结尾;
返回值:是二维数组,函数写法 可以是 public int [][] threeSum() 或者
public List<List<Integer>>两者的区别就是一个是每行固定,一个是动态增加不知道长度。
对a的去重:a, b ,c, 对应的就是 nums[i],nums[left],nums[right], 例如{-1, -1 ,2,-1} ,我们需要判断的[-1 -1 2] 和[-1,2,-1]怎么做剪枝?当前使用 nums[i],我们判断前一位是不是一样的元素,如果使用的元素是一样的,那么直接跳过
对b,c的去重:既然对a进行了去重,那么b,c同样也需要去重,
这里最好画图理解{-1,2,2,-1,1,-1},例如nums[i] 为-1,nums[left]在 2,nums[right] 在-1,我们得到一种结果(-1,2.-1)这时左指针向下移动,还是2,这样就需要去重,while(left < right&&nums[left] ==nums[left+1]),left++,同理对右的去重也是如此,最后左右还需要向中间移动。
为什么去重是在esle里面写,为什么不在while循环一开始写呢?因为如果你不把元素添加进去就开始去重,那么你会忽略到正确结果例如{0,0,0,0,0,0},你将不会得到答案。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for(int i=0; i < nums.length;i++){
if(nums[i]>0) return res;
//对a进行剪纸
if( i > 0 && nums[i] == nums[i-1]) continue;
int left = i + 1;
int right = nums.length-1;
while(left < right){
int sum = nums[i] + nums[left] + nums[right];
if(sum > 0){
right--;
}
else if(sum < 0){
left++;
}else{
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
while (left < right && nums[right] == nums[right - 1]) right--;
while (left < right && nums[left] == nums[left + 1]) left++;
right--;
left++ ;
}
}
}
return res;
}
}
清楚了三数之和,那么很容易解决四数之和,只需要加入另一个指针j就ok啦:
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for(int i=0; i < nums.length;i++){
if(nums[i] > 0 && nums[i] > target ) return res;
if(i>0 && nums[i-1]==nums[i]) continue;
for(int j =i+1; j <nums.length;j++){
if (j > i + 1 && nums[j - 1] == nums[j]) {
continue;
}
int left = j + 1;
int right = nums.length-1;
while(left < right){
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum > target){
right--;
}
else if(sum < target){
left++;
}else{
res.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
while(left < right && nums[left] == nums[left + 1]) left++;
while(left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
}
return res;
}
}
反思:
看了别人写的博客,自己写的还是太粗糙,这种粗糙在于 1.投入的时间太少。 2.面向的只是自己,并没有考虑能否让其他阅读者明白。希望第二次刷题的时候能够更精美的写博客!