반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- maybe not public or not valid?
- AWS CLI
- yum install java
- springboot
- easy
- java 1.8 11
- Java 1.8
- mac os git error
- 스프링부트테스트
- Medium
- parse
- java version
- java
- el1008e
- xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools)
- No tests found for given includes
- LeetCode
- no sources given
- error
- log error
- property or field 'jobparameters' cannot be found on object of type
- java 여러개 버전
- JUnit
- springboottest
- java 11
- java 버전 변경
- querydsl no sources given
- OpenFeign
- aws
- springbatch error
Archives
- Today
- Total
쩨이엠 개발 블로그
[ Leetcode ] 6. ZigZag Conversion - Java 본문
728x90
반응형
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Example 3:
Input: s = "A", numRows = 1 Output: "A"
Constraints:
- 1 <= s.length <= 1000
- s consists of English letters (lower-case and upper-case), ',' and '.'.
- 1 <= numRows <= 1000
Solution
class Solution {
public String convert(String s, int numRows) {
if(s.length() == numRows){
return s;
}
if(numRows == 1){
return s;
}
String[] rowStr = new String[numRows];
for(int i=0; i< numRows; i++){
rowStr[i] = "";
}
String[] strs = s.split("");
int rowNum = 0;
boolean isPlus = true;
for(String str : strs){
rowStr[rowNum] += str;
if(isPlus){
rowNum++;
}else{
rowNum--;
}
if(rowNum == numRows-1) {
isPlus = false;
}
if(rowNum == 0) {
isPlus = true;
}
}
String answer = "";
for(String str : rowStr){
answer += str;
}
return answer;
}
}
728x90
반응형
'개발 > Programming' 카테고리의 다른 글
[ Leetcode ] 8. String to Integer (atoi) - Java (0) | 2021.03.09 |
---|---|
[ Leetcode ] 9. Palindrome Number - Java (0) | 2021.03.08 |
[ Leetcode ] 1669. Merge In Between Linked Lists - Java (0) | 2021.02.13 |
[ Leetcode ] 148. Sort List - Java (0) | 2021.02.10 |
[ Leetcode ] 324. Wiggle Sort II - Java (0) | 2021.02.08 |
Comments