劍指Offer之Java算法習(xí)題精講鏈表與數(shù)組專項(xiàng)訓(xùn)練
題目一
數(shù)組題——查找目標(biāo)值
在給定的數(shù)組中查找指定的目標(biāo)值,這里提供兩種解法
具體題目如下
解法一
class Solution { public int[] twoSum(int[] nums, int target) { int[] a = {-1,-1}; for(int i = 0;i<nums.length-1;i++){ for(int j = i+1;j<nums.length;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<Integer, Integer> index = new HashMap<>(); for(int i = 0;i<nums.length;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}; } }
題目二
數(shù)組題——查找三元組
查找給定的數(shù)組中是否存在指定的三個(gè)元素并使得該三個(gè)元素相加等于0
具體題目如下
解法
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); return nSumTarget(nums, 3, 0, 0); } public List<List<Integer>> nSumTarget(int[] nums, int n, int start, int target){ int size = nums.length; List<List<Integer>> 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<Integer> 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<List<Integer>> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]); for(List<Integer> list : temp){ list.add(nums[i]); res.add(list); } while(i < size - 1 && nums[i] == nums[i + 1]) i++; } } return res; } }
題目三
數(shù)組題——查找四元組
查找給定的數(shù)組中滿足條件的四元組
具體題目如下
解法
class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); return nSumTarget(nums,4,0,target); } public List<List<Integer>> nSumTarget(int[] nums, int n, int start, int target){ int size = nums.length; List<List<Integer>> 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<Integer> 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<List<Integer>> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]); for(List<Integer> list : temp){ list.add(nums[i]); res.add(list); } while(i < size - 1 && nums[i] == nums[i + 1]) i++; } } return res; } }
模板理解背下來~
題目四
鏈表題——反轉(zhuǎn)鏈表
根據(jù)單鏈表的頭節(jié)點(diǎn)head來返回反轉(zhuǎn)后的鏈表
具體題目如下
解法
/** * 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; } }
題目五
鏈表題——反轉(zhuǎn)鏈表
根據(jù)單鏈表的頭節(jié)點(diǎn)head和指定條件來返回反轉(zhuǎn)后的鏈表
具體題目如下
解法
/** * 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; } ListNode last = reverseN(head.next,n-1); head.next.next = head; head.next = cure; return last; } }
到此這篇關(guān)于劍指Offer之Java算法習(xí)題精講鏈表與數(shù)組專項(xiàng)訓(xùn)練的文章就介紹到這了,更多相關(guān)Java 鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 劍指Offer之Java算法習(xí)題精講鏈表與二叉樹專項(xiàng)訓(xùn)練
- 劍指Offer之Java算法習(xí)題精講鏈表與字符串及數(shù)組
- Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之單向鏈表
- Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之環(huán)形鏈表
- Java實(shí)現(xiàn)順序表和鏈表結(jié)構(gòu)
- Java實(shí)現(xiàn)單鏈表基礎(chǔ)操作
- Java關(guān)于重排鏈表詳細(xì)解析
- Java?詳解分析鏈表的中間節(jié)點(diǎn)
- 劍指Offer之Java算法習(xí)題精講鏈表專項(xiàng)訓(xùn)練
相關(guān)文章
java實(shí)現(xiàn)動(dòng)態(tài)上傳多個(gè)文件并解決文件重名問題
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)動(dòng)態(tài)上傳多個(gè)文件,并解決文件重名問題的方法,感興趣的小伙伴們可以參考一下2016-03-03Java后臺(tái)基于POST獲取JSON格式數(shù)據(jù)
這篇文章主要介紹了Java后臺(tái)基于POST獲取JSON格式數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Java經(jīng)典面試題匯總:網(wǎng)絡(luò)編程
本篇總結(jié)的是Java 網(wǎng)絡(luò)編程相關(guān)的面試題,后續(xù)會(huì)持續(xù)更新,希望我的分享可以幫助到正在備戰(zhàn)面試的實(shí)習(xí)生或者已經(jīng)工作的同行,如果發(fā)現(xiàn)錯(cuò)誤還望大家多多包涵,不吝賜教,謝謝2021-07-07Spring Cloud學(xué)習(xí)教程之Zuul統(tǒng)一異常處理與回退
Spring Cloud Zuul對(duì)異常的處理整體來說還是比較方便的,流程也比較清晰,下面這篇文章主要給大家介紹了關(guān)于Spring Cloud學(xué)習(xí)教程之Zuul統(tǒng)一異常處理與回退的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04