forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ed3557
commit f27854c
Showing
7 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution | ||
{ | ||
public: | ||
vector<string> printVertically(string s) | ||
{ | ||
stringstream ss(s); | ||
vector<string> strs; | ||
string str; | ||
|
||
while (ss >> str) strs.push_back(str); | ||
int max_len = 0; | ||
for (string& st : strs) max_len = max(max_len, (int)st.size()); | ||
|
||
vector<string> res(max_len); | ||
for (int j = 0; j < max_len; j++) | ||
{ | ||
for (string& st : strs) | ||
{ | ||
if (st.size() > j) res[j] += st[j]; | ||
else res[j] += " "; | ||
} | ||
} | ||
|
||
for (string& st : res) | ||
{ | ||
while (!st.empty() && st.back() == ' ') st.pop_back(); | ||
} | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
func printVertically(s string) []string { | ||
strs := strings.Split(s, " ") | ||
max_len := 0 | ||
for _, st := range strs { | ||
max_len = max(max_len, len(st)) | ||
} | ||
|
||
res := []string{} | ||
for j := 0; j < max_len; j++ { | ||
t, t_len := []byte{}, 0 | ||
for _, st := range strs { | ||
if len(st) > j { | ||
t = append(t, st[j]) | ||
t_len = len(t) | ||
} else { | ||
t = append(t, ' ') | ||
} | ||
} | ||
res = append(res, string(t[:t_len])) | ||
} | ||
return res | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
public List<String> printVertically(String s) { | ||
String[] strs = s.split(" "); | ||
int max_len = 0; | ||
for (String st : strs) max_len = Math.max(max_len, st.length()); | ||
|
||
List<String> res = new ArrayList(); | ||
for (int j = 0; j < max_len; j++) { | ||
StringBuilder t = new StringBuilder(); | ||
int t_len = 0; | ||
|
||
for (String st : strs) { | ||
if (st.length() > j) { | ||
t.append(st.charAt(j)); | ||
t_len = t.length(); | ||
} else t.append(" "); | ||
} | ||
t.setLength(t_len); | ||
res.add(t.toString()); | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var printVertically = function(s) { | ||
let strs = s.split(" "), max_len = 0; | ||
for (let st of strs) { | ||
max_len = Math.max(max_len, st.length); | ||
} | ||
|
||
let res = []; | ||
for (let j = 0; j < max_len; j++) { | ||
let t = "", t_len = 0; | ||
for (let st of strs) { | ||
if (st.length > j) { | ||
t += st[j]; | ||
t_len = t.length; | ||
} else t += " "; | ||
} | ||
res.push(t.substring(0, t_len)); | ||
} | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Solution: | ||
def printVertically(self, s: str) -> List[str]: | ||
return [''.join(a).rstrip() for a in itertools.zip_longest(*s.split(), fillvalue=' ')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters