两数组的交(直接上代码,关键点说一下就好了)
public int[] intersection(int[] nums1, int[] nums2) {
// write your code here
// write your code here
//这里是关键点,如果一个数组很长很长,另一个是空的,那就直接返回一个空就好了。
if (nums1.length < 1) {
return nums1;
}
if (nums2.length < 1) {
return nums2;
}
//这里选择用treeset 重复的元素会直接过滤掉。
TreeSet<Integer> temp = new TreeSet<>();
TreeSet<Integer> temp2 = new TreeSet<>();
for (Integer i : nums1) {
temp.add(i);
}
for (Integer j : nums2) {
if (temp.contains(j)) {
temp2.add(j);
}
}
int[] a = new int[temp2.size()];
int i = 0;
Iterator<Integer> iterator = temp2.iterator();
while (iterator.hasNext()) {
a[i++] = iterator.next();
}
return a;
}
if (nums1.length < 1) {
return nums1;
}
if (nums2.length < 1) {
return nums2;
}
//这里选择用treeset 重复的元素会直接过滤掉。
TreeSet<Integer> temp = new TreeSet<>();
TreeSet<Integer> temp2 = new TreeSet<>();
for (Integer i : nums1) {
temp.add(i);
}
for (Integer j : nums2) {
if (temp.contains(j)) {
temp2.add(j);
}
}
int[] a = new int[temp2.size()];
int i = 0;
Iterator<Integer> iterator = temp2.iterator();
while (iterator.hasNext()) {
a[i++] = iterator.next();
}
return a;
}
两数组的交 II (这题目的说明有点不明确,我还以为是每个元素出现的次数也需要相同。说明的太差。)
//思路和上一题基本差不多,选择hashmap,获取两个map中相同元素最少的次数,for循环加到集合中即可。不懂的看上面的解题思路。
public int[] intersection(int[] nums1, int[] nums2) {
// write your code here
if (nums1.length < 1) {
return nums1;
}
if (nums2.length < 1) {
return nums2;
}
HashMap<Integer, Integer> temp = new HashMap<>();
HashMap<Integer, Integer> temp2 = new HashMap<>();
ArrayList<Integer> temp3 = new ArrayList<>();
// write your code here
if (nums1.length < 1) {
return nums1;
}
if (nums2.length < 1) {
return nums2;
}
HashMap<Integer, Integer> temp = new HashMap<>();
HashMap<Integer, Integer> temp2 = new HashMap<>();
ArrayList<Integer> temp3 = new ArrayList<>();
for (Integer i : nums1) {
if (temp.containsKey(i)) {
temp.put(i, temp.get(i) + 1);
} else {
temp.put(i, 1);
}
}
for (Integer i : nums2) {
if (temp2.containsKey(i)) {
temp2.put(i, temp2.get(i) + 1);
} else {
temp2.put(i, 1);
}
}
Set<Integer> integers = temp.keySet();
Iterator<Integer> iterator = integers.iterator();
while (iterator.hasNext()) {
int i = iterator.next();
if (temp2.keySet().contains(i)) {
int min = Math.min(temp.get(i), temp2.get(i));
for (int j = 0; j < min; j++) {
temp3.add(i);
}
}
}
int[] a = new int[temp3.size()];
int i = 0;
Iterator<Integer> it = temp3.iterator();
while (it.hasNext()) {
a[i++] = it.next();
}
return a;
}
左填充 (毫无难度,唯一要注意的就是,预先写好的那个代码,没有加static)
static public String leftPad(String originalStr, int size) {// Write your code here
while(size - originalStr.length() > 0) {
originalStr = " " + originalStr;
}
return originalStr;
}
/*
* @param originalStr: the string we want to append to
* @param size: the target length of the string
* @param padChar: the character to pad to the left side of the string
* @return: A string
*/
static public String leftPad(String originalStr, int size, char padChar) {
// write your code here
while(size - originalStr.length() > 0) {
originalStr = padChar + originalStr;
}
return originalStr;
}
最后一个单词的长度 (这题简单的以为我看错题目了。切割一下就好了,没啥好说的。)
public int lengthOfLastWord(String s) {// write your code here
if (s.length() == 0) {
return 0;
}
String[] split = s.split(" ");
return split[split.length - 1].length();
}
最长上升连续子序列(根据以往的套路,找一个压轴题。其实这题也不难,哈哈。)
//说一下我的思路吧,如果数组长度小于等于2,直接返回数组长度。
public int longestIncreasingContinuousSubsequence(int[] A) {
// write your code here
if (A.length <= 2) {
return A.length;
}
//我这边用的是笨方法,正着遍历一遍,然后反着遍历一遍。记录下对应的次数。返回较大的即可。
int times1 = 0;
int times2 = 0;
int temp = 1; //这里说明以下,因为我们是从1开始遍历,所以默认长度应该为1。
for (int i = 1; i < A.length; i++) {
if (A[i] > A[i - 1]) {
temp++;
} else {
if (times1 < temp) {
times1 = temp;
}
temp = 1;
}
}
if (times1 < temp) { //记录最后一个数的情况。并赋值。
times1 = temp;
}
if (times1 > A.length / 2) {//这里是为了节约时间,如果次数已经大于一半了,那没有必要反着遍历了。下面都是重复了,就不解释了。
return times1;
}
temp = 1;
for (int i = 1; i < A.length; i++) {
if (A[i] < A[i - 1]) {
temp++;
} else {
if (times2 < temp) {
times2 = temp;
}
temp = 1;
}
}
if (times2 < temp) {
times2 = temp;
}
if (times2 > A.length / 2) {
return times2;
}
return times1 > times2 ? times1 : times2;
}
int times1 = 0;
int times2 = 0;
int temp = 1; //这里说明以下,因为我们是从1开始遍历,所以默认长度应该为1。
for (int i = 1; i < A.length; i++) {
if (A[i] > A[i - 1]) {
temp++;
} else {
if (times1 < temp) {
times1 = temp;
}
temp = 1;
}
}
if (times1 < temp) { //记录最后一个数的情况。并赋值。
times1 = temp;
}
if (times1 > A.length / 2) {//这里是为了节约时间,如果次数已经大于一半了,那没有必要反着遍历了。下面都是重复了,就不解释了。
return times1;
}
temp = 1;
for (int i = 1; i < A.length; i++) {
if (A[i] < A[i - 1]) {
temp++;
} else {
if (times2 < temp) {
times2 = temp;
}
temp = 1;
}
}
if (times2 < temp) {
times2 = temp;
}
if (times2 > A.length / 2) {
return times2;
}
return times1 > times2 ? times1 : times2;
}