面试题目15:三数之和
将数组排序,遍历数组,以当前数为第一个数,在后面的序列中寻找剩余的两个数(头尾指针方式)。注意当前数大于0则可结束,且需要跳过重复的数。 |
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> lists = new ArrayList<>();
if (nums == null || nums.length<=2){
return lists;
}
Arrays.sort(nums);
for (int x = 0;x<=nums.length-3;x++){
if (nums[x]>0){
return lists;
}
if (x>0 && nums[x] == nums[x-1]){
continue;
}
find(nums,x+1,nums.length-1,nums[x],lists);
}
return lists;
}
public static void find(int[] nums,int start,int end,int number,List<List<Integer>> lists){
while (start<end){
int temp =nums[start] + nums[end] + number;
if (temp == 0){
List<Integer> thistime = new ArrayList<>();
thistime.add(nums[start]);
thistime.add(nums[end]);
thistime.add(number);
lists.add(thistime);
while (start<end && nums[start+1] == nums[start]){
start++;
}
start++;
while (start<end && nums[end-1] == nums[end]){
end--;
}
end--;
}else if (temp<0){
start++;
}else {
end--;
}
}
}
面试题73:矩阵置0
用第一行及第一列来记录出现0的行列,首先记录下第一行第一列是否有0。 |
public static void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0||matrix[0].length == 0){
return;
}
int row = matrix.length;
int col = matrix[0].length;
for (int x = 0;x<=row-1;x++){
for (int y = 0;y<=col-1;y++){
if (matrix[x][y] == 0){
reset(matrix,x,y,row,col);
}
}
}
for (int x = 0;x<=row-1;x++){
for (int y = 0;y<=col-1;y++){
if (matrix[x][y] ==Integer.MIN_VALUE){
matrix[x][y] = 0;
}
}
}
}
public static void reset(int[][] matrix,int x,int y,int row,int col){
for (int i = 0;i<=col-1;i++){
if (matrix[x][i]!=0){
matrix[x][i] = Integer.MIN_VALUE;
}
}
for (int i = 0;i<=row-1;i++){
if (matrix[i][y]!=0){
matrix[i][y] = Integer.MIN_VALUE;
}
}
}
面试题49:字母异位词分组
遍历字符串数组,用hashmap辅助,每读一个就将其字符串排序,不存在则添加,存在则添加其下标,再遍历hashmap生成返回答案 |
public static List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> lists = new ArrayList<>();
if (strs == null){
return lists;
}
HashMap<String,ArrayList<Integer>> hashMap = new HashMap<>();
for (int x = 0;x<=strs.length-1;x++){
char[] temp = strs[x].toCharArray();
Arrays.sort(temp);
String temp_st = String.valueOf(temp);
if (hashMap.containsKey(temp_st)){
hashMap.get(temp_st).add(x);
}else {
ArrayList<Integer> group = new ArrayList<>();
group.add(x);
hashMap.put(temp_st,group);
}
}
for (ArrayList<Integer> temp :hashMap.values()){
List<String> group = new ArrayList<>();
for (int x: temp){
group.add(strs[x]);
}
lists.add(group);
}
return lists;
}
面试题3:无重复字符的最长子串
遍历字符串,不断更新子串及最大长度 |
public static int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0){
return 0;
}
StringBuilder stringBuilder = new StringBuilder();
int max = 0;
int substring = 0;
char[] s_char = s.toCharArray();
for (int x = 0;x<=s_char.length-1;x++){
if (stringBuilder.indexOf(String.valueOf(s_char[x])) == -1){
stringBuilder.append(s_char[x]);
substring++;
}else {
int lastindex = stringBuilder.lastIndexOf(String.valueOf(s_char[x]));
stringBuilder.delete(0,lastindex+1);
stringBuilder.append(s_char[x]);
substring = stringBuilder.length();
}
if (max<substring){
max = substring;
}
}
return max;
}
面试题5:最长回文子串
动态规划求解,p[i.j] = true when p[i+1,j-1] = true && p[i] = p[j] |
public static String longestPalindrome(String s) {
if (s == null || s.length() == 0){
return "";
}
int start = 0;
int maxlenth = 0;
boolean[][] palindrome = new boolean[s.length()][s.length()];
char[] s_char = s.toCharArray();
for (int x = 0;x<=s_char.length-1;x++){
palindrome[x][x] = true;
if (x<s_char.length-1 && s_char[x] == s_char[x+1]){
palindrome[x][x+1] = true;
start = x;
maxlenth = 2;
}
}
if (maxlenth == 0){
maxlenth = 1;
start = 0;
}
for (int x = 3;x<=s_char.length;x++){
for (int y = 0;y<=s_char.length-x;y++){
int end = y+x-1;
if (palindrome[y+1][end-1] && s_char[y] == s_char[end]){
palindrome[y][end] = true;
maxlenth = x;
start = y;
}
}
}
if (maxlenth>0){
return s.substring(start,start+maxlenth);
}
return "";
}
面试题334:递增的三元子序列
用两个指针,当当前数小于第一个指针时,第一个指针变为当前数,当前数小于第二个指针时,第二个指针变为当前数,第一个指针维护最小值,第二个指针维护较小值,当前数大于第二个指针时,说明存在第三个数,输出true。 |
public static boolean increasingTriplet(int[] nums) {
if (nums == null || nums.length <=2){
return false;
}
int index1 = Integer.MAX_VALUE;
int index2 = Integer.MAX_VALUE;
for (int x = 0;x<=nums.length-1;x++){
if (index1>nums[x]){
index1 = nums[x];
}else if (nums[x]>index1 && nums[x]<index2){
index2 = nums[x];
}else if (nums[x]>index2){
return true;
}
}
return false;
}
面试题2:两数相加
按位相后走,注意进位问题 |
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null || l2 == null){
return null;
}
ListNode head = new ListNode(-1);
ListNode rem = head;
int carry = 0;
while (l1!=null && l2!=null){
int temp = l1.val+l2.val+carry;
if (temp > 9){
carry = 1;
temp = temp-10;
}else {
carry = 0;
}
ListNode tp = new ListNode(temp);
rem.next = tp;
rem = rem.next;
l1 = l1.next;
l2 = l2.next;
}
while (l1!=null){
int temp = l1.val+carry;
if (temp > 9){
carry = 1;
temp = temp-10;
}else {
carry = 0;
}
ListNode tp = new ListNode(temp);
rem.next = tp;
rem = rem.next;
l1 = l1.next;
}
while (l2!=null){
int temp = l2.val+carry;
if (temp > 9){
carry = 1;
temp = temp-10;
}else {
carry = 0;
}
ListNode tp = new ListNode(temp);
rem.next = tp;
rem = rem.next;
l2 = l2.next;
}
if (carry == 1){
ListNode tp = new ListNode(1);
rem.next = tp;
}
return head.next;
}
面试题328:奇偶链表
用两个指针,一个指向奇数,一个指向偶数,把偶数后出现的奇数移到目前的奇数后面,再将两个指针分别移动一位,此时偶数指针再次指向偶数位置。 |
public static ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null){
return head;
}
ListNode odd = head;
ListNode even = head.next;
while (even!= null && even.next!=null){
ListNode temp = even.next;
even.next = even.next.next;
temp.next = odd.next;
odd.next = temp;
odd = odd.next;
even = even.next;
}
return head;
}
面试题160:相交链表
计算两个的长度,调整为相同,再顺序遍历 |
分别遍历,当某链表读完后跳到另外一条的头继续开始读,最后会在交点相遇或在结尾(null)相遇 |
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null){
return null;
}
int lenthA = getlenth(headA);
int lenthB = getlenth(headB);
int dif = lenthA > lenthB ? lenthA-lenthB:lenthB-lenthA;
ListNode first = headA;
ListNode second = headB;
if (lenthA > lenthB){
for (int x = 1;x<=dif;x++){
first = first.next;
}
}else {
for (int x = 1;x<=dif;x++){
second = second.next;
}
}
while (first!=null){
if (first.val == second.val){
return fir