こんにちは、私は
方圆
リンクされたリスト
剣はオファー06を指します。リンクされたリストを最後から最初まで印刷してください
class Solution {
ArrayList<Integer> temp = new ArrayList<>();
public int[] reversePrint(ListNode head) {
recur(head);
int[] res = new int[temp.size()];
for(int i = 0;i < temp.size();i++)
res[i] = temp.get(i);
return res;
}
private void recur(ListNode head){
if(head == null) return;
recur(head.next);
temp.add(head.val);
}
}
ソードはオファー18を指します。リンクされたリストのノードを削除します
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val) return head.next;
ListNode pre = head;
ListNode cur = head.next;
while(cur != null){
if(cur.val == val){
pre.next = cur.next;
return head;
}
pre = cur;
cur = cur.next;
}
return head;
}
}
剣はオファー22を指します。リンクされたリストの下からk番目のノード
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode temp = head;
while(k != 0){
temp = temp.next;
k--;
}
while(temp != null){
head = head.next;
temp = temp.next;
}
return head;
}
}
ソードはオファー24を指します。逆にリンクされたリスト
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur = head;
ListNode pre = null;
ListNode temp = null;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
ソードはオファー35を指します。複雑なリンクリストのコピー
class Solution {
HashMap<Node,Node> visited = new HashMap<>();
public Node copyRandomList(Node head) {
if(head == null) return null;
if(visited.containsKey(head))
return visited.get(head);
Node node = new Node(head.val);
visited.put(head,node);
node.next = copyRandomList(head.next);
node.random = copyRandomList(head.random);
return node;
}
}
ソードはオファー52を指します。2つのリンクされたリストの最初の共通ノード
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lengthA = getLength(headA);
int lengthB = getLength(headB);
if(lengthA > lengthB){
for(int i = 0;i < lengthA - lengthB;i++)
headA = headA.next;
}else{
for(int j = 0;j < lengthB - lengthA;j++)
headB = headB.next;
}
while(headA != headB){
headA = headA.next;
headB = headB.next;
}
return headA;
}
private int getLength(ListNode head){
int length = 0;
while(head != null){
head = head.next;
length++;
}
return length;
}
}
動的プログラミング
剣はオファー12を指します。マトリックス内のパス
class Solution {
public boolean exist(char[][] board, String word) {
char[] words = word.toCharArray();
for(int i = 0;i < board.length;i++){
for(int j = 0;j < board[0].length;j++){
if(dfs(board,words,i,j,0))
return true;
}
}
return false;
}
private boolean dfs(char[][] board,char[] words,int i,int j,int k){
if(i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != words[k])
return false;
if(k == words.length - 1)
return true;
char temp = board[i][j];
board[i][j] = ' ';
boolean res = (dfs(board,words,i + 1,j,k + 1) || dfs(board,words,i - 1,j,k + 1)
|| dfs(board,words,i,j + 1,k + 1) || dfs(board,words,i,j - 1,k + 1));
board[i][j] = temp;
return res;
}
}
ソードはオファー42を指します。連続するサブアレイの最大合計
class Solution {
public int maxSubArray(int[] nums) {
int res = nums[0];
int sum = 0;
for(int num : nums){
if(sum > 0){
sum += num;
}else{
sum = num;
}
res = Math.max(sum,res);
}
return res;
}
}
ソードはオファー47を指します。贈り物の最大の価値
class Solution {
public int maxValue(int[][] grid) {
int row = grid.length,col = grid[0].length;
//初始化第一行
for(int i = 1;i < col;i++)
grid[0][i] += grid[0][i - 1];
//初始化第一列
for(int j = 1;j < row;j++)
grid[j][0] += grid[j - 1][0];
//原地变换
for(int i = 1;i < row;i++){
for(int j = 1;j < col;j++){
grid[i][j] += Math.max(grid[i][j - 1],grid[i - 1][j]);
}
}
return grid[row - 1][col - 1];
}
}
剣はオファー63を指します。株式の最大利益
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int res = 0;
for(int i = 0;i < prices.length;i++){
if(minPrice > prices[i])
minPrice = prices[i];
else
res = Math.max(res,prices[i] - minPrice);
}
return res;
}
}
いい加減にして!