ListNode , list 的运算
1.在链表中找值为 value 的节点,如果没有的话,返回空(null)
简单模拟,复杂度O(n)
//ListNode的定义 public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
} } public class num1 {
public ListNode a1(ListNode head, int val) {
for (ListNode node = head; node != null; node = node.next) {
if (node.val == val) {
return node;
}
} return null;
} }
2.给一个浮点数数组,求数组中的最大值
public class Solution {
public float Arraya1(float[] A) {
float max = A[0];
for (int i = 1; i < A.length; i++) {
if (A[i] > max) {
max = A[i];
}
} return max;
}
}
3.水仙花数的定义是,这个数等于他每一位数上的幂次之和 见维基百科的定义
比如一个3位的十进制整数153就是一个水仙花数。因为 153 = 13 + 53 + 33。
而一个4位的十进制数1634也是一个水仙花数,因为 1634 = 14 + 64 + 34 + 44。
给出n,找到所有的n位十进制水仙花数
直接枚举,拆数计算然后判断即可
class num2 {
public ArrayList<Integer> Numbers(int n) {
// write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
if (n == 1) {
for (int i = 0; i < 10; ++i)
result.add(i);
return result;
}
if (n == 6) {
result.add(548834);
return result;
}
for (int i = pow(10, n-1); i < pow(10, n); ++i) {
int j = i;
int s = 0;
while (j > 0) {
s += pow((j % 10), n);
j = j / 10;
}
if (s == i){
result.add(i);
}
} return result;
}
public int pow(int a, int b) {
int val = 1;
for (int i = 1; i <=b; ++i)
val *=a;
return val;
}
};
请看官看看,有什么好建议可以提出.