LeetCode代码整理(二)
来源:互联网 发布:path软件 编辑:程序博客网 时间:2024/06/05 09:17
27.Remove Element
https://leetcode.com/problems/remove-element/
我用的方法是记录相同的个数,移动不同的元素,看Discuss,发现有人用记录不同个数来移动。
public class Solution { public int removeElement(int[] nums, int val) { int count = 0; for(int i = 0; i < nums.length; i++) { if(nums[i] == val) count++; else nums[i-count] = nums[i]; } return nums.length - count; }}
283.Move Zeroes
https://leetcode.com/problems/move-zeroes/
这道题和上面27差不多。
public class Solution { public void moveZeroes(int[] nums) { int count = 0; int len = nums.length; for(int i =0 ; i < len; i++) { if(nums[i] == 0) count++; else { nums[i-count] = nums[i]; } } for(int i = len- count; i < len; i++) nums[i] = 0; }}
28.Implement strStr()
https://leetcode.com/problems/implement-strstr/
当年最在行的莫过于字符串的遍历这种简单的题了,暴力解法,这次忘记考虑两个是否都是“”。
public class Solution { public int strStr(String haystack, String needle) { int lenH = haystack.length(); int lenN = needle.length(); for(int i = 0 ; i <= lenH - lenN; i++) { boolean flag = true; for(int j = 0; j < lenN; j++) { if(haystack.charAt(i + j) != needle.charAt(j)) { flag = false; break; } } if(flag) return i; } if(lenH == lenN && lenH == 0) return 0; else return -1; }}
0 0
- LeetCode代码整理(二)
- LeetCode代码整理(一)
- LeetCode代码整理(三)
- LINQ查询代码整理(二)
- LeetCode题解整理版(二)
- LeetCode题解整理版(二)
- 布局代码整理二 footer
- 逻辑组合与代码整理及优化(二)
- 新手必备的常用代码片段整理(二)
- 新手必备的常用代码片段整理(二)
- Android:新手必备的常用代码片段整理(二)
- 新手必备的常用代码片段整理(二)
- 新手必备的常用代码片段整理(二)
- 新手必备的常用代码片段整理(二)
- OpenGL实验(二三代码整理)桌子
- leetcode 题解代码整理 1-5题
- leetcode 题解代码整理 6-10题
- leetcode 题解代码整理 11-15题
- 用于去掉button外部的边线
- 安卓向服务器(Spring)发送json
- iOS上Sqlite多线程问题
- java.lang.NoClassDefFoundError:/javax/transaction/SystemException错误解决方法
- popToViewController返回到指定页面
- LeetCode代码整理(二)
- 【学习笔记javascript设计模式与开发实践(组合模式)----10】
- Desktop Entry,关于Linux如何创建桌面快捷方式
- 模糊查询
- MySQL准备语句
- log4cplus 编译问题
- hessian 使用token安全机制
- perl 获取post响应信息
- java的日期类型