编程题训练1
删除链表中等于给定值val的所有节点。
样例
给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @param val an integer
* @return a ListNode
*/
public ListNode removeElements(ListNode head, int val) {
// Write your code here
//处理头节点就等于 Val 的情况
while ( head != null && head.val == val){
head = head.next; //将头节点后移
}
//处理只有 null 节点的情况
if(head==null) return head;
ListNode pre = head,nextNode = head.next;
while( nextNode != null ){
if( nextNode.val == val ){
pre.next = nextNode.next;
nextNode = nextNode.next;
}else{
pre = pre.next;
nextNode = nextNode.next;
}
}
return head;
}
}
查找斐波纳契数列中第 N 个数。
- 所谓的斐波纳契数列是指:前2个数是 0 和 1 。第 i 个数是第 i-1 个数和第i-2 个数的和。
class Solution {
/**
* @param n: an integer
* @return an integer f(n)
*/
public int fibonacci(int n) {
// write your code here
if(n==1)
return 0;
if(n==2)
return 1;
int f0 = 0;
int f1 = 1;
int i = 3;
int f = 0;
while(i<=n){
f = f0 + f1;
f0 = f1;
f1 = f;
i++;
}
return f;
}
}
Add Digits 加数字
- Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
样例
Given num = 38.
The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return 2.
public class Solution {
/**
* @param num a non-negative integer
* @return one digit
*/
public int addDigits(int num) {
// Write your code here
while (num / 10 > 0) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num;
}
}
//观察法
public int addDigits(int num) {
// Write your code here
return (num - 1) % 9 + 1;
}
计算两个数组的交1 (去重)
- 样例
nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].
public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set =new HashSet<Integer>();
for (int i : nums2) {
set.add(i);
}
HashSet<Integer> re = new HashSet<Integer>();
for (int i : nums1) {
if(set.contains(i))
{
re.add(i);
}
}
Iterator<Integer> iterator=re.iterator();
int[] reInt = new int[re.size()];
int i =0;
while(iterator.hasNext()){
reInt[i++] = iterator.next();
}
return reInt;
}
//用 hashSet
HashSet<Integer> set =new HashSet<Integer>();
for (int i : nums2) {
set.add(i);
}
HashSet<Integer> re = new HashSet<Integer>();
for (int i : nums1) {
if(set.contains(i))
{
re.add(i);
}
}
Iterator<Integer> iterator=re.iterator();
int[] reInt = new int[re.size()];
int i =0;
while(iterator.hasNext()){
reInt[i++] = iterator.next();
}
return reInt;
}
计算两个数组的交2
- 样例
nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].
public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// Write your code here
Arrays.sort(nums1);
Arrays.sort(nums2);
ArrayList<Integer> A = new ArrayList<Integer>();
int i=0;
int j=0;
while(i<nums1.length && j<nums2.length ){
if(nums1[i] == nums2[j]){
A.add(nums1[i]);
i++;
j++;
}else if(nums1[i] < nums2[j]){
i++;
}else{
j++;
}
}
int[] res = new int[A.size()];
for( i=0;i<A.size();i++){
res[i] = (int)A.get(i);
}
return res;
}
}
移动零
- 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
- 注意事项: 1.必须在原数组上操作 2.最小化操作数
- 思路:实际上就是将所有的非0数向前尽可能的压缩,最后把没压缩的那部分全置0就行了。比如103040,先压缩成134,剩余的3为全置为0。过程中需要一个指针记录压缩到的位置。
public class Solution {
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
public void moveZeroes(int[] nums) {
int cnt = 0, pos = 0;
// 将非0数字都尽可能向前排
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0){
nums[pos]= nums[i];
pos++;
}
}
// 将剩余的都置0
for(;pos<nums.length; pos++){
nums[pos] = 0;
}
}
}