重庆百度网站推广,外贸拓客软件,网站建设服务代理商,免费企业网站cms系统阿华代码#xff0c;不是逆风#xff0c;就是我疯 你们的点赞收藏是我前进最大的动力#xff01;#xff01; 希望本文内容能够帮助到你#xff01;#xff01; 目录
第一题#xff1a;移动零
第二题#xff1a;复写零
第三题#xff1a;快乐数
第四题#xff1a… 阿华代码不是逆风就是我疯 你们的点赞收藏是我前进最大的动力 希望本文内容能够帮助到你 目录
第一题移动零
第二题复写零
第三题快乐数
第四题盛最多水的容器
第五题有效三角形的个数
第六题和为s的两个数
第七题三数之和
第八题四数之和 第一题移动零
283. 移动零 - 力扣LeetCode class Solution {public void moveZeroes(int[] nums) {int dest -1;int cur 0;int tem 0;while(cur nums.length){if(nums[cur] ! 0){dest;tem nums[dest] ;nums[dest] nums[cur];nums[cur] tem;}cur;}}
}
第二题复写零
1089. 复写零 - 力扣LeetCode class Solution {public void duplicateZeros(int[] arr) {int cur 0 , dest -1 , n arr.length;while(cur n){//dest位置不确定所以不能用作判断循环的条件if(arr[cur] ! 0){dest;}else{dest 2;}if(dest n-1){break;}cur;}if(dest n){arr[n-1] 0;dest - 2;cur--;}//开始从后往前复写while(cur 0 ){if(arr[cur] ! 0){arr[dest] arr[cur];cur--;dest--;}else{arr[dest] arr[cur];dest--;arr[dest] arr[cur];cur--;dest--;}}}
}
第三题快乐数
202. 快乐数 - 力扣LeetCode class Solution {public static int sumResult(int n){int sum 0;while(n ! 0){//int tem n % 10;//sum tem * tem;sum Math.pow(n%10,2);n n/10;}return sum;} public boolean isHappy(int n) {int slow n ,fast sumResult(n);while(slow ! fast){slow sumResult(slow);fast sumResult(sumResult(fast));}return slow 1;}
}
第四题盛最多水的容器
11. 盛最多水的容器 - 力扣LeetCode class Solution {public int maxArea(int[] height) {int left 0 ,right height.length -1 , ret 0;while(left right){int v Math.min(height[left],height[right]) * (right - left);ret Math.max(ret,v);if(height[left] height[right]){left;}else{right--;}}return ret;}
}
第五题有效三角形的个数
611. 有效三角形的个数 - 力扣LeetCode class Solution {public int triangleNumber(int[] nums) {int end nums.length-1;Arrays.sort(nums);int count 0;for( ; end 2 ; end--){int right end-1;int left 0;while(left right){int tem nums[left] nums[right];if(tem nums[end]){count right - left;right--;}else{left;}} } return count;}
} 第六题和为s的两个数
LCR 179. 查找总价格为目标值的两个商品 - 力扣LeetCode class Solution6 {public int[] twoSum(int[] price, int target) {int n price.length;int left 0 , right n-1;int[] car {-1,-1};while(left right){int result sum(price[left],price[right]);if(result target){left;}else if(result target){right--;}else{car[0] price[left];car[1] price[right];return car;}}return car;}public int sum(int a , int b){int sum a b;return sum;}
}
第七题三数之和
15. 三数之和 - 力扣LeetCode class Solution {public ListListInteger threeSum(int[] nums) {Arrays.sort(nums);int n nums.length;ListListInteger ret new ArrayList();for(int i 0 ; i n ;){if(nums[i] 0){break;}int left i1 , right n-1 ,target -nums[i];while(left right){int sum sum(nums[left] , nums[right]);if(sum target){right--;}else if(sum target){left; }else{ret.add(Arrays.asList(nums[left] , nums[right] , nums[i]));left;right--;while(left right nums[left] nums[left - 1]){left;}while(left right nums[right] nums[right 1]){right--;}}}i;while(i n nums[i] nums[i-1]){i;}}return ret;}public int sum(int a , int b){return ab;}
}
第八题四数之和
18. 四数之和 - 力扣LeetCode
强烈建议先把三数之和看完 class Solution {public ListListInteger fourSum(int[] nums, int target) {ListListInteger list new ArrayList();Arrays.sort(nums);int n nums.length;for(int i 0 ; i n ; ){//第一层循环遍历固定a遍历数组int a nums[i];for(int j i1 ; j n ; ){int b nums[j] , left j1 ,right n-1;long tem (long)target - a - b;while(left right){long sum sum(nums[left],nums[right]);if(sum tem){right--;}else if(sum tem){left;}else{list.add(Arrays.asList(a,b,nums[left],nums[right]));left;right--;while(left right nums[left] nums[left-1]){left;}while(right left nums[right] nums[right1]){right--;}}}j;while(j n-2 nums[j] nums[j-1]){j;}}i;while(i n-1 nums[i] nums[i-1]){i;}}return list;}public int sum(int a , int b){return ab;}
} 文章转载自: http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.tqpnf.cn.gov.cn.tqpnf.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn