add q14/q16/q20

pull/6/head
yuanguangxin 5 years ago
parent bda4ca6a1b
commit 7ca606bcba

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="add q3/q8/q11">
<change afterPath="$PROJECT_DIR$/src/q6/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q6/f2/Solution.java" afterDir="false" />
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/q14/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q16/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q20/Solution.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -73,13 +74,13 @@
<recent name="$PROJECT_DIR$/src/q206/f1" />
</key>
</component>
<component name="RunManager" selected="Application.Solution (2)">
<configuration name="Solution (3)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q3.Solution" />
<component name="RunManager" selected="Application.Solution">
<configuration name="Solution (1)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q14.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q3.*" />
<option name="PATTERN" value="q14.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@ -87,12 +88,12 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution (4)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q6.Solution" />
<configuration name="Solution (2)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q16.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q6.*" />
<option name="PATTERN" value="q16.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@ -100,12 +101,12 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution (5)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q11.Solution" />
<configuration name="Solution (4)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q6.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q11.*" />
<option name="PATTERN" value="q6.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@ -113,12 +114,12 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q8.Solution" />
<configuration name="Solution (5)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q11.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q8.*" />
<option name="PATTERN" value="q11.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@ -127,11 +128,11 @@
</method>
</configuration>
<configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q6.Solution" />
<option name="MAIN_CLASS_NAME" value="q20.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q6.*" />
<option name="PATTERN" value="q20.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@ -167,11 +168,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Solution" />
<item itemvalue="Application.Solution (2)" />
<item itemvalue="Application.Solution (1)" />
<item itemvalue="Application.Solution (2)" />
<item itemvalue="Application.Solution (4)" />
<item itemvalue="Application.Solution (3)" />
<item itemvalue="Application.Solution (5)" />
<item itemvalue="Application.Solution" />
</list>
</recent_temporary>
</component>
@ -209,7 +210,7 @@
<workItem from="1579695121386" duration="2796000" />
<workItem from="1579758668363" duration="319000" />
<workItem from="1579759214129" duration="10251000" />
<workItem from="1580044287086" duration="30269000" />
<workItem from="1580044287086" duration="40107000" />
</task>
<task id="LOCAL-00001" summary="add q5/q7/q172">
<created>1580045439607</created>
@ -232,7 +233,14 @@
<option name="project" value="LOCAL" />
<updated>1580235186067</updated>
</task>
<option name="localTasksCounter" value="4" />
<task id="LOCAL-00004" summary="add q6">
<created>1580320003660</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1580320003660</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -242,65 +250,66 @@
<MESSAGE value="add q5/q7/q172" />
<MESSAGE value="add q9" />
<MESSAGE value="add q3/q8/q11" />
<option name="LAST_COMMIT_MESSAGE" value="add q3/q8/q11" />
<MESSAGE value="add q6" />
<option name="LAST_COMMIT_MESSAGE" value="add q6" />
</component>
<component name="WindowStateProjectService">
<state x="533" y="166" key="#com.intellij.ide.util.MemberChooser" timestamp="1578548165321">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="533" y="166" key="#com.intellij.ide.util.MemberChooser/0.0.1440.900@0.0.1440.900" timestamp="1578548165321" />
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1580235238743">
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1580320003455">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1580235238743" />
<state width="1398" height="176" key="GridCell.Tab.0.bottom" timestamp="1580319226409">
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1580320003455" />
<state width="1398" height="165" key="GridCell.Tab.0.bottom" timestamp="1580395374914">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="176" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1580319226409" />
<state width="1398" height="165" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1580395374914" />
<state width="1398" height="267" key="GridCell.Tab.0.bottom/0.23.1440.797@0.23.1440.797" timestamp="1580307317596" />
<state width="1398" height="176" key="GridCell.Tab.0.center" timestamp="1580319226407">
<state width="1398" height="165" key="GridCell.Tab.0.center" timestamp="1580395374912">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="176" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1580319226407" />
<state width="1398" height="165" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1580395374912" />
<state width="1398" height="267" key="GridCell.Tab.0.center/0.23.1440.797@0.23.1440.797" timestamp="1580307317589" />
<state width="1398" height="176" key="GridCell.Tab.0.left" timestamp="1580319226406">
<state width="1398" height="165" key="GridCell.Tab.0.left" timestamp="1580395374911">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="176" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1580319226406" />
<state width="1398" height="165" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1580395374911" />
<state width="1398" height="267" key="GridCell.Tab.0.left/0.23.1440.797@0.23.1440.797" timestamp="1580307317585" />
<state width="1398" height="176" key="GridCell.Tab.0.right" timestamp="1580319226408">
<state width="1398" height="165" key="GridCell.Tab.0.right" timestamp="1580395374913">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="176" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1580319226408" />
<state width="1398" height="165" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1580395374913" />
<state width="1398" height="267" key="GridCell.Tab.0.right/0.23.1440.797@0.23.1440.797" timestamp="1580307317594" />
<state width="1398" height="304" key="GridCell.Tab.1.bottom" timestamp="1580303064431">
<state width="1398" height="304" key="GridCell.Tab.1.bottom" timestamp="1580395301686">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="304" key="GridCell.Tab.1.bottom/0.0.1440.900@0.0.1440.900" timestamp="1580303064431" />
<state width="1398" height="304" key="GridCell.Tab.1.bottom/0.0.1440.900@0.0.1440.900" timestamp="1580395301686" />
<state width="1398" height="303" key="GridCell.Tab.1.bottom/0.23.1440.797@0.23.1440.797" timestamp="1579765319366" />
<state width="1398" height="304" key="GridCell.Tab.1.center" timestamp="1580303064428">
<state width="1398" height="304" key="GridCell.Tab.1.center" timestamp="1580395301684">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="304" key="GridCell.Tab.1.center/0.0.1440.900@0.0.1440.900" timestamp="1580303064428" />
<state width="1398" height="304" key="GridCell.Tab.1.center/0.0.1440.900@0.0.1440.900" timestamp="1580395301684" />
<state width="1398" height="303" key="GridCell.Tab.1.center/0.23.1440.797@0.23.1440.797" timestamp="1579765319364" />
<state width="1398" height="304" key="GridCell.Tab.1.left" timestamp="1580303064428">
<state width="1398" height="304" key="GridCell.Tab.1.left" timestamp="1580395301684">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="304" key="GridCell.Tab.1.left/0.0.1440.900@0.0.1440.900" timestamp="1580303064428" />
<state width="1398" height="304" key="GridCell.Tab.1.left/0.0.1440.900@0.0.1440.900" timestamp="1580395301684" />
<state width="1398" height="303" key="GridCell.Tab.1.left/0.23.1440.797@0.23.1440.797" timestamp="1579765319363" />
<state width="1398" height="304" key="GridCell.Tab.1.right" timestamp="1580303064429">
<state width="1398" height="304" key="GridCell.Tab.1.right" timestamp="1580395301685">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="304" key="GridCell.Tab.1.right/0.0.1440.900@0.0.1440.900" timestamp="1580303064429" />
<state width="1398" height="304" key="GridCell.Tab.1.right/0.0.1440.900@0.0.1440.900" timestamp="1580395301685" />
<state width="1398" height="303" key="GridCell.Tab.1.right/0.23.1440.797@0.23.1440.797" timestamp="1579765319365" />
<state x="221" y="88" key="SettingsEditor" timestamp="1578704600210">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="221" y="88" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1578704600210" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1580235187213">
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1580320007370">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1580235187213" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1580320007370" />
<state x="378" y="207" width="683" height="486" key="find.popup" timestamp="1578496174047">
<screen x="0" y="0" width="1440" height="900" />
</state>

@ -0,0 +1,35 @@
package q14;
/**
* o(n)
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) {
return "";
}
if (strs.length == 1) {
return strs[0];
}
String pre = "";
int i = 0;
while (true) {
if (strs[0].length() == i) {
return pre;
}
char temp = strs[0].charAt(i);
for (int k = 1; k < strs.length; k++) {
if (strs[k].length() == i || temp != strs[k].charAt(i)) {
return pre;
}
}
pre += temp;
i++;
}
}
public static void main(String[] args) {
String[] s = new String[]{"c", "c"};
System.out.println(new Solution().longestCommonPrefix(s));
}
}

@ -0,0 +1,46 @@
package q16;
import java.util.Arrays;
/**
* q15 + o(n^2)
*/
public class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums.length < 3) {
return 0;
}
Arrays.sort(nums);
int rs = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
int c = sum - target;
if (Math.abs(c) < Math.abs(rs - target)) {
rs = sum;
}
if (c == 0) {
return target;
} else if (c > 0) {
right--;
} else {
left++;
}
}
}
return rs;
}
public static void main(String[] args) {
int[] a = new int[]{-3, -2, -5, 3, -4};
System.out.println(new Solution().threeSumClosest(a, -1));
}
}

@ -0,0 +1,37 @@
package q20;
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char t = s.charAt(i);
if (t == '(' || t == '[' || t == '{') {
stack.push(t);
} else {
if (stack.empty()) {
return false;
}
if (t == ')') {
if (stack.pop() != '(') {
return false;
}
} else if (t == ']') {
if (stack.pop() != '[') {
return false;
}
} else {
if (stack.pop() != '{') {
return false;
}
}
}
}
return stack.empty();
}
public static void main(String[] args) {
System.out.println(new Solution().isValid("()"));
}
}
Loading…
Cancel
Save