codingtest(12)
-
Reverse String
SolutionWrite a function that reverses a string. The input string is given as an array of characters s.You must do this by modifying the input array in-place with O(1) extra memory. ExamplesExample 1:Input: s = ["h","e","l","l","o"]Output: ["o","l","l","e","h"] Example 2:Input: s = ["H","a","n","n","a","h"]Output: ["h","a","n","n","a","H"] Explanationclass Solution { public void reverseStri..
2024.10.06 -
Two Sum
SolutionGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order. Example Explanationclass Solution { public int[] twoSum(int[] nums, int target) { Map map = new HashMap(); ..
2024.09.24 -
Move Zeroes
SolutionGiven an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.Note that you must do this in-place without making a copy of the array. Examples Explanationclass Solution { public void moveZeroes(int[] nums) { int idx = 0; for(int i = 0; i Create a variable, idx, to keep track of the position where the next non-..
2024.09.23 -
Plus One
SolutionYou are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.Increment the large integer by one and return the resulting array of digits. Examples Explanationclass Solution { public..
2024.09.20 -
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