Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100
🌟Coding:
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { publicintremoveElement(int[] nums, int val) { intflag=0; for(inti=0 ; i < nums.length; i++){ if(nums[i] != val){ nums[flag] = nums[i]; flag ++; } } return flag; } }
344. Reverse String
Easy
Write 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.
Example :
1 2
Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return trueif it is a palindrome, orfalseotherwise.
Example :
1 2 3
Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome.
Given a 1-indexed array of integers numbers that is already *sorted in non-decreasing order*, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
Return the indices of the two numbers,index1andindex2, added by one as an integer array[index1, index2]of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
Example 1:
1 2 3
Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
🌟Coding:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicint[] twoSum(int[] numbers, int target) { for (inti=0 ; i < numbers.length; i ++){ for(intj= numbers.length - 1; j > i; j --){ if (numbers[i] + numbers[j] == target) returnnewint[] {i+1,j+1}; if( numbers[i] + numbers[j] < target) // without this if sentence then the time complexity cannot pass the test break; } }
returnnewint[]{0}; } }
⭕The break statement can be used to jump out of a loop.
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
2109. Adding Spaces to a String
Medium
Example :
1 2 3 4 5
Input: s = "icodeinpython", spaces = [1,5,7,9] Output: "i code in py thon" Explanation: The indices 1, 5, 7, and 9 correspond to the underlined characters in "icodeinpython". We then place spaces before those characters.
classSolution { //remove extra space publicstatic StringBuilder removeSpace(String s){ StringBuildersb=newStringBuilder(); intstart=0 ; intend= s.length() - 1; // remove all space in the front and back of the array while(s.charAt(start) ==(' ')) start++; while(s.charAt(end) ==(' ')) end--; //remove extra space in the middle of the array for(inti= start ; i <= end; i++){ if(s.charAt(i) != ' '){ sb.append(s.charAt(i)); } if(s.charAt(i) == ' ' && s.charAt(i-1) != ' '){ sb.append(" ");
} } return sb; } // reverse from start to end publicstaticvoidreverse(StringBuilder sb, int start, int end){ //notice here could be void
public String reverseWords(String s) { StringBuildertemp= removeSpace(s); reverse(temp, 0 , temp.length()-1); // reverse each single word intstart=0; intend=1; intn= temp.length(); while (start < n) { while (end < n && temp.charAt(end) != ' ') { end++; } reverse(temp, start, end - 1); start = end + 1; end = start + 1; } System.out.println(temp); return temp.toString(); } //MY FIRST TRY WHICH IS WRONG // reverse each single word // int start = 0; // int end = 0; // for( int i = 0 ; i < temp.length(); i ++){ // if(temp.charAt(i)!= ' ' ){ // end = i; // end equals to the last charater which is right before the space // } // if(temp.charAt(i) == ' ' ){ //这里错了 可以改成if (temp.charAt(i) == ' ' || i == temp.length() - 1) // reverse(temp, start, end); // start = i + 1; //start move to one after the end // } // } }
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
1 2 3 4 5 6 7 8
Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order of the output and the order of the triplets does not matter.