java正则题_牛客网java编程题整理(不定期更新)
文章目錄
190516 - 調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)前面
題目
我的代碼
高贊代碼(via:海天一色)
190517 — 鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)
題目
我的代碼
高贊代碼(via:渡不過己)
190523 - 表示字符的字符串
題目
我的代碼 - 窮舉正則表達(dá)式
高贊代碼(via:sarah_cw)
總結(jié)
190516 - 調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)前面
題目
輸入一個(gè)整數(shù)數(shù)組,實(shí)現(xiàn)一個(gè)函數(shù)來調(diào)整該數(shù)組中數(shù)字的順序,使得所有的奇數(shù)位于數(shù)組的前半部分,所有的偶數(shù)位于數(shù)組的后半部分,并保證奇數(shù)和奇數(shù),偶數(shù)和偶數(shù)之間的相對(duì)位置不變。
我的代碼
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 — 鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)
題目
輸入一個(gè)鏈表,輸出該鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)。
我的代碼
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;
//兩個(gè)指針都指向頭結(jié)點(diǎn)
p=head;
pre=head;
//記錄k值
int a=k;
//記錄節(jié)點(diǎn)的個(gè)數(shù)
int count=0;
//p指針先跑,并且記錄節(jié)點(diǎn)數(shù),當(dāng)p指針跑了k-1個(gè)節(jié)點(diǎn)后,pre指針開始跑,
//當(dāng)p指針跑到最后時(shí),pre所指指針就是倒數(shù)第k個(gè)節(jié)點(diǎn)
while(p!=null){
p=p.next;
count++;
if(k<1){
pre=pre.next;
}
k--;
}
//如果節(jié)點(diǎn)個(gè)數(shù)小于所求的倒數(shù)第k個(gè)節(jié)點(diǎn),則返回空
if(count
190523 - 表示字符的字符串
題目
請(qǐng)實(shí)現(xiàn)一個(gè)函數(shù)用來判斷字符串是否表示數(shù)值(包括整數(shù)和小數(shù))。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示數(shù)值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。
我的代碼 - 窮舉正則表達(dá)式
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]+)?");
}
}
總結(jié)
大佬兩行AC,我還是太菜了…正則表達(dá)式還是沒學(xué)到精髓…
總結(jié)
以上是生活随笔為你收集整理的java正则题_牛客网java编程题整理(不定期更新)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sift分类java_使用SIFT /
- 下一篇: scala java抽象理解_Scala