본문 바로가기

Study & Edu/Algorithm7

회문(palindrome) public class Palindrome { public static boolean isPalindrome(String word) { char[] wordArr = word.toLowerCase().toCharArray(); int end = wordArr.length - 1; int mid = wordArr.length / 2; for (int i = 0; i < mid; i++) { if (wordArr[i] != wordArr[end - i]) return false; } return true; } public static void main(String[] args) { System.out.println(Palindrome.isPalindrome("Deleveled")); // level(true.. 2018. 11. 26.
[Java] String to int, int to String 형변환 int num = 123;String str = "789"; // int to StringString str1 = String.valueOf(num);String str2 = Integer.toString(num);String str3 = "" + num; System.out.println( str1 + " | " + str2 + " | " + str3 ); //123 | 123 | 123 // String to intint num1 = Integer.parseInt(str);int num2 = Integer.valueOf(str); //자바 8의 Optional을 이용하여 String을 int로 변환 int num3 = Optional.of(str).map(Integer::valueOf).get(); .. 2018. 9. 22.
[프로그래머스] 자연수 뒤집어 배열로 만들기 (3가지 방법) //자연수 뒤집어 배열로 만들기 (3가지 방법) class Solution { //solution1 (my solution) public int[] solution1(long n) { int[] answer = {}; String temp = String.valueOf(n); int len = temp.length(); answer = new int[len]; int cnt = len; for(int i=0; i 2018. 9. 16.
String to char/char[] 형변환, 혹은 그 반대로 형변환 /* * 1. String to char (스트링에서 캐릭터로 형변환) * 2. char to String (캐릭터에서 스트링으로 형변환) * 3. String to char[] (스트링에서 캐릭터형 배열로 형변환) * 4. char[] to String (캐릭터형 배열에서 스트링으로 형변환) * 5. equalsIgnoreCase (대소문자 관계없이 알파벳 체크)*/ //1. String to char String s = "a"; char c = s.charAt(0); System.out.println(s); //a String str = "abc"; char[] charArr = str.toCharArray(); System.out.println(charArr); //abc //2. char to S.. 2018. 9. 11.
[프로그래머스] 문자열 내 p와 y의 개수 (String to char array) 문제)대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.예를들어 s가 pPoooyY면 true를 return하고 Pyy라면 false를 return합니다. /* 문자열 내 p와 y의 개수 * URL: https://www.welcomekakao.com/learn/courses/30/lessons/12916 */ class Solution15 { boolean solution(String s) { boolean answer = true; char[].. 2018. 9. 11.
JAVA로 10진수 2진수 변환 (decimal to binary in JAVA) JAVA를 이용하여 10진수를 2진수로 변환하는 방법 6가지를 정리해보았다. public class BinaryTest { public static void main(String[] args) throws Exception { //case1 intToBinaryCase1(5); //case2 String result = intToBinaryCase2(5); System.out.println("case2: "+ result); //case3 intToBinaryCase3(5); //case4 System.out.println("case4(1): " + intToBinaryCase4(5,4)); System.out.println("case4(2): " + Integer.toString(5,2)); //cas.. 2017. 10. 5.
[codility] 1 BinaryGap Question1) BinaryGapFind longest sequence of zeros in binary representation of an integer. (출처 : https://codility.com/programmers/lessons/1-iterations/binary_gap/#disqus_thread) A.) import java.util.*; class Solution { public int solution(int[] A, int[] B, int[] C) { int begin = 0; int end = C.length - 1; int res = -1; while (begin 2017. 9. 12.
반응형