package org.example.interview.practice;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* @author xianzhe.ma
* @date 2021/7/13
*/
public class NC_40_MERGETWOLINKLIST {
public static class ListNode {
int val;
ListNode next = null;
public ListNode(int val) {
this.val = val;
}
}
public static ListNode addInList (ListNode head1, ListNode head2) {
// write code here
Deque<Integer> queueA = new ArrayDeque<>();
Deque<Integer> queueB = new ArrayDeque<>();
for ( ; head1!= null; head1 = head1.next) {
queueA.addLast(head1.val);
}
for ( ; head2!= null; head2 = head2.next) {
queueB.addLast(head2.val);
}
int highValue = 0;//装进位的
ListNode result = null;
ListNode last = null;
while (!queueA.isEmpty() && !queueB.isEmpty()) {
int number1 = queueA.pollLast();
int number2 = queueB.pollLast();
int val = (number1 + number2 + highValue) % 10;
highValue = (number1 + number2 + highValue) / 10;
ListNode node = new ListNode(val);
if (result == null) {
result = node;
// last = node;
} else {
node.next = result;
result = node;
}
}
while (!queueA.isEmpty()) {
int number1 = queueA.pollLast();
int val = (number1 + highValue) % 10;
highValue = (number1 + highValue) / 10;
ListNode node = new ListNode(val);
if (result == null) {
result = node;
// last = node;
} else {
node.next = result;
result = node;
}
}
while (!queueB.isEmpty()) {
int number1 = queueB.pollLast();
int val = (number1 + highValue) % 10;
highValue = (number1 + highValue) / 10;
ListNode node = new ListNode(val);
if (result == null) {
result = node;
// last = node;
} else {
node.next = result;
result = node;
}
}
if (highValue != 0) {
ListNode node = new ListNode(highValue);
node.next = result;
result = node;
}
return result;
}
public static void main (String[] args) {
int[] arrA = {9,3,7};
int[] arrB = {6,3};
ListNode listA = buildLinkList(arrA);
ListNode listB = buildLinkList(arrB);
ListNode result = addInList(listA, listB);
}
public static ListNode buildLinkList(int[] arr) {
ListNode listA = null;
ListNode last = null;
for (int i : arr) {
ListNode node = new ListNode(i);
if (listA == null) {
listA = node;
last = node;
}
last.next = node;
last = node;
}
return listA;
}
}
package org.example.interview.practice;
import java.util.HashMap;
import java.util.Map;
/**
* @author xianzhe.ma
* @date 2021/7/7
*/
public class NC_41_LONGEST_SUBARR {
public int maxLength(int[] arr) {
// write code here
if (arr.length == 0)
return 0;
HashMap<Integer, Integer> map = new HashMap<>();
int max = 0;
for (int i = 0, j = 0; i < arr.length; ++i) {
if (map.containsKey(arr[i])) {
j = Math.max(j, map.get(arr[i]) + 1);
}
map.put(arr[i], i);
max = Math.max(max, i - j + 1);
}
return max;
}
public static int maxLength2(int[] arr) {
// write code here
if (arr.length == 0)
return 0;
HashMap<Integer, Integer> map = new HashMap<>();
int max = 0;
for (int i = 0, j = 0; i < arr.length; ++i) {
if (map.containsKey(arr[i])) {
// j = Math.max(j, map.get(arr[i]) + 1);
j = map.get(arr[i]) + 1;
}
map.put(arr[i], i);
max = Math.max(max, i - j + 1);
}
return max;
}
public static int maxLength3(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
int maxlength = 0;
Map<Integer, Integer> positionMap = new HashMap<>();
int i = 0, j = 0;
for (; i < arr.length; i++) {
if (positionMap.containsKey(Integer.valueOf(arr[i]))) {
j = Math.max(j, positionMap.get(arr[i]) + 1);
}
positionMap.put(arr[i], i);
maxlength = Math.max(maxlength, i - j + 1);
}
return maxlength;
}
public static void main(String[] args) {
int[] arr = {3, 3, 2, 1, 3, 3, 3, 1};
System.out.println(maxLength3(arr));
}
}
package org.example.interview.practice;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
/**
* @author xianzhe.ma
* @date 2021/7/27
*/
public class NC_42_PERMUTATION {
//用于标记是否访问过
static boolean[] mark;
public static ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
//存储总的返回结果集
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
//存储一个合法全排列
LinkedList<Integer> track = new LinkedList<>();
mark = new boolean[num.length];
//因为存在重复项,为了更好的处理,将数组先排序
Arrays.sort(num);
backtrack(num,res,track);
return res;
}
public static void backtrack(int[] num, ArrayList<ArrayList<Integer>> res, LinkedList<Integer> track){
//若 找到一个全排列,则将它加进结果集中,然后返回(回溯)
if(track.size() == num.length){
res.add(new ArrayList<Integer>(track));
return;
}
for(int i = 0; i < num.length; i++){
// 当此时的被访问过
// 当i>0 &&此时的数等于它的上一个&&上一个没访问过(没访问过证明是回溯后将前面的置为false,所以此时避免重复得用 !mark[i-1] 满足要求然后跳过该数字)
// arr[1,1,1,2,3]
// 1,1,1 1,1,2 1,1,3 前面两次回溯得到三个结果
// 接下来再进行回溯,此时mark[1]被置为false
// 此时按道理应该遍历到arr[2]这个位置
// 1,arr[2] 然后后面再加进去,但是我们发现arr[2]==arr[1],并且此时mark[1]==false
// 证明它的已经访问过然后回溯的,所以我们跳过arr[2],直接从1,arr[3]开始
// 也就是说此时得到全排列的结果将会是 1,2,1 1,2,3 而不再是 1,1 ··· 这些重复的了
if(mark[i] || i>0 && num[i] == num[i-1] && !mark[i-1]){
continue;
}
//添加进全排列数组中
track.add(num[i]);
//标记为已经访问
mark[i] = true;
//继续寻找下一个数
backtrack(num,res,track);
//将上一次全排列的结果中,最后一个数移除掉
track.removeLast();
//移除掉的数置为 未访问
mark[i] = false;
}
}
public static void main (String[] args) {
int[] num = {1,2,1};
ArrayList<ArrayList<Integer>> res = permuteUnique (num);
for (ArrayList<Integer> item : res) {
System.out.println(item.toString());
}
}
}