java正则题_牛客网java编程题整理(不定期更新)
文章目錄
190516 - 調整數組順序使奇數位于偶數前面
題目
我的代碼
高贊代碼(via:海天一色)
190517 — 鏈表中倒數第k個結點
題目
我的代碼
高贊代碼(via:渡不過己)
190523 - 表示字符的字符串
題目
我的代碼 - 窮舉正則表達式
高贊代碼(via:sarah_cw)
總結
190516 - 調整數組順序使奇數位于偶數前面
題目
輸入一個整數數組,實現一個函數來調整該數組中數字的順序,使得所有的奇數位于數組的前半部分,所有的偶數位于數組的后半部分,并保證奇數和奇數,偶數和偶數之間的相對位置不變。
我的代碼
public static void reOrderArray(int[] array){
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for(int i = 0; i < array.length; i++){
int num = array[i];
if(num % 2 != 0){
sb1.append(num);
sb1.append(",");
}else{
sb2.append(num);
sb2.append(",");
}
}
sb1.append(sb2);
String str1 = sb1.toString();
String[] strArray1 = str1.split(",");
for(int i = 0; i < array.length; i++){
array[i] = new Integer(strArray1[i]);
}
//System.out.println(Arrays.toString(array));
高贊代碼(via:海天一色)
public class Solution {
public void reOrderArray(int [] array) {
for(int i= 0;i
190517 — 鏈表中倒數第k個結點
題目
輸入一個鏈表,輸出該鏈表中倒數第k個結點。
我的代碼
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode node = head;
int size = 0;
while(node.next != null){
node = node.next
size++;
}
if(size < k) return null;
for(int i = 0; i < size - k; i++){
node = node.next;
}
return node;
}
}
高贊代碼(via:渡不過己)
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode pre=null,p=null;
//兩個指針都指向頭結點
p=head;
pre=head;
//記錄k值
int a=k;
//記錄節點的個數
int count=0;
//p指針先跑,并且記錄節點數,當p指針跑了k-1個節點后,pre指針開始跑,
//當p指針跑到最后時,pre所指指針就是倒數第k個節點
while(p!=null){
p=p.next;
count++;
if(k<1){
pre=pre.next;
}
k--;
}
//如果節點個數小于所求的倒數第k個節點,則返回空
if(count
190523 - 表示字符的字符串
題目
請實現一個函數用來判斷字符串是否表示數值(包括整數和小數)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示數值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。
我的代碼 - 窮舉正則表達式
String string = new String(str);
String regex1 = "\\+[1-9][0-9]*";
String regex2 = "\\-[1-9][0-9]*";
String regex3 = "[1-9][0-9]*";
String regex4 = "[1-9][0-9]*e[1-9][0-9]*";
String regex5 = "[1-9][0-9]*\\.[0-9]*e\\+[1-9][0-9]*";
String regex6 = "\\-[1-9][0-9]*e[1-9][0-9]*";
String regex7 = "\\-[1-9][0-9]*e\\-[1-9][0-9]*";
String regex8 = "[1-9][0-9]*e\\-[1-9][0-9]*";
String regex9 = "[1-9][0-9]*\\.[0-9]*";
String regex10 = "[1-9][0-9]*E[1-9][0-9]*";
String regex11 = "[1-9][0-9]*\\.[0-9]*E\\+[1-9][0-9]*";
String regex12 = "\\-[1-9][0-9]*E[1-9][0-9]*";
String regex13 = "\\-[1-9][0-9]*E\\-[1-9][0-9]*";
String regex14 = "[1-9][0-9]*E\\-[1-9][0-9]*";
String regex15 = "\\-\\.[0-9]*";
boolean flag = false;
if(string.matches(regex1) || string.matches(regex2) || string.matches(regex3) ||
string.matches(regex4) || string.matches(regex5) || string.matches(regex6) ||
string.matches(regex7) || string.matches(regex8) || string.matches(regex9) ||
string.matches(regex10) || string.matches(regex11) || string.matches(regex12) ||
string.matches(regex13) || string.matches(regex14) || string.matches(regex15)){
flag = true;
}
return flag;
高贊代碼(via:sarah_cw)
public class Solution {
public boolean isNumeric(char[] str) {
String s=String.valueOf(str);
//return s.matches("[+-]?[0-9]*(.[0-9]*)?([eE][+-]?[0-9]+)?");
return s.matches("[+-]?[0-9]*(\\.[0-9]*)?([eE][+-]?[0-9]+)?");
}
}
總結
大佬兩行AC,我還是太菜了…正則表達式還是沒學到精髓…
總結
以上是生活随笔為你收集整理的java正则题_牛客网java编程题整理(不定期更新)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sift分类java_使用SIFT /
- 下一篇: scala java抽象理解_Scala