[LeetCode] 17.Add Binary

2021. 1. 21. 11:13

leetcode.com/problems/add-binary/submissions/

 

Add Binary - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1. Problem

Given two binary strings a and b, return their sum as a binary string.

 

Example 1:

Input: a = "11", b = "1" Output: "100"

 

Example 2:

Input: a = "1010", b = "1011" Output: "10101"

 

Constraints:

  • 1 <= a.length, b.length <= 104
  • a and b consist only of '0' or '1' characters.
  • Each string does not contain leading zeros except for the zero itself.

2. Code

package leetCode;

import java.util.ArrayList;
import java.util.List;

public class AddBinary {

	public static void main(String[] args) {
		String a = "1010";
		String b = "1011";
		System.out.println(addBinary(a,b));
	}   
	public static String addBinary(String a, String b) {
		//무조건 a가 더 길다고 가정
		if(a.length() < b.length()) {
			String temp = a;
			a = b;
			b = temp;
		}
		/*int[] arrayA = new int[a.length()];
        int[] arrayB = new int[b.length()];
		for(int i = 0; i < a.length(); i++) {
			arrayA[i] = Integer.parseInt(a.charAt(i)+"");
		}
		for(int j = 0; j < b.length(); j++) {
			arrayB[j] = Integer.parseInt(b.charAt(j)+"");
		}*/
		
		boolean up = false;
		List<Integer> arrList = new ArrayList<>();
		for(int i = 0; i < a.length(); i++) {
			if(i < b.length()) {
				int sum = Integer.parseInt(a.charAt(a.length()-1-i)+"") + Integer.parseInt(b.charAt(b.length()-1-i)+"");
				if(!up) {
					if(sum == 2) {
						System.out.println(i + " " + 1);
						up = true;
						arrList.add(0);
					} else {
						System.out.println(i + " " + 2);
						arrList.add(sum);
					}
				} else {
					if(sum >= 1) {
						System.out.println(i + " " + 3);
						arrList.add(sum-1);
					} else {
						System.out.println(i + " " + 4);
						up = false;
						arrList.add(1);
					}
				}
				if(up && i == a.length()-1) {
					System.out.println(i + " " + 5);
					arrList.add(1);
				}
			} else {
				if(!up) {
					System.out.println(i + " " + 6);
					arrList.add(Integer.parseInt(a.charAt(a.length()-1-i)+""));
				} else {
					if(Integer.parseInt(a.charAt(a.length()-1-i)+"") == 1) {
						System.out.println(i + " " + 7);
						arrList.add(0);
						if(i == a.length()-1) {
							arrList.add(1);
						}
					} else {
						System.out.println(i + " " + 8);
						up = false;
						arrList.add(1);
					}
				}
			}
		}
		String result = "";
		for(int i = 0; i < arrList.size(); i++) {
			result += ""+arrList.get(arrList.size()-i-1);
		}
		return result;
    }
}

3. Report

처음엔

int[] arrayArr = new int[a.length()]

arrayArr[i] = Integer.parseInt(a.charAt(a.length()-1-i)+"";

으로 배열을 이용해서 풀었다

String a, b를 int arrayA, arrayB로 만드는 반복을 없애면 시간이 줄어들까 싶어서

바꿔봤는데 걸리는 시간은 같았다,,

'Algorithm > 문제' 카테고리의 다른 글

[Study] 19.최대 실행 횟수 구하기  (0) 2021.01.26
[LeetCode] 18.Same Tree  (0) 2021.01.22
[LeetCode] 16.3Sum  (0) 2021.01.21
[LeetCode] 15.Insert Interval (again)  (0) 2021.01.18
[LeetCode] 14.Maximum Subarray  (0) 2021.01.18

BELATED ARTICLES

more