-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
142 changed files
with
15,383 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.aphysia; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
|
||
public class GenerateRoute { | ||
static String format =" * [%s](/剑指OfferV2/%s)"; | ||
String fileName = "/Users/xuwenhao/Nutstore Files/我的坚果云/codeSolution/剑指OfferV2"; | ||
|
||
static String leetCode = " * [%s](/leetcode/%s)"; | ||
static String leetcodeName= "/Users/xuwenhao/Nutstore Files/我的坚果云/codeSolution/leetcode"; | ||
|
||
static String coderFormat = " * [%s](/程序员面试金典/%s)"; | ||
static String coderName= "/Users/xuwenhao/Nutstore Files/我的坚果云/codeSolution/程序员面试金典"; | ||
|
||
public static void main(String[] args) { | ||
folderMethod1(coderName,coderFormat); | ||
} | ||
public static void folderMethod1(String path,String format) { | ||
int fileNum = 0, folderNum = 0; | ||
File file = new File(path); | ||
LinkedList<File> list = new LinkedList<>(); | ||
|
||
if (file.exists()) { | ||
if (null == file.listFiles()) { | ||
return; | ||
} | ||
list.addAll(Arrays.asList(file.listFiles())); | ||
list.sort((l1,l2)->l1.getName().compareTo(l2.getName())); | ||
for (File file1:list){ | ||
System.out.println(String.format(format,file1.getName().replace(".md",""),file1.getName())); | ||
} | ||
} else { | ||
System.out.println("文件不存在!"); | ||
} | ||
System.out.println("文件夹数量:" + folderNum + ",文件数量:" + fileNum); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.aphysia.interview; | ||
|
||
import java.util.HashSet; | ||
|
||
public class Solution1 { | ||
/*public boolean isUnique(String astr) { | ||
if (astr == null) { | ||
return false; | ||
} | ||
HashSet<Character> set = new HashSet<>(); | ||
for (int i = 0; i < astr.length(); i++) { | ||
if (set.contains(astr.charAt(i))) { | ||
return false; | ||
} else { | ||
set.add(astr.charAt(i)); | ||
} | ||
} | ||
return true; | ||
}*/ | ||
|
||
public boolean isUnique(String astr) { | ||
if (astr == null) { | ||
return false; | ||
} | ||
long left = 0; | ||
long right = 0; | ||
for (char c : astr.toCharArray()) { | ||
if (c >= 64) { | ||
long bit = 1L << (c - 64); | ||
if ((left & bit) != 0) { | ||
return false; | ||
} | ||
left |= bit; | ||
} else { | ||
long bit = 1L << c; | ||
if ((right & bit) != 0) { | ||
return false; | ||
} | ||
right |= bit; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.aphysia.interview; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Solution2 { | ||
public boolean CheckPermutation(String s1, String s2) { | ||
if (s1 == null && s2 == null) { | ||
return true; | ||
} | ||
if (s1 == null || s2 == null || s1.length() != s2.length()) { | ||
return false; | ||
} | ||
int[] counts = new int[26]; | ||
for (int i = 0; i < s1.length(); i++) { | ||
counts[s1.charAt(i) - 'a']++; | ||
} | ||
|
||
for (int i = 0; i < s2.length(); i++) { | ||
if (counts[s1.charAt(i) - 'a'] == 0) { | ||
return false; | ||
} | ||
counts[s1.charAt(i) - 'a']--; | ||
} | ||
return true; | ||
} | ||
/*public boolean CheckPermutation(String s1, String s2) { | ||
if (s1 == null && s2 == null) { | ||
return true; | ||
} | ||
if (s1 == null || s2 == null || s1.length() != s2.length()) { | ||
return false; | ||
} | ||
Map<Character, Integer> count1 = new HashMap<>(); | ||
for (int i = 0; i < s1.length(); i++) { | ||
if (count1.get(s1.charAt(i)) != null) { | ||
count1.put(s1.charAt(i), count1.get(s1.charAt(i)) + 1); | ||
} else { | ||
count1.put(s1.charAt(i), 1); | ||
} | ||
} | ||
Map<Character, Integer> count2 = new HashMap<>(); | ||
for (int i = 0; i < s2.length(); i++) { | ||
if (count2.get(s2.charAt(i)) != null) { | ||
count2.put(s2.charAt(i), count1.get(s2.charAt(i)) + 1); | ||
} else { | ||
count2.put(s2.charAt(i), 1); | ||
} | ||
} | ||
for (Map.Entry<Character, Integer> entry : count1.entrySet()) { | ||
if (count2.get(entry.getKey()) != entry.getValue()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}*/ | ||
/*public boolean CheckPermutation(String s1, String s2) { | ||
if (s1 == null && s2 == null) { | ||
return true; | ||
} | ||
if (s1 == null || s2 == null || s1.length() != s2.length()) { | ||
return false; | ||
} | ||
char[] chars1 = s1.toCharArray(); | ||
char[] chars2 = s2.toCharArray(); | ||
Arrays.sort(chars1); | ||
Arrays.sort(chars2); | ||
for (int i = 0; i < chars1.length; i++) { | ||
if (chars1[i] != chars2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.aphysia.interview; | ||
|
||
public class Solution3 { | ||
public static void main(String[] args) { | ||
System.out.println(replaceSpaces("Mr John Smith ", 13)); | ||
} | ||
|
||
public static String replaceSpaces(String S, int length) { | ||
// 字符串的长度 | ||
int size = S.length(); | ||
char[] arrays = new char[size]; | ||
// 这个其实是字符串有效长度最后的一个位置的索引 | ||
int strIndex = length - 1; | ||
// 从数组后面开始往前面放 | ||
int arraysIndex = size - 1; | ||
while (strIndex >= 0 && arraysIndex >= 0) { | ||
// 从有效的长度的最后一个字符开始 | ||
char c = S.charAt(strIndex--); | ||
// 如果为空格,则替换成%20 | ||
if (c == ' ') { | ||
arrays[arraysIndex--] = '0'; | ||
arrays[arraysIndex--] = '2'; | ||
arrays[arraysIndex--] = '%'; | ||
} else { | ||
// 否则保持原字符串 | ||
arrays[arraysIndex--] = c; | ||
} | ||
} | ||
// 结束的时候,可能前面还剩下空位置,截取掉 | ||
return new String(arrays).substring(arraysIndex + 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.aphysia.leetcode; | ||
|
||
public class Solution28 { | ||
public int strStr(String haystack, String needle) { | ||
if (needle == null || needle.length() == 0) { | ||
return 0; | ||
} | ||
if (haystack == null || haystack.length() == 0) { | ||
return -1; | ||
} | ||
// next 数组,保存已匹配的字符串的最长公共前后缀 | ||
int next[] = new int[needle.length()]; | ||
getNext(next, needle); | ||
int i = 0; | ||
int j = 0; | ||
while (i < haystack.length() && j < needle.length()) { | ||
if (j == -1 || haystack.charAt(i) == needle.charAt(j)) { | ||
i++; | ||
j++; | ||
} else j = next[j]; // 模式串上的指针回溯到j位置 | ||
} | ||
if (j == needle.length()) return (i - j); | ||
else return -1; | ||
} | ||
|
||
void getNext(int[] next, String patterns) { | ||
int j = 0, k = -1; | ||
next[0] = -1; | ||
while (j < patterns.length() - 1) { | ||
if (k == -1 || patterns.charAt(j) == patterns.charAt(k)) { | ||
j++; | ||
k++; | ||
next[j] = k; | ||
} else k = next[k]; // 不相等的时候,需要看前面已匹配的字符串的最长公共前后缀,也就是 next[k] | ||
} | ||
} | ||
|
||
|
||
// 时间超限 | ||
/*public int strStr(String haystack, String needle) { | ||
if(needle==null||needle.length()==0){ | ||
return 0; | ||
} | ||
for(int i=0;i<haystack.length();i++){ | ||
if(isMatch(haystack,i,needle)){ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
public boolean isMatch(String haystack,int i, String needle){ | ||
int j=0; | ||
for(; i<haystack.length() && j<needle.length();i++,j++){ | ||
if(haystack.charAt(i) != needle.charAt(j)){ | ||
return false; | ||
} | ||
} | ||
if(i <= haystack.length()&& j == needle.length()){ | ||
return true; | ||
} | ||
return false; | ||
}*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.aphysia.leetcode; | ||
|
||
public class Solution29 { | ||
|
||
public static void main(String[] args) { | ||
/*System.out.println(divide(1, -1)==-1); | ||
System.out.println(divide(-1, 1)==-1); | ||
System.out.println(divide(-1, -1)==1); | ||
System.out.println(divide(-2147483648, -1)==2147483647);*/ | ||
System.out.println(divide(2147483647, 2) == 1073741823); | ||
} | ||
|
||
public static int divide(int dividend, int divisor) { | ||
if (dividend == 0) { | ||
return 0; | ||
} | ||
if (divisor == 1) { | ||
return dividend; | ||
} | ||
|
||
// 是否需要翻转符号 | ||
boolean flag = true; | ||
if (dividend > 0 && divisor > 0) { | ||
dividend = -dividend; | ||
divisor = -divisor; | ||
flag = false; | ||
} else if (dividend > 0) { | ||
dividend = -dividend; | ||
} else if (divisor > 0) { | ||
divisor = -divisor; | ||
} else { | ||
flag = false; | ||
} | ||
int result = 0; | ||
while (dividend <= divisor) { | ||
int temp = divisor; | ||
int num = 1; | ||
while (dividend < -1 && temp > (dividend >> 1)) { | ||
if (num > (Integer.MAX_VALUE >> 1)) { | ||
break; | ||
} | ||
temp = temp + temp; | ||
num = num << 1; | ||
} | ||
if (result > Integer.MAX_VALUE - num) { | ||
return Integer.MAX_VALUE; | ||
} | ||
result = result + num; | ||
dividend = dividend - temp; | ||
} | ||
return flag ? -result : result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.aphysia.leetcode; | ||
|
||
import java.util.Stack; | ||
|
||
public class Solution63 { | ||
public int maxProfit(int[] prices) { | ||
if (prices == null || prices.length == 0) | ||
return 0; | ||
Stack<Integer> stack = new Stack<>(); | ||
stack.push(prices[0]); | ||
int result = 0; | ||
for (int i = 1; i < prices.length; i++) { | ||
if (stack.peek() > prices[i]) { | ||
stack.pop(); | ||
stack.push(prices[i]); | ||
} else { | ||
result = Math.max(result, prices[i] - stack.peek()); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.aphysia.leetcode; | ||
|
||
class TreeNode { | ||
int val = 0; | ||
TreeNode left = null; | ||
TreeNode right = null; | ||
|
||
public TreeNode(int val) { | ||
this.val = val; | ||
} | ||
} | ||
|
||
public class Solution68 { | ||
|
||
public int lowestCommonAncestor(TreeNode root, int p, int q) { | ||
TreeNode result = commonAncestor(root, p, q); | ||
return result == null ? -1 : result.val; | ||
} | ||
|
||
public TreeNode commonAncestor(TreeNode root, int p, int q) { | ||
if (null == root) { | ||
return null; | ||
} | ||
if (root.val == p || root.val == q) { | ||
return root; | ||
} | ||
TreeNode left = commonAncestor(root.left, p, q); | ||
TreeNode right = commonAncestor(root.right, p, q); | ||
if (left == null) { | ||
return right; | ||
} else if (right == null) { | ||
return left; | ||
} else { | ||
return root; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.aphysia.leetcode; | ||
|
||
public class Solution72 { | ||
public int mySqrt(int n) { | ||
int left = 1; | ||
int right = n; | ||
while (left <= right) { | ||
int mid = (left + right) / 2; | ||
if (mid <= n / mid) { | ||
if ((mid + 1) > n / (mid + 1)) { | ||
return mid; | ||
} | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.