Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2/6 타겟넘버 #50

Open
skarltjr opened this issue Feb 6, 2021 · 0 comments
Open

2/6 타겟넘버 #50

skarltjr opened this issue Feb 6, 2021 · 0 comments
Labels

Comments

@skarltjr
Copy link
Owner

skarltjr commented Feb 6, 2021

package algo;


/**
 * 주어진 numbers들로 target을 만드는 방법 수를 리턴
 * */
public class Solution {
    int ans = 0;
    public int solution(int[] numbers, int target) {
        int count = 0; // 수행한 numbers 중 숫자 개수 . 이게 length랑 같으면 종료
        int total = 0; // 총합이 target이면 정답


        //  (+/-) ㅁ (+/-) ㅁ (+/-) ㅁ ....
        // +일때
        bfs(numbers,total+numbers[0],target,count+1);
        // -일때
        bfs(numbers,total-numbers[0],target,count+1);


        return ans;
    }

    public void bfs(int[] numbers,int total, int target, int count)
    {
        if (count == numbers.length)
        {
            if (total == target) {
                ans++;
                return;
            }else {
                return;
            }
        }

        bfs(numbers, total + numbers[count], target, count + 1);
        bfs(numbers, total - numbers[count], target, count + 1);
    }

}



@skarltjr skarltjr changed the title 알고리즘 2/6 타겟넘버 Feb 6, 2021
@skarltjr skarltjr added the Daily label Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant