leetcode(21)
-
Single Number
SolutionGiven a non-empty array of integers nums, every element appears twice except for one. Find that single one.You must implement a solution with a linear runtime complexity and use only constant extra space. ExampleExample 1:Input: nums = [2,2,1]Output: 1Example 2:Input: nums = [4,1,2,1,2]Output: 4Example 3:Input: nums = [1]Output: 1 Explaination class Solution { public int singleNumber(..
2024.09.05 -
Rotate Array
SolutionGiven an integer array nums, rotate the array to the right by k steps, where k is non-negative. ExampleInput: nums = [1,2,3,4,5,6,7], k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4] class Solution { public void rotate(int[] nums, int k) { k %= nums.length..
2024.09.03 -
Best Time to Buy and Sell Stock II
Problem ExplanationThe problem, "Best Time to Buy and Sell Stock II," asks us to maximize the profit from multiple stock transactions, where you can buy and sell stocks on different days. However, you cannot engage in multiple transactions at the same time — you must sell before you can buy again. The goal is to develop an efficient strategy to determine the maximum profit you can achieve over a..
2024.08.31