给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。
请你 合并nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。
**注意:**最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。
解法:
合并之后,直接排序 Arrays.sort(nums1);
新建数组,用双指针拷贝到新数组中,最后把数组拷贝回原来的nums1.
双指针从后面开始,这样就有位置放多余的元素,不用新建数组。
classSolution{publicvoidmerge(int[]nums1,intm,int[]nums2,intn){int p1 = m -1, p2 = n -1;int tail = m + n -1;int cur;while(p1 >=0|| p2 >=0){if(p1 ==-1){ cur = nums2[p2--];}elseif(p2 ==-1){ cur = nums1[p1--];}elseif(nums1[p1]> nums2[p2]){ cur = nums1[p1--];}else{ cur = nums2[p2--];} nums1[tail--]= cur;}}}
class Solution {
public int removeElement(int[] nums, int val) {
int n = nums.length;
int left = 0;
for (int right = 0; right < n; right++) {
if (nums[right] != val) {
nums[left] = nums[right];
left++;
}
}
return left;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
int left = 0;
int right = nums.length;
while (left < right) {
if (nums[left] == val) {
nums[left] = nums[right - 1];
right--;
} else {
left++;
}
}
return left;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
int fast = 1, slow = 1;
while (fast < n) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n <= 2) {
return n;
}
int slow = 2, fast = 2;
while (fast < n) {
if (nums[slow - 2] != nums[fast]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
}
class Solution {
public int majorityElement(int[] nums) {
int count = 0;
Integer candidate = null;
for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}
return candidate;
}
}
class Solution {
public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start += 1;
end -= 1;
}
}
}
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
int[] newArr = new int[n];
for (int i = 0; i < n; ++i) {
newArr[(i + k) % n] = nums[i];
}
System.arraycopy(newArr, 0, nums, 0, n);
}
}
public class Solution {
public int maxProfit(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}
class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
int n = prices.length;
for ( int i=1; i<n; i++){
ans += Math.max(0, prices[i]-prices[i-1]);
}
return ans;
}
}
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < n; ++i) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[n - 1][0];
}
}
class Solution {
public boolean canJump(int[] nums) {
int n = nums.length;
int rightmost=0;
for (int i=0;i< n; i++){
if(i<= rightmost){
rightmost = Math.max(i+nums[i], rightmost);
if(rightmost >= n-1){
return true;
}
}
}
return false;
}
}
class Solution {
public int jump(int[] nums) {
int length= nums.length;
int end =0;
int maxPosition=0;
int steps=0;
for ( int i=0;i< length-1; i++){
maxPosition = Math.max(maxPosition, i+nums[i]);
if(i ==end ){
end = maxPosition;
steps++;
}
}
return steps;
}
}
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int h = 0, i = citations.length - 1;
//考虑什么时候h需要加一,找到了一篇被引用了至少 h+1 次的论文
//为什么是citations[i] > h,而不是>=呢?考虑只有1个元素的情况,citations[0]=0,h=0,得到的结果应该是0而不是1。
//如果当前 H 指数为 h 并且在遍历过程中找到当前值 citations[i]>h,则说明我们找到了一篇被引用了至少 h+1 次的论文,所以将现有的 h值加 1
while (i >= 0 && citations[i] > h) {
h++;
i--;
}
return h;
}
}
public class Solution {
public int hIndex(int[] citations) {
int n = citations.length, tot = 0;
int[] counter = new int[n + 1];
for (int i = 0; i < n; i++) {
//对于引用次数超过论文发表数的情况,我们可以将其按照总的论文发表数来计算即可
if (citations[i] >= n) {
counter[n]++;
} else {
counter[citations[i]]++;
}
}
for (int i = n; i >= 0; i--) {
tot += counter[i];
if (tot >= i) {
return i;
}
}
return 0;
}
}
class Solution {
public int[] productExceptSelf(int[] nums) {
int length = nums.length;
int[] L = new int[length];
int[] R = new int[length];
int[] answner = new int[length];
L[0]=1;
for (int i=1; i< length; i++){
L[i]= nums[i-1] * L[i-1];
}
R[length-1]=1;
for ( int i = length-2; i>=0; i--){
R[i] = nums[i+1]* R[i+1];
}
for (int i=0; i< length; i++){
answner[i]=L[i] * R[i];
}
return answner;
}
}
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
int i = 0;
while (i < n) {
int sumOfGas = 0, sumOfCost = 0;
//cnt是本轮触发可以走的步数
int cnt = 0;
while (cnt < n) {
int j = (i + cnt) % n;
sumOfGas += gas[j];
sumOfCost += cost[j];
if (sumOfCost > sumOfGas) {
break;
}
cnt++;
}
if (cnt == n) {
return i;
} else {
i = i + cnt + 1;
}
}
return -1;
}
}
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int sum = 0;
int min = Integer.MAX_VALUE;
int minIndex = -1;
for(int i = 0; i < gas.length; i++){
sum = sum + gas[i] - cost[i];
if(sum < min && sum < 0){
min = sum;
minIndex = i;
}
}
if(sum < 0) return -1;
return (minIndex + 1 )%gas.length;
}
}
class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
int[] left = new int[n];
for ( int i=0; i< n ; i++){
if(i>0 && ratings[i] > ratings[i-1]){
left[i] = left[i-1]+1;
}else{
left[i]=1;
}
}
int right=0, ret=0;
for ( int i=n-1; i>=0; i--){
if(i < n-1&& ratings[i] > ratings[i+1]){
right++;
}else{
right=1;
}
ret += Math.max(left[i], right);
}
return ret;
}
}
class Solution {
public int trap(int[] height) {
int n = height.length;
if(n==0){
return 0;
}
int[] leftMax = new int[n];
leftMax[0] = height[0];
for(int i=1; i< n; i++){
leftMax[i] = Math.max(leftMax[i-1], height[i]);
}
int[] rightMax= new int[n];
rightMax[n-1] = height[n-1];
for(int i=n-2; i >=0 ; i--){
rightMax[i] = Math.max(rightMax[i+1], height[i]);
}
int ans=0;
for(int i=0;i< n; i++){
ans += Math.min(leftMax[i], rightMax[i]) - height[i];
}
return ans;
}
}
class Solution {
public int trap(int[] height) {
int ans = 0;
Deque<Integer> stack = new LinkedList<Integer>();
int n = height.length;
for (int i = 0; i < n; ++i) {
//判断stack中是否还有小于当前值的元素,保证单调栈的单调性
while (!stack.isEmpty() && height[i] > height[stack.peek()]) {
int top = stack.pop();
if (stack.isEmpty()) {
break;
}
//面积等于两边的距离乘以(两边的最小高度-当前的高度)
int left = stack.peek();
int currWidth = i - left - 1;
int currHeight = Math.min(height[left], height[i]) - height[top];
ans += currWidth * currHeight;
}
stack.push(i);
}
return ans;
}
}
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
int count = strs.length;
for (int i = 1; i < count; i++) {
prefix = longestCommonPrefix(prefix, strs[i]);
if (prefix.length() == 0) {
break;
}
}
return prefix;
}
public String longestCommonPrefix(String str1, String str2) {
int length = Math.min(str1.length(), str2.length());
int index = 0;
while (index < length && str1.charAt(index) == str2.charAt(index)) {
index++;
}
return str1.substring(0, index);
}
}
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int length = strs[0].length();
int count = strs.length;
for (int i = 0; i < length; i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < count; j++) {
//1.到达字符串长度 2.当前字符不相等
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
}
class Solution {
public String reverseWords(String s) {
// 除去开头和末尾的空白字符
s = s.trim();
// 正则匹配连续的空白字符作为分隔符分割
List<String> wordList = Arrays.asList(s.split("\\s+"));
Collections.reverse(wordList);
return String.join(" ", wordList);
}
}
class Solution {
public String reverseWords(String s) {
s = s.trim();
Deque<String> d = new ArrayDeque<String>();
StringBuilder word = new StringBuilder();
int left =0, right=s.length()-1;
while(left <=right){
char c = s.charAt(left);
if(word.length()!=0 && c==' '){
d.offerFirst(word.toString());
word.setLength(0);
}else if(c !=' '){
word.append(c);
}
left++;
}
d.offerFirst(word.toString());
return String.join(" ",d);
}
}
P A H N
A P L S I I G
Y I R
string convert(string s, int numRows);
class Solution {
public String convert(String s, int numRows) {
int n= s.length(), r= numRows;
if(r==1 || r >=n){
return s;
}
StringBuilder[] mat = new StringBuilder[r];
for(int i=0; i<r; i++){
mat[i]= new StringBuilder();
}
for(int i=0,x=0, t=r*2-2; i<n ;i++){
mat[x].append(s.charAt(i));
if(i%t < r-1){
x++;
}else{
x--;
}
}
StringBuilder ans = new StringBuilder();
for(StringBuilder row : mat){
ans.append(row);
}
return ans.toString();
}
}