Skip to content

Commit 9378776

Browse files
committed
feat: add 007
1 parent e6e61f2 commit 9378776

File tree

5 files changed

+60
-39
lines changed

5 files changed

+60
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# awesome-java-leetcode
22

3-
如今我是一名Android Developer,大学曾是一名ACMer,如今工作了,不想让算法淡出我的记忆,故重拾LeetCode之Algorithm,语言选择的是Java,题库会一点点完善起来,按简单,中等,困难分类,相应难度下按题号排序,工程代码在[project][project]目录中,相关解题都在[note][note]目录中,欢迎star。
3+
我如今是一名Android Developer,大学的我曾是一名ACMer,为了不想让算法淡出我的记忆,故重拾LeetCode之Algorithm,语言选择的是Java,题库会一点点完善起来,按简单,中等,困难分类,相应难度下按题号排序,工程代码在[project][project]目录中,相关解题都在[note][note]目录中,欢迎star。
44

55
## Easy
66

note/001/README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## Description
44

5-
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
5+
Given an array of integers, return **indices** of the two numbers such that they add up to a specific target.
66

7-
You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
You may assume that each input would have ***exactly*** one solution, and you may not use the same element twice.
88

9-
Example:
9+
**Example:**
1010

1111
```
1212
Given nums = [2, 7, 11, 15], target = 9,
@@ -15,9 +15,12 @@ Because nums[0] + nums[1] = 2 + 7 = 9,
1515
return [0, 1].
1616
```
1717

18+
**Tag:** Array, Hash Table
19+
20+
1821
## 思路0
1922

20-
题意是让你从给定的数组中找到两个元素的和为指定值的两个索引,最容易的当然是循环两次,复杂度为O(n^2),首次提交居然是2ms,打败了100%的提交,谜一样的结果,之后再次提交就再也没跑到过2ms了。
23+
题意是让你从给定的数组中找到两个元素的和为指定值的两个索引,最容易的当然是循环两次,复杂度为`O(n^2)`,首次提交居然是2ms,打败了100%的提交,谜一样的结果,之后再次提交就再也没跑到过2ms了。
2124

2225
``` java
2326
public class Solution {
@@ -38,9 +41,9 @@ public class Solution {
3841

3942
## 思路1
4043

41-
利用HashMap作为存储,键为目标值减去当前元素值,索引为值,比如i = 0时,此时首先要判断nums[0] = 2是否在map中,如果不存在,那么插入键值对key = 9 - 2 = 7, value = 0,之后当i = 1时,此时判断nums[1] = 7已存在于map中,那么取出该value = 0作为第一个返回值,当前i作为第二个返回值,具体代码如下所示。
44+
利用HashMap作为存储,键为目标值减去当前元素值,索引为值,比如`i = 0`时,此时首先要判断`nums[0] = 2`是否在map中,如果不存在,那么插入键值对`key = 9 - 2 = 7, value = 0`,之后当`i = 1`时,此时判断`nums[1] = 7`已存在于map中,那么取出该`value = 0`作为第一个返回值,当前`i`作为第二个返回值,具体代码如下所示。
4245

43-
```
46+
``` java
4447
class Solution {
4548
public int[] twoSum(int[] nums, int target) {
4649
int len = nums.length;
@@ -54,4 +57,4 @@ class Solution {
5457
return null;
5558
}
5659
}
57-
```
60+
```

note/007/007.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

note/007/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Reverse Integer
2+
3+
## Description
4+
5+
Reverse digits of an integer.
6+
7+
**Example1:** x = 123, return 321
8+
9+
**Example2:** x = -123, return -321
10+
11+
**Spoilers:**
12+
13+
**Have you thought about this?**
14+
15+
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
16+
17+
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
18+
19+
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
20+
21+
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
22+
23+
**Note:**
24+
25+
The input is assumed to be a 32-bit signed integer. Your function should **return 0 when the reversed integer overflows**.
26+
27+
**Tag:** Math
28+
29+
30+
## 思路
31+
32+
题意很清楚,就是给你一个整型数,返回它的逆序整型数,而且有个小坑点,当它的逆序整型数溢出的话,那么就返回0
33+
,用我们代码表示的话可以求得结果保存在long中,最后把结果和整型的两个范围比较即可。
34+
35+
``` java
36+
public class Solution {
37+
public int reverse(int x) {
38+
long res = 0;
39+
for (; x != 0; x /= 10)
40+
res = res * 10 + x % 10;
41+
return res > Integer.MAX_VALUE || res < Integer.MIN_VALUE ? 0 : (int) res;
42+
}
43+
}
44+
```

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_007/_007.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
public class _007 {
1515
public static void main(String[] args) {
16-
16+
Solution solution = new Solution();
17+
System.out.println(solution.reverse(123));
18+
System.out.println(solution.reverse(-123));
19+
System.out.println(solution.reverse(100));
20+
System.out.println(solution.reverse(1000000003));
1721
}
1822
}
1923

0 commit comments

Comments
 (0)