Skip to content

Commit 0d5b2b7

Browse files
committed
str
1 parent fe48a15 commit 0d5b2b7

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ReverseWordsinaString;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* User: Danyang
10+
* Date: 1/28/2015
11+
* Time: 12:19
12+
*
13+
* Given an input string, reverse the string word by word.
14+
15+
For example,
16+
Given s = "the sky is blue",
17+
return "blue is sky the".
18+
*/
19+
public class Solution {
20+
public String reverseWords(String s) {
21+
List<String> lst = Arrays.asList(s.trim().split("\\s+"));
22+
Collections.reverse(lst);
23+
return lst.stream().collect(Collectors.joining(" "));
24+
}
25+
}

src/SimplifyPath/Solution.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package SimplifyPath;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* User: Danyang
10+
* Date: 1/28/2015
11+
* Time: 12:31
12+
* Given an absolute path for a file (Unix-style), simplify it.
13+
14+
For example,
15+
path = "/home/", => "/home"
16+
path = "/a/./b/../../c/", => "/c"
17+
*/
18+
public class Solution {
19+
public String simplifyPath(String path) {
20+
List<String> lst = Arrays.asList(path.split("/"));
21+
List<String> stk = new ArrayList<>();
22+
for(String elt: lst) {
23+
if(elt.equals(".")) {
24+
continue;
25+
}
26+
else if(elt.equals("..")) {
27+
if(!stk.isEmpty())
28+
stk.remove(stk.size()-1);
29+
}
30+
else if(elt.length()>0) {
31+
stk.add(elt);
32+
}
33+
}
34+
return "/"+stk.stream().collect(Collectors.joining("/"));
35+
}
36+
37+
public static void main(String[] args) {
38+
String ret = new Solution().simplifyPath("/..");
39+
assert ret.equals("/");
40+
}
41+
}

src/ZigZagConversion/Solution.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ZigZagConversion;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* User: Danyang
9+
* Date: 1/28/2015
10+
* Time: 11:24
11+
*
12+
* he string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to
13+
* display this pattern in a fixed font for better legibility)
14+
15+
P A H N
16+
A P L S I I G
17+
Y I R
18+
And then read line by line: "PAHNAPLSIIGYIR"
19+
Write the code that will take a string and make this conversion given a number of rows:
20+
21+
string convert(string text, int nRows);
22+
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
23+
*/
24+
public class Solution {
25+
/**
26+
* Notice:
27+
* 1. new ArrayList<>(n); // initialize array with length n, but not the object itself
28+
* @param s
29+
* @param nRows
30+
* @return
31+
*/
32+
public String convert(String s, int nRows) {
33+
List<StringBuilder> rows = new ArrayList<>();
34+
for(int i=0; i<nRows; i++)
35+
rows.add(new StringBuilder());
36+
if(nRows==1)
37+
return s;
38+
char[] cs = s.toCharArray();
39+
for(int i=0; i<cs.length; i++) {
40+
int pos = i%(2*nRows-2);
41+
if(pos<nRows)
42+
rows.get(pos).append(cs[i]);
43+
else
44+
rows.get(2*nRows-2-pos).append(cs[i]);
45+
}
46+
return rows.stream().map(StringBuilder::toString).collect(Collectors.joining());
47+
}
48+
49+
public static void main(String[] args) {
50+
String ret = new Solution().convert("A", 2);
51+
System.out.println(ret);
52+
}
53+
}

0 commit comments

Comments
 (0)