Seven's blog

你不会找到路,除非你敢于迷路

Algorithm

35. 搜索插入位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int searchInsert(int[] nums, int target) {
if (nums == null) {
return 0;
}

for (int i = 0; i < nums.length; i++) {
if (target <= nums[i]) {
return i;
}
}

return nums.length;
}
}

执行用时: 0ms, 内存消耗: 38.5MB.

阅读全文 »

Algorithm

27. 移除元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int removeElement(int[] nums, int val) {
if (nums == null) {
return 0;
}

int insertIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[insertIndex] = nums[i];
insertIndex++;
}
}

return insertIndex;
}
}

执行用时: 0ms, 内存消耗: 35.3MB.

阅读全文 »

前言

如果你看不懂标题, 那么恭喜你, 没有遇到这个心烦的问题.

Linux 下使用 wine 微信有一定概率会出现烦人的黑色色块, 看它不爽, 却又无可奈何, 极大地影响了用户体验. 刚好在 github 看到了一个解决方案, 整理记录至此.

环境

编辑本文时笔者电脑的环境是:

  • Ubuntu 19.10
  • deepin-wine-ubuntu 2018-12-ubuntu3
  • deepin-wine-wechat

其他环境请自行举一反三.

阅读全文 »

Algorithm

26. 删除排序数组中的重复项

解法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}

int scanIndex;
int insertIndex = 1;
int compareIndex = 0;
int length = 1;

for (scanIndex = 1; scanIndex < nums.length; scanIndex++) {
if (nums[scanIndex] != nums[compareIndex]) {
nums[insertIndex] = nums[scanIndex];
insertIndex++;
length++;
compareIndex = scanIndex;
}
}

return length;
}
}

执行用时: 1ms, 内存消耗: 41.5MB.

阅读全文 »

Algorithm

1108. IP 地址无效化

  • 解法一:

    直接调用 Java String 的 replaceAll() 方法, 因为该方法内部实现使用了正则匹配, 所以执行效率不高.

    1
    2
    3
    4
    5
    class Solution {
    public String defangIPaddr(String address) {
    return address == null ? null : address.replaceAll("\\.", "[\\.]");
    }
    }

    执行用时: 3ms, 内存消耗: 34.4MB.

    阅读全文 »
0%