Leetcode刷题
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
- nums1 = [1, 3]
- nums2 = [2]
- The median is 2.0
Example 2:
- nums1 = [1, 2]
- nums2 = [3, 4]
- The median is (2 + 3)/2 = 2.5
- double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
- int *nums = NULL;
- int totle_num = ;
- int mid_num = ;
- double mid = ;
- int i = , j = , k = ;
- totle_num = nums1Size + nums2Size;
- mid_num = totle_num >> ;
- nums = (int *)malloc(sizeof(int) * (totle_num));
- if (nums == NULL) {
- return -;
- }
- for (k = ; k < (mid_num + ); k++) {
- if (nums1Size == || i == nums1Size) {
- *(nums + k) = *(nums2 + j);
- j++;
- } else if (nums2Size == || j == nums2Size) {
- *(nums + k) = *(nums1 + i);
- i++;
- } else if (*(nums1 + i) <= *(nums2 + j)) {
- *(nums + k) = *(nums1 + i);
- i++;
- } else if (*(nums1 + i) > *(nums2 + j)){
- *(nums + k) = *(nums2 + j);
- j++;
- }
- }
- if (totle_num % == ) {
- mid = (double)((*(nums + (mid_num - )) + *(nums + mid_num))) / (double);
- } else {
- mid = *(nums + mid_num);
- }
- free(nums);
- return mid;
- }
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * struct ListNode *next;
- * };
- */
- struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
- struct ListNode *tail1 = NULL;
- struct ListNode *tail2 = NULL;
- struct ListNode *new = NULL;;
- int tmp = ;
- if (NULL == l1 || NULL == l2) {
- perror("list NULL!\n");
- return NULL;
- }
- for (tail1 = l1, tail2 = l2; (tail1->next != NULL) || (tail2->next != NULL);) {
- if ((tail1->next != NULL) && (tail2->next != NULL)) {
- tail1->val += tail2->val;
- } else if ((tail1->next != NULL) && (tail2->next == NULL)) {
- if (tail2 != NULL) {
- tail1->val += tail2->val;
- }
- } else if ((tail1->next == NULL) && (tail2->next != NULL)) {
- if (tail1 != NULL) {
- tail1->val += tail2->val;
- new = (struct ListNode *)malloc(sizeof(struct ListNode));
- if (NULL == new) {
- perror("malloc failed!\n");
- return NULL;
- }
- tail1->next = new;
- new->val = ;
- new->next = NULL;
- }
- }
- tail1->val += tmp;
- if (tail1->val < ) {
- tmp = ;
- } else {
- tmp = tail1->val / ;
- tail1->val %= ;
- }
- if (tail1->next != NULL) {
- tail1 = tail1->next;
- }
- if (tail2->next != NULL) {
- tail2 = tail2->next;
- } else {
- tail2->val = ;
- }
- }
- if (tail1 != NULL) {
- if (tail2 != NULL) {
- tail1->val += tail2->val;
- }
- tail1->val += tmp;
- if (tail1->val >= ) {
- tmp = tail1->val / ;
- tail1->val %= ;
- new = (struct ListNode *)malloc(sizeof(struct ListNode));
- if (NULL == new) {
- perror("malloc failed!\n");
- return NULL;
- }
- tail1->next = new;
- new->val = tmp;
- new->next = NULL;
- }
- }
- return l1;
- }
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
[1,3,5,6]
, 5 → 2[1,3,5,6]
, 2 → 1[1,3,5,6]
, 7 → 4
[1,3,5,6]
, 0 → 0
- int searchInsert(int* nums, int numsSize, int target) {
- int *pNum = NULL;
- int i = ;
- if (NULL == nums) {
- return -;
- }
- if (numsSize <= ) {
- return -;
- }
- pNum = nums;
- for (i = ; i < numsSize; i++) {
- if (*(pNum + i) >= target) {
- return i;
- } else {
- if (i == numsSize - ) {
- return numsSize;
- }
- }
- }
- return -;
- }
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * struct ListNode *next;
- * };
- */
- struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
- struct ListNode *tail1 = NULL;
- struct ListNode *tail2 = NULL;
- struct ListNode *bak_node1 = NULL;
- struct ListNode *bak_node2 = NULL;
- struct ListNode *bak_node = NULL;
- if (l1 == NULL) {
- return l2;
- }
- if (l2 == NULL) {
- return l1;
- }
- // 找到第一个数据最小的链表,作为返回链表
- if (l1->val <= l2->val) {
- tail1 = l1;
- tail2 = l2;
- } else {
- tail1 = l2;
- tail2 = l1;
- }
- bak_node1 = tail1;
- bak_node2 = tail2;
- bak_node = tail1;
- while (tail2 != NULL) {
- while (tail1->val <= tail2->val) {
- if (tail1->next == NULL) {
- tail1->next = tail2;
- return bak_node;
- }
- bak_node1 = tail1;
- tail1 = tail1->next;
- }
- bak_node2 = tail2->next;
- tail2->next = tail1;
- bak_node1->next = tail2;
- bak_node1 = bak_node1->next;
- tail2 = bak_node2;
- }
- return bak_node;
- }
- bool isValid(char* s) {
- char *pString = NULL;
- int flag1 = , flag2 = , flag3 = ;
- int flag = ;
- if (s == NULL) {
- perror("String NULL!\n");
- return false;
- }
- pString = s;
- while (*pString != '\0') {
- if (*pString == '(') {
- flag1++;
- flag = ;
- } else if (*pString == ')') {
- if (flag == && flag1 != ) {
- flag1--;
- } else if (flag == ) {
- return false;
- }
- }
- if (*pString == '{') {
- flag2++;
- flag = ;
- } else if (*pString == '}') {
- if (flag == && flag2 != ) {
- flag2--;
- } else if (flag == ) {
- return false;
- }
- }
- if (*pString == '[') {
- flag3++;
- flag = ;
- } else if (*pString == ']') {
- if (flag == && flag3 != ) {
- flag3--;
- } else if (flag == ) {
- return false;
- }
- }
- if (flag1 + flag2 + flag3 > ) {
- return false;
- }
- pString++;
- }
- if ((flag1 != ) || (flag2 != ) || (flag3 != )) {
- return false;
- }
- return true;
- }
Determine whether an integer is a palindrome. Do this without extra space.
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
- bool isPalindrome(int x) {
- int data = ;
- int num = ;
- int count = ;
- int i = ;
- int tmp1 = , tmp2 = ;
- data = x;
- if (data < ) {
- return ;
- }
- while (data / ) {
- data /= ;
- tmp1 *= ;
- count++;
- }
- if (data != ) {
- count++;
- if (count != ) {
- tmp1 *= ;
- }
- }
- data = x;
- i = ;
- if (count == ) {
- if (data / != data % ) {
- return ;
- }
- i++;
- tmp2 *= ;
- }
- while ((i < count / ) && ((data % tmp1 / (tmp1 / )) == (data % (tmp2 * ) / tmp2))) {
- tmp1 /= ;
- tmp2 *= ;
- i++;
- }
- if (i != count / ) {
- return ;
- }
- return ;
- }
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
- int reverse(int x) {
- int array[] = { };
- int int_max = 0x7FFFFFFF;
- int int_min = 0x80000000;
- int data = ;
- int count = ;
- int num = ;
- int i = ;
- data = x;
- if ((int)data > (int)0x7FFFFFFF) {
- printf("data > 0x7FFFFFFF");
- return ;
- }
- if ((int)data < (int)0x80000000) {
- printf("data < 0x80000000\n");
- return ;
- }
- if (data == ) {
- printf("data == 0\n");
- return ;
- }
- i = ;
- data = x;
- count = ;
- while ((data / ) != ) {
- array[i++] = data % ;
- data = data / ;
- count++;
- }
- if (data != ) {
- array[i] = data;
- data = ;
- count++;
- }
- if (count == ) {
- i = ;
- num = ;
- while (i < count) {
- if ((array[i] == int_max/num) || (array[i] == int_min/num)) {
- i++;
- int_max %= num;
- int_min %= num;
- num /= ;
- } else if ((array[i] < int_max/num) && (array[i] > int_min/num)) {
- break;
- } else {
- return ;
- }
- }
- }
- for (i = count - , num = ; i >= ; i--) {
- data += array[i] * num;
- num *= ;
- }
- if ((int)data > (int)0x7FFFFFFF) {
- printf("After: data > 0x7FFFFFFF");
- return ;
- }
- if ((int)data < (int)0x80000000) {
- printf("After: data < 0x80000000\n");
- return ;
- }
- return data;
- }
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int* twoSum(int* nums, int numsSize, int target) {
- int *parray = NULL;
- int i = , j = ;
- parray = (int *)malloc(sizeof(int) * );
- if (NULL == parray) {
- perror("malloc error!\n");
- return NULL;
- }
- for (i = ; i < numsSize; i++) {
- for (j = i + ; j < numsSize; j++) {
- if (nums[i] + nums[j] == target) {
- *parray = i;
- *(parray + ) = j;
- break;
- }
- }
- }
- return parray;
- }
Leetcode刷题的更多相关文章
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结之双指针法
Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- LeetCode刷题总结-树篇(下)
本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结.前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题, ...
随机推荐
- Qt+VS2015应用程序发布
本文以Qt 5.9.1+VS2015编译环境为例介绍应用程序发布流程,也适用于Qt+mingw的情况. 1. Qt依赖库 将需要发布的exe(如test.exe),放到单独的目录. 在"开始 ...
- 【学习】Zepto与jQuery 差别
前几天遇到一个项目,需要把jquery全部改成Zepto,当时因为自己没有实际经验,所以没有接.今天查了一下两者究竟有什么区别. 首先看到了这么一篇文章:http://www.bootcss.com/ ...
- 微信公众号第三方 推送component_verify_ticket协议
整了一天,终于弄明白了 component_verify_ticket 怎么获取的了.在此先批一下微信公众号平台,文档又没写清楚,又没有客服,想搞哪样哈! 好,回归正题. 第一,先通过开发者资质认证, ...
- .net Mvc框架原理
.net Mvc框架原理 本文只是简要说明原理,学习后的总结. 1.当一个Http请求发送后会被URLRoutingModule拦截(这时候也就是正式进入管道,下章会讲管道事件) 2.这时根据Isap ...
- ios 返回不会自动刷新页面问题
在实际开发过程中,移动端的兼容性问题有很大的坑,安卓可以了ios不行,ios可以了安卓又失效了这样,其中ios的回退操作就是不会自动刷新页面,很烦! 常见的history.back() history ...
- [译]ASP.NET Core 2.0 全局配置项
问题 如何在 ASP.NET Core 2.0 应用程序中读取全局配置项? 答案 首先新建一个空项目,并添加两个配置文件: 1. appsettings.json { "Section1&q ...
- HiveQL简单操作DDL
hive-2.1.1 DDL操作 Create/Drop/Alter/Use Database 创建数据库 //官方指导 CREATE (DATABASE|SCHEMA) [IF NOT EXISTS ...
- Fedora 19 搭建Qt环境
1.搭建桌面环境fedora的源里包含的需要的套件包,用下面命令安装sudo yum intall qt qt-devel qt-x11 qt-doc qt-demos qt-examples qt- ...
- Azure 基础:用 PowerShell 自动登录
PowerShell 是管理 Azure 的最好方式,因为我们可以使用脚本把很多的工作自动化.比如把 Azure 上的虚拟机关机,并在适当的时间把它开机,这样我们就能节省一些开支,当然我们同时也为减少 ...
- 关于echarts、layer.js和jqGrid的知识点
使用echarts和layer.js直接去官方文档,能解决大部分问题. 但是有些问题,解释不够清楚,在这里记录一下. 1.echarts的使用 第一点:关于echarts的labelline在数据为零 ...