[leetCode刷题笔记]2017.02.05
2017-02-06 11:16
106 查看
今天早上上了一门课,然后出了除了买菜吃饭之外一直看书看到现在。。。。。然后继续刷题。。。。啊啊啊
18. 4Sum
方法见下链接:
http://www.cnblogs.com/yrbbest/p/4434188.html
和3sum一样。时间复杂度是O(n3)
26. Remove Duplicates from Sorted Array
这道题用到一个删除数组元素操作。另外,原位置删除以后,要减少循环的次数。。。个人觉得建一个新的array可能比这方便的多。
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}
int output = 0;
int curr = 0;
int i = 0;
int len = nums.length;
while (i < len) {
if (i == 0) {
curr = nums[i];
output++;
i++;
}
else {
if (nums[i] != curr) {
output++;
curr = nums[i];
i++;
}
else {
delete(i, nums);
len--;
}
}
}
return output;
}
private void delete(int k, int[] nums) {
for (int i = k; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
}
27. Remove Element
思路和前面的一样,用上first point 和last point
public class Solution {
public int removeElement(int[] nums, int val) {
int last = nums.length - 1;
int first = 0;
while (first <= last) {
if (val != nums[first]) {
first++;
}
else {
delete(first, nums);
last--;
}
}
return last + 1;
}
private void delete(int k, int[] nums) {
for (int i = k; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
}
31. Next Permutation
这道题目死活都看不懂。。。。所以找了一个讲的比骄清晰的。
其实不难http://blog.csdn.net/happyaaaaaaaaaaa/article/details/50973026
18. 4Sum
方法见下链接:
http://www.cnblogs.com/yrbbest/p/4434188.html
和3sum一样。时间复杂度是O(n3)
26. Remove Duplicates from Sorted Array
这道题用到一个删除数组元素操作。另外,原位置删除以后,要减少循环的次数。。。个人觉得建一个新的array可能比这方便的多。
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}
int output = 0;
int curr = 0;
int i = 0;
int len = nums.length;
while (i < len) {
if (i == 0) {
curr = nums[i];
output++;
i++;
}
else {
if (nums[i] != curr) {
output++;
curr = nums[i];
i++;
}
else {
delete(i, nums);
len--;
}
}
}
return output;
}
private void delete(int k, int[] nums) {
for (int i = k; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
}
27. Remove Element
思路和前面的一样,用上first point 和last point
public class Solution {
public int removeElement(int[] nums, int val) {
int last = nums.length - 1;
int first = 0;
while (first <= last) {
if (val != nums[first]) {
first++;
}
else {
delete(first, nums);
last--;
}
}
return last + 1;
}
private void delete(int k, int[] nums) {
for (int i = k; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
}
31. Next Permutation
这道题目死活都看不懂。。。。所以找了一个讲的比骄清晰的。
其实不难http://blog.csdn.net/happyaaaaaaaaaaa/article/details/50973026
相关文章推荐
- LeetCode 笔记系列三 3Sum
- LeetCode题目笔记(一) -- Minimum Depth Of Binary Tree
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
- LeetCode 笔记系列 19 Scramble String [合理使用递归]
- 【LeetCode】Triangle做题笔记
- LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]
- LeetCode 笔记系列 14 N-Queen II [思考的深度问题]
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
- LeetCode题目笔记(二) -- Maximum Depth of Binary Tree
- LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
- LeetCode 笔记系列十 Suduko
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
- LeetCode 笔记系列七 Substring with Concatenation of All Words
- LeetCode题目笔记(三) -- Median of Two Sorted Arrays
- LeetCode 笔记系列一 Median of Two Sorted Arrays
- LeetCode 笔记系列五 Generate Parentheses
- LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]
- 【LeetCode】Reverse Integer解题笔记
- 【LeetCode】Valid Sudoku解题笔记