牛客网刷题(Hw题库)

Posted Panda_Java

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了牛客网刷题(Hw题库)相关的知识,希望对你有一定的参考价值。

刷题笔记 ACM形式

1. 将16进制的数转为10进制的数

import java.util.*;
public class Main 

  private final static int BASE = 16;
  private static Map<Character, Integer> map = new HashMap<Character, Integer>()
  
      put('0', 0);
      put('1', 1);
      put('2', 2);
      put('3', 3);
      put('4', 4);
      put('5', 5);
      put('6', 6);
      put('7', 7);
      put('8', 8);
      put('9', 9);
      put('A', 10);
      put('B', 11);
      put('C', 12);
      put('D', 13);
      put('E', 14);
      put('F', 15);
      put('a', 10);
      put('b', 11);
      put('c', 12);
      put('d', 13);
      put('e', 14);
      put('f', 15);
  ;

  public static int getDecimal(String number) 
      int res = 0;
      for (char ch : number.toCharArray()) 
          res = res * BASE + map.get(ch);
      
      return res;
  

  public static void main(String[] args) 
      Scanner in = new Scanner(System.in);
      while (in.hasNext()) 
          String number = in.next();
          int res = getDecimal(number.substring(2));   // 截取第二位到最后一位
          System.out.println(res);
      
  

2 回溯法模板 (leetcode 79 单词搜索)

自己写的只能通过大部分案例 如下:

class Solution 
    static boolean  flag = false;
    public boolean exist(char[][] board, String word) 
       
        int m = board.length;
        int n = board[0].length;
        boolean[][] visit = new boolean[m][n];

        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                visit[i][j] = false;
            
        

        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(board[i][j] == word.charAt(0))
                    backtracking(board,word,visit,i,j,0);
            
        
        return flag;
    

    public void backtracking(char[][] board, String s, boolean[][] visit, int i, int j,int index)
        if(i<0 || i>=board.length || j<0 || j>=board[0].length || visit[i][j] == true)
            return;
        if(index < s.length())
            if(s.charAt(index) != board[i][j])
                return;
        else
            return;
        if(index == s.length()-1)
            flag = true;
       
        
        visit[i][j] = true;
        if(s.charAt(index) == board[i][j])
       
            backtracking(board,s,visit,i-1,j,index+1);
            backtracking(board,s,visit,i+1,j,index+1);
            backtracking(board,s,visit,i,j-1,index+1);
            backtracking(board,s,visit,i,j+1,index+1);    
       
       visit[i][j] = false; 
    

正确做法如下

class Solution 
    boolean find = false;

    public boolean exist(char[][] board, String word) 
        int m = board.length;
        int n = board[0].length;
        boolean[][] visited = new boolean[m][n];
        for (int i = 0; i < m; i++) 
            for (int j = 0; j < n; j++) 
                backtracking(i, j, board, word, visited, 0);
                if(find) 
                    break;
                
            
        
        return find;
    
    public void backtracking(int i, int j, char[][] board, String word, boolean[][] visited, int pos) 
        if (i < 0 || i >= board.length || j < 0 ||  j >= board[0].length) 
            return;
        
        if (board[i][j] != word.charAt(pos) || visited[i][j] || find) 
            return;
        
        if (pos == word.length() - 1) 
            find = true;
            return;
        
        visited[i][j] = true;
        backtracking(i + 1, j, board, word, visited, pos + 1);
        backtracking(i - 1, j, board, word, visited, pos + 1);
        backtracking(i, j + 1, board, word, visited, pos + 1);
        backtracking(i, j - 1, board, word, visited, pos + 1);
        visited[i][j] = false;
    

3. NC2 重排链表

public class Solution 
    public void reorderList(ListNode head) 
        if(head == null || head.next == null)
        	return;
        
        // 快满指针找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next != null && fast.next.next != null)
        	fast = fast.next.next;
        	slow = slow.next;
        
        // 拆分链表,并反转中间节点之后的链表
        ListNode after = slow.next;
        slow.next = null;
        ListNode pre = null;
        while(after != null)
        	ListNode temp = after.next;
        	after.next = pre;
        	pre = after;
        	after = temp;
        
        // 合并两个链表
        ListNode first = head;
        after = pre;
        while(first != null && after != null)
        	ListNode ftemp = first.next;
        	ListNode aftemp = after.next;
        	first.next = after;
        	first = ftemp;
        	after.next = first;      	
        	after = aftemp;     	
        
    


以上是关于牛客网刷题(Hw题库)的主要内容,如果未能解决你的问题,请参考以下文章

Python牛客网刷题-Day1

牛客网刷题知识汇总3

牛客网刷题——JAVA

SQL之牛客网刷题

牛客网刷题ACM模式 Objc Swift

C语言之牛客网刷题笔记 点进来保证让知识充实你一整天