[알고리즘] Reverse String
주어진 문자를 역순으로 반환하는 작성하라
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
class Solution { public String reverseString(String s) { char[] word = s.toCharArray(); char[] newword = new char[word.length]; for(int i=word.length-1; i>=0; i--){ newword[(word.length-1)-i] = word[i]; } return new String(newword); } // public String reverseString(String s) { // String reverseString = ""; // for(int i=s.length()-1; i>=0; i--){ // reverseString += s.substring(i,i+1); // } // return reverseString; // } }
최근 댓글