那个题好像没有什么意思,因为俺是按照难易度排序然后开始刷题的哈。 第二题是258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. 就是给出一个非负整数,然后不断地加,加到个位数。加的规则如例子所示:38-->3+8=11-->1+1=2;再举一个例子,456-->4+5+6=15--->1+5=6. 给出的函数格式: int addDigits(int num) { } 尽量 不要用o(1)时间复杂度下面的递归算法。
第5个题:283. Move Zeroes 这个题不是很难,之前用python写的时候很简单。要求比较多。 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note: You must do this in-place without making a copy of the array. Minimize the total number of operations. 将给定数组中的零移动到最后,并保持整个数组的相对顺序。 例如;[0,1,0,3,12]变化之后是[1,2,12,0,0] 有两点要求: 1、不得制作给定数组的副本。 2、尽量减少操作步骤。 说白了就是最小的时间和空间复杂度。 函数格式: void moveZeroes(int* nums, int numsSize) { }
第六个题:237. Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function. 删除链表中的给定节点。尾节点除外。 例如:1-2-3-4中给定3,删除之后是1-2-4 函数格式: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ void deleteNode(struct ListNode* node) { } 今天放假,这些题的简单程度基本上是五分钟一个。我试试能发多少个吧。
第七题:100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 判定给定的两个二叉树是否相同。 相同标准为,结构与值均相同。 函数格式如下: /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ bool isSameTree(struct TreeNode* p, struct TreeNode* q) { }
第8题:242. Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string contains only lowercase alphabets. 确定给定两个字符串中字符是否相同(顺序可以不同)。 例如: s=“anagram”,t=“anagram”为true,s = "rat", t = "car", 为false。 假定:给定的字符串中仅仅包含小写英文字母。 c语言处理字符串是鸡肋。题目的难度就是字符串。 给定函数格式: bool isAnagram(char* s, char* t) { }