2019年6月7日星期五

关于LDAP协议

LDAP 在网络七层结构中属于哪一层?
LDAP - 轻量级目录访问协议。属于会话层(Session Layer),在建立和管理session的时候使用。

怎样找到某个 domain 的 ldap server?
luchens-MacBook-Pro:~ luchen$ nslookup -type=srv _ldap._tcp.google.com
Server: 2001:568:ff09:10a::57
Address: 2001:568:ff09:10a::57#53
Non-authoritative answer:
_ldap._tcp.google.com service = 5 0 389 ldap.google.com.

2019年1月28日星期一

[LeetCode] 49. Group Anagrams

class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> result = new ArrayList<>();
Map<Character, Integer> characterMap;
Map<Map<Character, Integer>, List<String>> groupMap = new HashMap<>();
if (strs == null || strs.length == 0) {
return result;
}
for (String s : strs) {
characterMap = new HashMap<>();
for (Character c : s.toCharArray()) {
if (characterMap.get(c) == null) {
characterMap.put(c, 1);
} else {
characterMap.put(c, characterMap.get(c) + 1);
}
}
if (groupMap.get(characterMap) == null) {
groupMap.put(characterMap, new ArrayList<>());
}
groupMap.get(characterMap).add(s);
}
for (List<String> group : groupMap.values()) {
result.add(group);
}
return result;
}
}

2019年1月27日星期日

[LeetCode] 18. 4Sum

\(▔▽▔)/
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Set<List<Integer>> resultSet = new HashSet<>();
if (nums == null || nums.length < 4) {
return result;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
resultSet.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
left++;// Attn!
right--;
} else if (sum > target) {
right--;
} else {
left++;
}
}
}
}
for (List<Integer> solution : resultSet) {
result.add(solution);
}
return result;
}
}
view raw fourSum.java hosted with ❤ by GitHub

[LeetCode] 60. Permutation Sequence

有点想不明白,为什么要做 k = k - 1 ┑( ̄▽  ̄)┍
class Solution {
public String getPermutation(int n, int k) {
String chooseFrom = "123456789";
int[] factorial = new int[n + 1];
factorial[0] = 1;
factorial[1] = 1;
for (int i = 2; i <= n; i++) {
factorial[i] = factorial[i - 1] * i;
}
k = k - 1; // Why?
StringBuilder sb = new StringBuilder();
for (int i = n; i >= 1; i--) {
int x = k / factorial[i - 1];
k = k % factorial[i - 1];
sb.append(chooseFrom.charAt(x));
chooseFrom = chooseFrom.substring(0, x) + chooseFrom.substring(x + 1, chooseFrom.length());
}
return sb.toString();
}
}

2019年1月26日星期六

[LeetCode] 312. Burst Balloons

好难啊,不会做\(▔▽▔)/
class Solution {
public int maxCoins(int[] nums) {
// dp[i][j] - max coins could get when burst all the balloons between i to j
// dp[i][j] = max(dp[i][j], dp[i][k - 1] + nums[i - 1] * nums[k] * nums[j + 1] + dp[k + 1][j]), i <= k <= j
if (nums == null || nums.length == 0) {
return 0;
}
int[][] dp = new int[nums.length][nums.length];
for (int len = 1; len <= nums.length; len++) {
for (int i = 0; i + len <= nums.length; i++) {
int j = i + len - 1;
for (int k = i; k <= j; k++) {
int left = i == 0 ? 1 : nums[i - 1];
int right = j == nums.length - 1 ? 1 : nums[j + 1];
int current = left * nums[k] * right;
if (k - 1 >= i) {
current = current + dp[i][k - 1];
}
if (k + 1 <= j) {
current = current + dp[k + 1][j];
}
dp[i][j] = Math.max(dp[i][j], current);
}
}
}
return dp[0][nums.length - 1];
}
}
view raw maxCoins.java hosted with ❤ by GitHub

2019年1月24日星期四

[LeetCode] 58. Length of Last Word

lol,工作很繁重,脑子不是很清楚,只能拿一道简单到逆天的题目充数了 ╮( ̄▽ ̄)╭ 
class Solution {
public int lengthOfLastWord(String s) {
int end = s.length() - 1;
while (end >= 0 && s.charAt(end) == ' ') {
end--;
}
int start = end;
while (start >= 0 && s.charAt(start) != ' ') {
start--;
}
if (end >= 0) {
return end - start;
}
return 0;
}
}