Skip to content

Instantly share code, notes, and snippets.

@lizuyao2010
Created March 17, 2016 23:43
Show Gist options
  • Select an option

  • Save lizuyao2010/4db4a5227e3584733f85 to your computer and use it in GitHub Desktop.

Select an option

Save lizuyao2010/4db4a5227e3584733f85 to your computer and use it in GitHub Desktop.
Algorithms
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ClientPropertiesManager">
<properties class="javax.swing.AbstractButton">
<property name="hideActionText" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JComponent">
<property name="html.disable" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JEditorPane">
<property name="JEditorPane.w3cLengthUnits" class="java.lang.Boolean" />
<property name="JEditorPane.honorDisplayProperties" class="java.lang.Boolean" />
<property name="charset" class="java.lang.String" />
</properties>
<properties class="javax.swing.JList">
<property name="List.isFileList" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JPasswordField">
<property name="JPasswordField.cutCopyAllowed" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JSlider">
<property name="Slider.paintThumbArrowShape" class="java.lang.Boolean" />
<property name="JSlider.isFilled" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JTable">
<property name="Table.isFileList" class="java.lang.Boolean" />
<property name="JTable.autoStartsEdit" class="java.lang.Boolean" />
<property name="terminateEditOnFocusLost" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JToolBar">
<property name="JToolBar.isRollover" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JTree">
<property name="JTree.lineStyle" class="java.lang.String" />
</properties>
<properties class="javax.swing.text.JTextComponent">
<property name="caretAspectRatio" class="java.lang.Double" />
<property name="caretWidth" class="java.lang.Integer" />
</properties>
</component>
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="mavenHome" value="Bundled (Maven 3)" />
</MavenGeneralSettings>
</option>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/test.iml" filepath="$PROJECT_DIR$/test.iml" />
</modules>
</component>
</project>
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by lizuyao2010 on 3/14/16.
*/
public class Combinations {
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(candidates == null || candidates.length==0)
return res;
Arrays.sort(candidates);
helper(candidates,0,target,new ArrayList<Integer>(),res);
return res;
}
private void helper(int[] candidates, int start, int target, ArrayList<Integer> item,
ArrayList<ArrayList<Integer>> res)
{
if(target<0)
return;
if(target==0)
{
res.add(new ArrayList<Integer>(item));
return;
}
for(int i=start;i<candidates.length;i++)
{
if(i>0 && candidates[i]==candidates[i-1])
continue;
item.add(candidates[i]);
helper(candidates,i,target-candidates[i],item,res);
item.remove(item.size()-1);
}
}
public static void main(String[] args)
{
Combinations c=new Combinations();
System.out.print(c.combinationSum(new int[]{2,2,1,1},2));
}
}
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Created by lizuyao2010 on 3/17/16.
*/
public class FirstNonRepeat {
public static char firstNonRepeat(String s)
{
Map<Character,Integer> m=new LinkedHashMap<>();
for (char c: s.toCharArray())
{
if (!m.containsKey(c))
{
m.put(c,1);
}
else
{
m.put(c,m.get(c)+1);
}
}
for (char c: m.keySet())
{
if (m.get(c)==1)
return c;
}
return ' ';
}
public static void main(String[] args)
{
char c=firstNonRepeat("daabbc");
System.out.println(c);
}
}
/**
* Created by lizuyao2010 on 3/10/16.
*/
public class Strstr {
public static void main(String[] args)
{
String s1="bcde";
String s2="cd";
int index=findNeedle(s1, s2);
System.out.println(index);
}
private static int findNeedle(String s1, String s2) {
if (s2.length()==0 || s2==null) return 0;
for (int i=0;i<=s1.length()-s2.length();i++)
{
if (s1.substring(i,i+s2.length()).equals(s2))
return i;
}
return -1;
}
}
import java.util.Stack;
/**
* Created by lizuyao2010 on 3/10/16.
*/
class TreeNode
{
TreeNode left;
TreeNode right;
char val;
public TreeNode(char val)
{
this.val=val;
left=null;
right=null;
}
public String serialize(){
StringBuilder sb = new StringBuilder();
serialize(this, sb);
return sb.toString();
}
private void serialize(TreeNode x, StringBuilder sb){
if (x == null) {
sb.append("# ");
} else {
sb.append(x.val + " ");
serialize(x.left, sb);
serialize(x.right, sb);
}
}
}
public class TernaryExpressionToBinaryTree {
public static void main(String[] args)
{
TreeNode root=solve("a?b?c:d:e");
System.out.print(root.serialize());
}
public static TreeNode solve(String s)
{
Stack<TreeNode> stack=new Stack<>();
stack.add(new TreeNode(s.charAt(0)));
for (int i=1;i<s.length()-1;i+=2)
{
char c=s.charAt(i);
char next=s.charAt(i+1);
if (c==':')
{
TreeNode r=new TreeNode(next);
TreeNode l=stack.pop();
stack.peek().left=l;
stack.peek().right=r;
}
else if(c=='?')
{
stack.push(new TreeNode(next));
}
}
return stack.peek();
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by lizuyao2010 on 3/15/16.
*/
public class testRegex {
public static void main(String[] args)
{
// String pattern="(\\d{2})/(\\d{4})";
// String pattern="(\\d{1,2})\\W{1,3}(\\d{4}|\\d{2})";
String pattern="(\\d{1,2})\\s*[/-]\\s*(\\d{4}|\\d{2})";
Pattern r = Pattern.compile(pattern);
String input="12/99";
Matcher m = r.matcher(input);
if (m.find())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
}
/**
* Created by lizuyao2010 on 3/8/16.
*/
public class TestSplit {
public static void main(String[] args)
{
String s="AB|EF|CD";
String[] list=s.split("\\|");
System.out.println(list[0]);
}
}
import java.util.*;
/**
* Created by lizuyao2010 on 3/10/16.
*/
class Word {
String val;
int freq;
public Word(String val,int freq)
{
this.val=val;
this.freq=freq;
}
@Override
public String toString() {
return "Word{" +
"val='" + val + '\'' +
", freq=" + freq +
'}';
}
}
public class TopK {
public static void main(String[] args)
{
int k=2;
PriorityQueue<Word> pq= new PriorityQueue<>(k, new Comparator<Word>() {
@Override
public int compare(Word w1, Word w2) {
return Integer.compare(w1.freq, w2.freq);
}
});
Word w1=new Word("a",2);
Word w2=new Word("b",4);
Word w3=new Word("c",3);
Word w4=new Word("d",5);
List<Word> wordsStream = new ArrayList<>();
wordsStream.add(w1);
wordsStream.add(w2);
wordsStream.add(w3);
wordsStream.add(w4);
for (Word w: wordsStream) {
if (pq.size() < k) {
pq.add(w);
}
else
{
if (w.freq>pq.peek().freq)
{
pq.poll();
pq.add(w);
}
}
}
Stack<Word> wordsStack=new Stack<>();
while(!pq.isEmpty())
{
Word c=pq.poll();
wordsStack.add(c);
// System.out.println(c);
}
while(!wordsStack.isEmpty())
{
System.out.println(wordsStack.pop());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
@lizuyao2010
Copy link
Author

initial code base

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment