文章目录
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<array.length-1;i++){
for(int j=0;j<array.length-1-i;j++){
if(array[j]%2==0&&array[j+1]%2==1){
int t = array[j];
array[j]=array[j+1];
array[j+1]=t;
}
}
}
}
}
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<a) return null;
return pre;
}
}
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,我还是太菜了…正则表达式还是没学到精髓…