Skip to content

Commit 3c7c449

Browse files
author
Administrator
committed
commit
1 parent 862485e commit 3c7c449

File tree

38 files changed

+1359
-4
lines changed

38 files changed

+1359
-4
lines changed

.idea/modules.xml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Author: 王俊超
3+
* Date: 2015-08-21
4+
* Time: 16:52
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class Solution {
8+
/**
9+
* <pre>
10+
* 原题
11+
* Determine whether an integer is a palindrome. Do this without extra space.
12+
*
13+
* 题目大意
14+
* 判断一个数字是否是回访字数,不要使用额外的空间。
15+
*
16+
* 解题思路
17+
* 为了不使用额外的空间,参考了其它的解决,那些解法看起来在isPalindrome方法中没有使用额外参数,
18+
* 但是却使用了方法调用,这个比一个整数消耗的空间更多 ,并没有达到题目的要求,是假的实现,
19+
* 所以本题依然采用一个额外的空间进行实现。
20+
* 首先,负数不是回文数字,其次对数字进行逆转,123变成321这样,如果变换后的数字相等说明是回文数字。
21+
* </pre>
22+
*
23+
* @param x
24+
* @return
25+
*/
26+
public boolean isPalindrome(int x) {
27+
28+
// 负数不是回访数字
29+
if (x < 0) {
30+
return false;
31+
}
32+
33+
// 数字逆转后的值,为了不使用溢出采用long
34+
long reverse = 0;
35+
int tmp = x;
36+
37+
// 求逆转后的值
38+
while (tmp != 0) {
39+
reverse = reverse * 10 + tmp % 10;
40+
tmp /= 10;
41+
}
42+
43+
// 判断是否是回文数字
44+
return x == reverse;
45+
}
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

【014】【LongestCommonPrefix】/src/Solution.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ public class Solution {
88
/**
99
* <pre>
1010
* 原题
11-
*   Write a function to find the longest common prefix string amongst an array of strings.
11+
* Write a function to find the longest common prefix string amongst an array of strings.
1212
*
1313
* 题目大意
14-
*   写一个函数找出一个字串所数组中的最长的公共前缀。
14+
* 写一个函数找出一个字串所数组中的最长的公共前缀。
1515
*
1616
* 解题思路
17-
*   第一步先找出长度最小的字符串,然后将这个字符串与其它的字符串相比找出最短的最公共前缀。
17+
* 第一步先找出长度最小的字符串,然后将这个字符串与其它的字符串相比找出最短的最公共前缀。
1818
* </pre>
1919
*
2020
* @param strs
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import java.util.LinkedList;
2+
import java.util.List;
3+
4+
/**
5+
* Author: 王俊超
6+
* Date: 2015-08-21
7+
* Time: 16:23
8+
* Declaration: All Rights Reserved !!!
9+
*/
10+
public class Solution {
11+
12+
private String[] map = {
13+
"abc",
14+
"def",
15+
"ghi",
16+
"jkl",
17+
"mno",
18+
"pqrs",
19+
"tuv",
20+
"wxyz",
21+
};
22+
private List<String> result; // 存储最终结果
23+
private char[] chars; // 保存去掉0,1字符的结果
24+
private char[] curResult; // 存储中间结果
25+
private int end = 0; // 字符数组中的第一个未使用的位置
26+
private int handle = 0; // 当前处理的是第几个字符数字
27+
28+
/**
29+
* <pre>
30+
* 原题
31+
* Given a digit string, return all possible letter combinations that the number could represent.
32+
* A mapping of digit to letters (just like on the telephone buttons) is given below.
33+
*
34+
* Input:Digit string "23"
35+
* Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
36+
*
37+
* Note: Although the above answer is in lexicographical order, your answer
38+
* could be in any order you want.
39+
*
40+
* 题目大意
41+
* 给定一个数字串,返回数字上所有字符的所有组合,数字到字符的映射如上图所示。
42+
* 注意: 尽管上面的结果以字符顺序排列的,你可以以任何顺序返回结果。
43+
*
44+
* 解题思路
45+
* 用一个数组保存数字和字的映射关系,根据数字串的输入,找到对应的字符,组合结果。
46+
* </pre>
47+
*
48+
* @param digits
49+
* @return
50+
*/
51+
public List<String> letterCombinations(String digits) {
52+
result = new LinkedList<>();
53+
54+
if (digits != null && digits.length() > 0) {
55+
56+
chars = digits.toCharArray();
57+
58+
// 对字符串进行处理,去掉0和1
59+
// 找第一个0或者1的位置
60+
while (end < digits.length() && chars[end] != '0' && chars[end] != '1') {
61+
end++;
62+
}
63+
64+
handle = end + 1;
65+
while (handle < chars.length) {
66+
if (chars[handle] != '0' && chars[handle] != '1') {
67+
chars[end] = chars[handle];
68+
end++;
69+
}
70+
handle++;
71+
}
72+
73+
curResult = new char[end];
74+
// while结束后,end为有效字符的长度
75+
handle = 0; // 指向第一个有效字符的位置
76+
77+
letterCombinations();
78+
}
79+
return result;
80+
}
81+
82+
private void letterCombinations() {
83+
if (handle >= end) {
84+
result.add(new String(curResult));
85+
} else {
86+
int num = chars[handle] - '2';
87+
for (int i = 0; i < map[num].length(); i++) {
88+
curResult[handle] = map[num].charAt(i);
89+
handle++;
90+
letterCombinations();
91+
handle--;
92+
}
93+
}
94+
}
95+
}
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

【018】【4Sum】/src/Solution.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* Author: 王俊超
8+
* Date: 2015-08-21
9+
* Time: 16:28
10+
* Declaration: All Rights Reserved !!!
11+
*/
12+
public class Solution {
13+
/**
14+
* <pre>
15+
* 原题
16+
* Given an array S of n integers, are there elements a, b, c, and d in S
17+
* such that a + b + c + d = target? Find all unique quadruplets in the array
18+
* which gives the sum of target.
19+
* Note:
20+
* Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
21+
* The solution set must not contain duplicate quadruplets.
22+
*
23+
* For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
24+
*
25+
* A solution set is:
26+
* (-1, 0, 0, 1)
27+
* (-2, -1, 1, 2)
28+
* (-2, 0, 0, 2)
29+
*
30+
* 题目大意
31+
* 给定一个整数数组,找出a + b + c + d = target的唯一解。
32+
*
33+
* 解题思路
34+
* 先确定a和d的两个数,对于a和d两个数,不能同时重复使用。然后再确定b和c,同样这两个数也不能
35+
* 同时重复使用。找出所有满足条件的解,同时可以保证解不重复。
36+
* </pre>
37+
*
38+
* @param num
39+
* @param target
40+
* @return
41+
*/
42+
public List<List<Integer>> fourSum(int[] num, int target) {
43+
List<List<Integer>> result = new LinkedList<>();
44+
if (num == null || num.length < 4) {
45+
return result;
46+
}
47+
48+
Arrays.sort(num); // 对数组进行排序
49+
50+
for (int i = 0; i < num.length - 3; i++) { // 第一个加数
51+
if (i > 0 && num[i] == num[i - 1]) { // 第一个加数使用不重复
52+
continue;
53+
}
54+
55+
for (int j = num.length - 1; j > i + 2; j--) { // 第四个加数
56+
if (j < num.length - 1 && num[j] == num[j + 1]) { // 第四个加数使用不重复
57+
continue;
58+
}
59+
60+
int start = i + 1; // 第二个加数
61+
int end = j - 1; // 第三个加数
62+
int n = target - num[i] - num[j];
63+
64+
while (start < end) {
65+
if (num[start] + num[end] == n) {
66+
List<Integer> four = new ArrayList<>(4);
67+
four.add(num[i]);
68+
four.add(num[start]);
69+
four.add(num[end]);
70+
four.add(num[j]);
71+
72+
result.add(four);
73+
74+
do {
75+
start++;
76+
} while (start < end && num[start] == num[start - 1]); // 保证再次使用第二个数不重复
77+
78+
do {
79+
end--;
80+
} while (start < end && num[end] == num[end + 1]); // 保证再次使用第三个数不重复
81+
} else if (num[start] + num[end] < n) {
82+
do {
83+
start++;
84+
} while (start < end && num[start] == num[start - 1]); // 保证再次使用第二个数不重复
85+
} else {
86+
do {
87+
end--;
88+
} while (start < end && num[end] == num[end + 1]); // 保证再次使用第三个数不重复
89+
}
90+
}
91+
}
92+
}
93+
return result;
94+
}
95+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Author: Íõ¿¡³¬
3+
* Date: 2015-08-21
4+
* Time: 16:30
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class ListNode {
8+
int val;
9+
ListNode next;
10+
11+
ListNode(int x) {
12+
val = x;
13+
next = null;
14+
}
15+
}

0 commit comments

Comments
 (0)