1.移动零
题目描述:
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]
class Solution {
public void moveZeroes(int[] nums) {
int left = 0,right = 1,temp;
while (left < nums.length && right < nums.length) {
while (left < nums.length && right < nums.length && nums[left] == 0) {
while(left < nums.length && right < nums.length && right > left && nums[right] != 0){
temp = nums[right];
nums[right] = nums[left];
nums[left] = temp;
left ++;
right ++;
}
right ++;
}
left ++;
}
}
}
2.判断子序列
题目描述:
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
示例:
输入:s = “abc”, t = “ahbgdc”
输出:true
class Solution {
public boolean isSubsequence(String s, String t) {
int s_length = 0, t_length = 0;
while (t_length < t.length() && s_length < s.length()) {
if (t.charAt(t_length) == s.charAt(s_length)) {
//System.out.println(t.charAt(t_length));
s_length++;
t_length++;
}
else{
t_length++;
}
}
if(s_length == s.length()) return true;
return false;
}
}
3.找到最高海拔
题目描述:
有一个自行车手打算进行一场公路骑行,这条路线总共由 n + 1 个不同海拔的点组成。自行车手从海拔为 0 的点 0 开始骑行。
给你一个长度为 n 的整数数组 gain ,其中 gain[i] 是点 i 和点 i + 1 的 净海拔高度差(0 <= i < n)。请你返回 最高点的海拔 。
示例:
输入:gain = [-5,1,5,0,-7]
输出:1
解释:海拔高度依次为 [0,-5,-4,1,1,-6] 。最高海拔为 1 。
class Solution {
public int largestAltitude(int[] gain) {
int max = 0,height = 0;
for (int i = 0; i < gain.length; i++) {
height += gain[i];
if(height > max) max = height;
}
return max;
}
}