剑指Offer之Java算法习题精讲链表与数组专项训练

网友投稿 266 2022-08-18


剑指Offer之Java算法习题精讲链表与数组专项训练

题目一

数组题——查找目标值

在给定的数组中查找指定的目标值,这里提供两种解法

具体题目如下

解法一

class Solution {

public int[] twoSum(int[] nums, int target) {

int[] a = {-1,-1};

for(int i = 0;i

for(int j = i+1;j

if(nums[i]+nums[j]==target){

a[0] = i;

a[1] = j;

return a;

}

}

}

return a;

}

}

解法二

class Solution {

public int[] twoSum(int[] nums, int target) {

HashMap index = new HashMap<>();

for(int i = 0;i

index.put(nums[i],i);

}

for (int i = 0; i < nums.length; i++) {

if(index.containsKey(target - nums[i])&&i!=index.get(target - nums[i])){

return new int[]{i,index.get(target - nums[i])};

}

}

return new int[]{-1,-1};

}

}

题目二

数组题——查找三元组

查找给定的数组中是否存在指定的三个元素并使得该三个元素相加等于0

具体题目如下

解法

class Solution {

public List> threeSum(int[] nums) {

Arrays.sort(nums);

return nSumTarget(nums, 3, 0, 0);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

http:// while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

题目三

数组题——查找四元组

查找给定的数组中满足条件的四元组

具体题目如下

解法

class Solution {

public List> fourSum(int[] nums, int target) {

Arrays.sort(nums);

return nSumTarget(nums,4,0,target);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

http:// list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

模板理解背下来~

题目四

链表题——反转链表

根据单链表的头节点head来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

public ListNode reverseList(ListNode head) {

if(head==null||head.next==null){

return head;

}

ListNode last = reverseList(head.next);

head.next.next = head;

head.next = null;

return last;

}

}

题目五

链表题——反转链表

根据单链表的头节点head和指定条件来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

ListNode cure = null;

public ListNode reverseBetween(ListNode head, int left, int right) {

if(left==1){

return reverseN(head, right);

}

head.next = reverseBetween(head.next,left-1,right-1);

return head;

}

public ListNode reverseN(ListNode head,int n){

if(n==1){

cure = head.next;

return head;

}

http:// ListNode last = reverseN(head.next,n-1);

head.next.next = head;

head.next = cure;

return last;

}

}

for(int j = i+1;j

if(nums[i]+nums[j]==target){

a[0] = i;

a[1] = j;

return a;

}

}

}

return a;

}

}

解法二

class Solution {

public int[] twoSum(int[] nums, int target) {

HashMap index = new HashMap<>();

for(int i = 0;i

index.put(nums[i],i);

}

for (int i = 0; i < nums.length; i++) {

if(index.containsKey(target - nums[i])&&i!=index.get(target - nums[i])){

return new int[]{i,index.get(target - nums[i])};

}

}

return new int[]{-1,-1};

}

}

题目二

数组题——查找三元组

查找给定的数组中是否存在指定的三个元素并使得该三个元素相加等于0

具体题目如下

解法

class Solution {

public List> threeSum(int[] nums) {

Arrays.sort(nums);

return nSumTarget(nums, 3, 0, 0);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

http:// while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

题目三

数组题——查找四元组

查找给定的数组中满足条件的四元组

具体题目如下

解法

class Solution {

public List> fourSum(int[] nums, int target) {

Arrays.sort(nums);

return nSumTarget(nums,4,0,target);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

http:// list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

模板理解背下来~

题目四

链表题——反转链表

根据单链表的头节点head来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

public ListNode reverseList(ListNode head) {

if(head==null||head.next==null){

return head;

}

ListNode last = reverseList(head.next);

head.next.next = head;

head.next = null;

return last;

}

}

题目五

链表题——反转链表

根据单链表的头节点head和指定条件来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

ListNode cure = null;

public ListNode reverseBetween(ListNode head, int left, int right) {

if(left==1){

return reverseN(head, right);

}

head.next = reverseBetween(head.next,left-1,right-1);

return head;

}

public ListNode reverseN(ListNode head,int n){

if(n==1){

cure = head.next;

return head;

}

http:// ListNode last = reverseN(head.next,n-1);

head.next.next = head;

head.next = cure;

return last;

}

}

if(nums[i]+nums[j]==target){

a[0] = i;

a[1] = j;

return a;

}

}

}

return a;

}

}

解法二

class Solution {

public int[] twoSum(int[] nums, int target) {

HashMap index = new HashMap<>();

for(int i = 0;i

index.put(nums[i],i);

}

for (int i = 0; i < nums.length; i++) {

if(index.containsKey(target - nums[i])&&i!=index.get(target - nums[i])){

return new int[]{i,index.get(target - nums[i])};

}

}

return new int[]{-1,-1};

}

}

题目二

数组题——查找三元组

查找给定的数组中是否存在指定的三个元素并使得该三个元素相加等于0

具体题目如下

解法

class Solution {

public List> threeSum(int[] nums) {

Arrays.sort(nums);

return nSumTarget(nums, 3, 0, 0);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

http:// while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

题目三

数组题——查找四元组

查找给定的数组中满足条件的四元组

具体题目如下

解法

class Solution {

public List> fourSum(int[] nums, int target) {

Arrays.sort(nums);

return nSumTarget(nums,4,0,target);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

http:// list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

模板理解背下来~

题目四

链表题——反转链表

根据单链表的头节点head来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

public ListNode reverseList(ListNode head) {

if(head==null||head.next==null){

return head;

}

ListNode last = reverseList(head.next);

head.next.next = head;

head.next = null;

return last;

}

}

题目五

链表题——反转链表

根据单链表的头节点head和指定条件来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

ListNode cure = null;

public ListNode reverseBetween(ListNode head, int left, int right) {

if(left==1){

return reverseN(head, right);

}

head.next = reverseBetween(head.next,left-1,right-1);

return head;

}

public ListNode reverseN(ListNode head,int n){

if(n==1){

cure = head.next;

return head;

}

http:// ListNode last = reverseN(head.next,n-1);

head.next.next = head;

head.next = cure;

return last;

}

}

index.put(nums[i],i);

}

for (int i = 0; i < nums.length; i++) {

if(index.containsKey(target - nums[i])&&i!=index.get(target - nums[i])){

return new int[]{i,index.get(target - nums[i])};

}

}

return new int[]{-1,-1};

}

}

题目二

数组题——查找三元组

查找给定的数组中是否存在指定的三个元素并使得该三个元素相加等于0

具体题目如下

解法

class Solution {

public List> threeSum(int[] nums) {

Arrays.sort(nums);

return nSumTarget(nums, 3, 0, 0);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

http:// while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

题目三

数组题——查找四元组

查找给定的数组中满足条件的四元组

具体题目如下

解法

class Solution {

public List> fourSum(int[] nums, int target) {

Arrays.sort(nums);

return nSumTarget(nums,4,0,target);

}

public List> nSumTarget(int[] nums, int n, int start, int target){

int size = nums.length;

List> res = new ArrayList<>();

if(n < 2 || size < n) return res;

if(n == 2){

int lo = start, hi = size - 1;

while(lo < hi){

int left = nums[lo], right = nums[hi];

int sum = left + right;

if(sum < target){

while(lo < hi && nums[lo] == left) lo++;

}else if(sum > target){

while(lo < hi && nums[hi] == right) hi--;

}else{

List list = new ArrayList<>();

http:// list.add(nums[lo]);

list.add(nums[hi]);

res.add(list);

while(lo < hi && nums[lo] == left) lo++;

while(lo < hi && nums[hi] == right) hi--;

}

}

}else{

for(int i = start; i < size; i++){

List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);

for(List list : temp){

list.add(nums[i]);

res.add(list);

}

while(i < size - 1 && nums[i] == nums[i + 1]) i++;

}

}

return res;

}

}

模板理解背下来~

题目四

链表题——反转链表

根据单链表的头节点head来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

public ListNode reverseList(ListNode head) {

if(head==null||head.next==null){

return head;

}

ListNode last = reverseList(head.next);

head.next.next = head;

head.next = null;

return last;

}

}

题目五

链表题——反转链表

根据单链表的头节点head和指定条件来返回反转后的链表

具体题目如下

解法

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode() {}

* ListNode(int val) { this.val = val; }

* ListNode(int val, ListNode next) { this.val = val; this.next = next; }

* }

*/

class Solution {

ListNode cure = null;

public ListNode reverseBetween(ListNode head, int left, int right) {

if(left==1){

return reverseN(head, right);

}

head.next = reverseBetween(head.next,left-1,right-1);

return head;

}

public ListNode reverseN(ListNode head,int n){

if(n==1){

cure = head.next;

return head;

}

http:// ListNode last = reverseN(head.next,n-1);

head.next.next = head;

head.next = cure;

return last;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用FeignClient调用POST表单Body内没有参数问题
下一篇:剑指Offer之Java算法习题精讲链表与二叉树专项训练
相关文章

 发表评论

暂时没有评论,来抢沙发吧~