剑指 Offer 04. 二维数组中的查找
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if(matrix==null || matrix.length==0){
return false;
}
int rows= matrix.length;
int cols=matrix[0].length;
int i=0;
int j=cols-1;
while(i<rows && j>=0){
if(matrix[i][j]>target){
j--;
}else if(matrix[i][j]<target){
i++;
}else {
return true;
}
}
return false;
}
}
剑指 Offer 06. 从尾到头打印链表
class Solution {
public int[] reversePrint(ListNode head) {
ListNode node = head;
int count = 0;
while (node != null) {
++count;
node = node.next;
}
int[] nums = new int[count];
node = head;
for (int i = count - 1; i >= 0; --i) {
nums[i] = node.val;
node = node.next;
}
return nums;
}
}
剑指 Offer 09. 用两个栈实现队列
class CQueue {
Stack<Integer> s1,s2;
public CQueue() {
s1=new Stack<>();
s2=new Stack<>();
}
public void appendTail(int value) {
s1.push(value);
}
public int deleteHead() {
if(s1.empty()){
return -1;
}
while(!s1.empty()){
s2.push(s1.peek());
s1.pop();
}
int num=s2.peek();
s2.pop();
while(!s2.empty()){
s1.push(s2.peek());
s2.pop();
}
return num;
}
}
剑指 Offer 18. 删除链表的节点
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode pre=null,cur=head;
while(cur.val!=val){
pre=cur;
cur=cur.next;
}
if(cur==head){
head=head.next;
}else{
pre.next=cur.next;
}
return head;
}
}
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
class Solution {
public int[] exchange(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
while (left < right && nums[left] % 2 != 0) {
left++;
}
while (left < right && nums[right] % 2 == 0) {
right--;
}
if (left < right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
return nums;
}
}
剑指 Offer 22. 链表中倒数第k个节点
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode frontNode=head, behindNode=head;
while(frontNode!=null && k>0){
frontNode=frontNode.next;
k--;
}
while(frontNode !=null){
frontNode=frontNode.next;
behindNode=behindNode.next;
}
return behindNode;
}
}
剑指 Offer 24. 反转链表
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null,next=null, cur=head;
while(cur!=null){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
}
public void reverse(){
if (isEmpt()){
return;
}
reverse(head.next);
}
public Node reverse(Node curr){
if (curr.next==null){
head.next=curr;
return curr;
}
Node pre = reverse(curr.next);
pre.next=curr;
curr.next=null;
return curr;
}
剑指 Offer 25. 合并两个排序的链表
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode newNode = new ListNode(0), pre = newNode;
while(l1!=null && l2 != null){
if(l1.val <= l2.val){
pre.next=l1;
pre=pre.next;
l1=l1.next;
}else{
pre.next=l2;
pre=pre.next;
l2=l2.next;
}
}
if(l1!=null){
pre.next=l1;
}
if(l2!=null){
pre.next=l2;
}
return newNode.next;
}
}
剑指 Offer 29. 顺时针打印矩阵
class Solution {
public int[] spiralOrder(int[][] matrix) {
int row=matrix.length;
if(row == 0){
return new int[0];
}
int col=matrix[0].length;
int[] nums=new int[row*col];
int index=0;
int left=0,top=0,right=col-1,bottom=row-1;
while(true){
for(int i=left;i<=right;i++){
nums[index++]=matrix[top][i];
}
if(++top>bottom){
break;
}
for(int i=top;i<=bottom;i++){
nums[index++]=matrix[i][right];
}
if(--right<left){
break;
}
for(int i=right;i>=left;i--){
nums[index++]=matrix[bottom][i];
}
if(--bottom<top){
break;
}
for(int i=bottom;i>=top;i--){
nums[index++]=matrix[i][left];
}
if(++left>right){
break;
}
}
return nums;
}
}