數(shù)組
// 找出某個目標數(shù) 在一二維數(shù)組中是否存在
public boolean find(int target,int [][] array){
if(array == null || array.length <= 0){
return false;
}
int row = array.length - 1;
int col = 0;
int cols = array[0].length;
while(row >= 0 && col < cols){
int cur = array[row][col];
if(cur < target){
col ++;
}else if(cur > target){
row --;
}else{
return true;
}
}
return false;
}
//求連續(xù)子列 最大和
int MaxSubSeqSub(int [] a,int N){
int thisSum = 0;
int maxSum = 0;
for(int i = 0;i < N;i++){
thisSum +=a[i];
if(thisSum > maxSum){
maxSum = thisSum;
}else if(thisSum < 0){
thisSum = 0;
}
}
return maxSum;
}
鏈表
// 單鏈表節(jié)點的結(jié)構(gòu)
public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
//鏈表反轉(zhuǎn)
ListNode reverse(ListNode head) {
if (head.next == null) return head;
ListNode last = reverse(head.next);
head.next.next = head;
head.next = null;
return last;
}
//反轉(zhuǎn)鏈表前n 個節(jié)點
ListNode successor = null;
ListNode reverseN(ListNode head,int n){
if(n == 1){
//記錄第 n+1 個節(jié)點
successor = head.next;
return head;
}
//以head.next 為起點,需要反轉(zhuǎn)前n-1個節(jié)點
ListNode last = reverseN(head.next, n - 1);
head.next.next = head;
head.next = successor;
return last;
}
// 打印鏈表
static void printListNode(ListNode head){
if(head.next == null){
System.out.println(head.val);
System.out.println("end");
return;
}
ListNode node = head.next;
System.out.println(head.val);
printListNode(node);
System.out.println(" end "+node.val);
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。