0%

Day02 - 删除排序数组的重复项

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

示例1

给定数组 nums = [1,1,2],函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。你不需要考虑数组中超出新长度后面的元素。`

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int removeDuplicates(int[] nums) {
int length;

if (nums.length == 0) {
return 0;
}

length = 1;

for (int i = 0, j = 1; i < nums.length; i++) {
while (j < nums.length && nums[j] == nums[i]) {
j++;
}

if (j >= nums.length)
break;

if (nums[j] != nums[i]) {
nums[i + 1] = nums[j];
length++;
}
}

return length;
}
}

/**
执行用时 :1 ms, 在所有 Java 提交中击败了99.89%的用户
*/

length 记录新数组长度,只需要在原数组中遍历一次与第 i 项比较若不同则将第 j 项赋给第 i+1 项。

Reference
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

Day01 - 两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] two = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = nums.length - 1; j > i; j--) {
if (nums[i] + nums[j] == target) {
two[0] = i;
two[1] = j;
return two;
}
}
}
return null;
}
}

Reference
https://leetcode-cn.com/problems/two-sum

Welcome to GitHub Pages

You can use the editor on GitHub to maintain and preview the content for your website in Markdown files.

Whenever you commit to this repository, GitHub Pages will run Jekyll to rebuild the pages in your site, from the content in your Markdown files.

Markdown

Markdown is a lightweight and easy-to-use syntax for styling your writing. It includes conventions for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Syntax highlighted code block

# Header 1
## Header 2
### Header 3

- Bulleted
- List

1. Numbered
2. List

**Bold** and _Italic_ and `Code` text

[Link](url) and ![Image](src)

For more details see GitHub Flavored Markdown.

Jekyll Themes

Your Pages site will use the layout and styles from the Jekyll theme you have selected in your repository settings. The name of this theme is saved in the Jekyll _config.yml configuration file.

Support or Contact

Having trouble with Pages? Check out our documentation or contact support and we’ll help you sort it out.