erDiagram
CAR ||--o{ NAMED-DRIVER : allows
PERSON ||--o{ NAMED-DRIVER : is
line-through strong italic mark_tag·
print("单行代码测试")
!> p.tip em code
单行引用
docsify-themeable's index.html.
Ctrl + S : Save file
- Python
- Rust
- PHP
题目描述
给定一个整数数组和一个目标值,找出数组中【和为目标值的两个数】,且元素不能重复
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
Note
An alert of type note.
[!NOTE|style:flat] An alert of type 'note.flat'.
Tip
An alert of type 'tip'
[!TIP|style:flat|label:untitled|iconVisibility:visible] (style:flat), label:untitled, iconVisibility:visible
Warning
This is a warning alert!
[!WARNING|style:flat] (style:flat)This is a warning alert!
[!DANGER] This is a danger alert!
[!DANGER|style:flat] (style:flat)This is a danger alert!
- github/vscode support diff block
+ this text is highlighted in green
- this text is highlighted in red