首页 > 编程知识 正文

pycharm怎么创建nums.txt文件,matlab中nums=0是什么意思

时间:2023-05-04 18:15:11 阅读:267345 作者:4543

所有实现代码均使用JAVA

letcode上面有个算法题,贴原图

先分析一下:

第一个条件,a+b+c = 0,这个好办,最简单的就是循环三次,把所有情况都过一篇就可得到所有结果,
第二个条件,满足条件且不重复,这个的话将每次符合条件的三个数字存进一个Map<Integer,Integer>,然后再存进List<Map<Integer,Integer>>,每次遇到符合条件的然后遍历List,再通过map.get()判断是否存在重复即可,
至此,完结,

但是如果nums的个数无穷大,那么循环次数也会指数级增长,所以上面这种做法不可取,

重新分析一下:

我们可以先拆分来看,从 a+b+c = 0 可以得出 a = 0 -(b + c)
a就是最外层每次循环的当前值
然后就可以搭第一层循环了,

int[] nums = {-1, 0, 1, 2, -1, -4}List<Integer[]> resList = new ArrayList<>(); //保存结果集合for (int i = 0; i < nums.length; i++) { int a = nums[i]; int bAndC = 0 - nums[i];// b+c }

然后再来分析b+ c; b+c就是用除了a之外的剩余数组成的
可以再搭一个循环获取b + c的值,但是需要一个Map来辅助, 这样的话一次遍历就可以把两个数全部“揪”出来,

int[] nums = {-1, 0, 1, 2, -1, -4};List<Integer[]> resList = new ArrayList<>(); //保存结果集合for (int i = 0; i < nums.length; i++) { int a = nums[i]; int bAndC = 0 - nums[i];// b+c Map<Integer,Integer> map = new HashMap<>(); //保存判断结果值b、c的值 for (int j = i + 1; j < nums.length; j++) { if (map.containsKey(bAndC - nums[j])) { resList .add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]}); } else { map.put(nums[j], nums[j]); } } }

这样下来所有的数组都就可以找到了,

分析最后一个问题,不重复,

再来分析 a + b + c = 0,

a + b + c = 0 可得 a + b = 0 – c,即通过a + b 确定 c 的值

符合的三元数组一共有三个数,总共有三种组合,

a + b = 0 - c,

b + c = 0 - a ,

a + c = 0 - b,

所以,遇到符合的数组就把当前所有情况保存起来,然后再遇到符合数组时再判断是否有相同的,这需要一个HashSet辅助,最后就可以获取预期结果了

int[] nums = {-1, 0, 1, 2, -1, -4};List<Integer[]> resList = new ArrayList<>(); //保存结果集合HashSet noRepeatSet = new HashSet(); //保存不重复的值并判重for (int i = 0; i < nums.length; i++) { int a = nums[i]; int bAndC = 0 - nums[i];// b+c Map<Integer, Integer> map = new HashMap<>(); //保存判断结果值b、c的值 for (int j = i + 1; j < nums.length; j++) { if (map.containsKey(bAndC - nums[j])) { if (!noRepeatSet.contains(nums[i] + nums[j])) { //判重 resList.add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]}); noRepeatSet.add(map.get(bAndC - nums[j]) + nums[j]); //保存所有可能值 noRepeatSet.add(map.get(bAndC - nums[j]) + nums[i]); noRepeatSet.add(nums[i] + nums[j]); } } else { map.put(nums[j], nums[j]); } }}

但这样还有问题,
-2 + 2 = 0
-3 + 3 = 0

a + b 会出现相同结果然而内容不同,所以这样不能判定,

两个不行,那就三个(全部)吧,

int[] nums = {-1, 0, 1, 2, -1, -4, 0, 0,4};List<Integer[]> resList = new ArrayList<>(); //保存结果集合HashSet noRepeatSet = new HashSet(); //保存不重复的值并判重int[] curArr;for (int i = 0; i < nums.length; i++) { int a = nums[i]; int bAndC = 0 - nums[i];// b+c Map<Integer, Integer> map = new HashMap<>(); //保存判断结果值b、c的值 for (int j = i + 1; j < nums.length; j++) { if (map.containsKey(bAndC - nums[j])) { curArr = new int[]{map.get(bAndC - nums[j]), nums[j], nums[i]}; Arrays.sort(curArr); //排序 if (!noRepeatSet.contains(curArr[0]+""+curArr[1]+""+curArr[2])) { //判重 resList.add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]}); noRepeatSet.add(curArr[0]+""+curArr[1]+""+curArr[2]); //保存此次数组值 } } else { map.put(nums[j], nums[j]); } }}

符合条件的的先排序再做判断,这样就可以准确判重了.
.
.
.

但是还有这样一种特殊情况,存在多个0的时候,结果里并不会出现 [0,0,0]

int[] nums = {-1, 0, 1, 2, -1, -4,0,0};

所以把这种情况加进去

int[] nums = {-1, 0, 1, 2, -1, -4, 0, 0,4};List<Integer[]> resList = new ArrayList<>(); //保存结果集合HashSet noRepeatSet = new HashSet(); //保存不重复的值并判重boolean isAllO = false; //是否出现过都为0int[] curArr;int k = 0;for (int i = 0; i < nums.length; i++) { int a = nums[i]; int bAndC = 0 - nums[i];// b+c Map<Integer, Integer> map = new HashMap<>(); //保存判断结果值b、c的值 for (int j = i + 1; j < nums.length; j++) { k++; if (map.containsKey(bAndC - nums[j])) { if (bAndC == 0 && nums[i] == 0 && nums[j] == 0) { //已添加过跳过本轮 if (isAllO) { continue; } else { resList.add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]}); //全为 0 添加到集合 isAllO = true; continue; } } curArr = new int[]{map.get(bAndC - nums[j]), nums[j], nums[i]}; Arrays.sort(curArr); //排序 if (!noRepeatSet.contains(curArr[0]+""+curArr[1]+""+curArr[2])) { //判重 resList.add(new Integer[]{map.get(bAndC - nums[j]), nums[j], nums[i]}); noRepeatSet.add(curArr[0]+""+curArr[1]+""+curArr[2]); //保存此次数组值 } } else { map.put(nums[j], nums[j]); } }}

以上所有Map和hashSet都可互换,但是使用HashSet比较“应景”一点。
以上所有解决方法均为个人理解,如有更好方案还望留言

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。