java/javaAlgorithm(16)
-
[Leetcode]1470. Shuffle the Array(O)
1. problem : https://leetcode.com/problems/shuffle-the-array/description/ 2. solution 1 : class Solution { public int[] shuffle(int[] nums, int n) { int[] ans = new int[nums.length]; for (int i = 0; i ans짝수번째는 nums [i]를 ans홀수번째는 nums [i+n]을 설정해 주면 된다. (은근히 헷갈림) 3. solution 2 : class Solution { public int[] shuffle(int[] nums, int n) { int[] temp = new int[2*n]; ..
2024.07.27 -
[Leetcode]300. Longest Increasing Subsequence(x)
1. problem : https://leetcode.com/problems/longest-increasing-subsequence/description/ 2. solution 1 : import java.util.Arrays;class Solution { public int lengthOfLIS(int[] nums) { int n = nums.length; int[] dp = new int[n]; Arrays.fill(dp, 1); // 각 위치에서 최소 길이는 1 for (int i =1;i nums[j]) { dp[i] = Math.max(dp[j]+1,dp[i]); } ..
2024.07.27 -
[Leetcode]1512. Number of Good Pairs
1. problem : https://leetcode.com/problems/number-of-good-pairs/ 2. solution 1 : public class Solution { public int numIdenticalPairs(int[] nums) { int ans = 0; int[] cnt = new int[101]; for (int num : nums) { ans += cnt[num]++; } return ans; }}이 문제의 solution은 leetcode의 다른 사람이 작성한 solution이다. 이 solution은 매우 특이하다. 그 이유는 cnt라는 101개의 원소가 들어갈 수 있는 ..
2024.07.26 -
[Leetcode]2011. Final Value of Variable After Performing Operations
1. problem : https://leetcode.com/problems/final-value-of-variable-after-performing-operations/description/ 2. solution 1 : class Solution { public int finalValueAfterOperations(String[] operations) { int x = 0; for (int i = 0; i java라는 언어를 배우는 도중, 언어의 이해를 높이기 위해 algorithm 문제도 같이 풀고 있다. 이 문제는 String[]배열에 들어있는 원소에 "+"가 들어있다면 x값을 1 더해준다. 아니라면 -1 해준다. 이때, contains라는 method가 사용된다. Solu..
2024.07.26