From 883ae702151c675907e62cc1eb4d72ed27341e9f Mon Sep 17 00:00:00 2001 From: HendSame <154903157@qq.com> Date: Sat, 13 Jun 2020 17:50:31 +0800 Subject: [PATCH] add 0520,0521,0522 solution for java --- .../0520.Detect Capital/Solution.java | 24 ++++++++++++ .../Solution.java | 7 ++++ .../Solution.java | 38 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 solution/0500-0599/0520.Detect Capital/Solution.java create mode 100644 solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.java create mode 100644 solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java diff --git a/solution/0500-0599/0520.Detect Capital/Solution.java b/solution/0500-0599/0520.Detect Capital/Solution.java new file mode 100644 index 0000000000000..b662e3de1d3bc --- /dev/null +++ b/solution/0500-0599/0520.Detect Capital/Solution.java @@ -0,0 +1,24 @@ +class Solution { + public boolean detectCapitalUse(String word) { + char[] cs = word.toCharArray(); + int upper = 0; + int lower = 0; + for (int i = 0; i < cs.length; i++) { + if (cs[i] >= 'a') { + lower++; + } else { + upper++; + } + } + if (upper == cs.length) { + return true; + } + if (lower == cs.length) { + return true; + } + if (upper == 1 && cs[0] < 'a') { + return true; + } + return false; + } +} diff --git a/solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.java b/solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.java new file mode 100644 index 0000000000000..7756ec7a0c4cb --- /dev/null +++ b/solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.java @@ -0,0 +1,7 @@ +class Solution { + public int findLUSlength(String a, String b) { + if (a.equals(b)) + return -1; + return Math.max(a.length(), b.length()); + } +} diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java new file mode 100644 index 0000000000000..3c69fbd286d01 --- /dev/null +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java @@ -0,0 +1,38 @@ +class Solution { + public int findLUSlength(String[] strs) { + int res = -1; + if (strs == null || strs.length == 0) { + return res; + } + if (strs.length == 1) { + return strs[0].length(); + } + // 两两比较 + // 1、存在子串,直接不比较后面的字符串 + // 2、不存在子串,判断当前字符串是否是最长的字符串 + for (int i = 0, j; i < strs.length; i++) { + for (j = 0; j < strs.length; j++) { + if (i == j) { + continue; + } + // 根据题意,子串 可以 不是 原字符串中 连续的子字符串 + if (isSubsequence(strs[i], strs[j])) { + break; + } + } + if (j == strs.length) { + res = Math.max(res, strs[i].length()); + } + } + return res; + } + + public boolean isSubsequence(String x, String y) { + int j = 0; + for (int i = 0; i < y.length() && j < x.length(); i++) { + if (x.charAt(j) == y.charAt(i)) + j++; + } + return j == x.length(); + } +}