一、返回中间指针:

class Solution {

    public ListNode middleNode(ListNode head) {  //方法返回的类型

        ListNode[] A = new ListNode[100];

        int t = 0;

        while (head != null) {

            A[t++] = head;   //执行完A[t]=head以后,t才会执行t=t++。++t就是先执行+。

            head = head.next;

        }

        return A[t / 2];

    }

}

二、删除string里的substring

string=string.replace(substring, "")

string=string.replaceFirst("X",""); //删除第一个“X”

三、比较字符串:

s.equals("") while(!s.equals(""))

四、分割字符串

String string = "hello,world,java";

String[] substrings = string.split(",");

五、字母和数字之间的转换

'a' 的Unicode值是97,'A' 对应的Unicode值是65,

(char) 65就成了‘A’。int val = 'S'就把字符转换成了数字。

六、char[]变string

char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = new String(charArray);

七、string转变成int

int result=Integer.valueOf(string_a);

八、井字棋问题

class Solution {

    public String tictactoe(int[][] moves) {

        int C = moves.length;

        // 只需要考虑最后一步棋和前面的是否能够组成一个三连

        int[] last = moves[C-1];

        int row =0, col=0, main=0, sub=0;

        int index = C-1;

        while (index >= 0){

            int current[] = moves[index];

            if(current[1] == last[1])   row++;

            if(current[0] == last[0])   col++;

            if(current[0] == current[1]) main++;

            if(current[0] + current[1] == 2) sub++;

            index -= 2;

        }

        if(row>=3 || col>=3 || main>=3 || sub>=3){

            return C%2 == 0? "B":"A"; //先看有没有人赢了,赢的人肯定是落下最后的棋子赢的,并且最后的棋子连成了一串。有人赢了我们再看是哪个人赢的。

        }

        if(C < 9) return "Pending";

        return "Draw";

    }

}

届ける言葉を今は育ててる
最后更新于 2024-09-03