ret

leetcode 121 股票买卖问题系列

混江龙づ霸主 提交于 2020-01-20 06:58:31
描述: 给一些列数字,表示每条股票的价格,如果可以买卖一次(不能同一天买和卖),求最大利益(即差最大)。 其他三道问题是,如果能买卖无限次,买卖两次,买卖k次。 题一: 实质是求后面一个数减前一个数的最大差值。 维护一个最小值,和当前最大值。只需遍历一次,空间也是常数。 int maxProfit(vector<int>& prices) { if (prices.size() < 1) return 0; int min_ = prices[0]; int ret = 0; for (int i = 1; i < prices.size(); i++) { ret = max(ret, prices[i] - min_); min_ = min(min_, prices[i]); } return ret; } 题二: 只要是后一个数比前一个大,都增。 int maxProfit(vector<int>& prices) { if (prices.size() < 1) return 0; int ret = 0; for (int i = 0; i < prices.size() - 1; i++) { ret += max(prices[i + 1] - prices[i], 0); } return ret; } 题三: 可进行两次操作。 其中一个思路,可以关注分界点

eeui的post、get请求封装

ぃ、小莉子 提交于 2020-01-15 09:21:32
<script> const stream = weex.requireModule('stream'); const storage = weex.requireModule('storage') // const jbtoken = storage.getItem("teacherToken",(res)=>{ return res.data}) // 如果你需要通过 POST 发送 json 数据, 需要将 Content-Type 设为 application/json。 let get = function(url,token){ return new Promise( function(resolve,reject ) { stream.fetch({ method: 'get', // timeout: 30000,//30s url:url, type: "json", headers: {"Content-Type":"application/json","token":token} }, function(ret){ if(!ret.ok){ let err = new Error('请求失败') reject(err); }else{ resolve(ret.data); } }) }); } let post = function({url,token

LC68. 文本左右对齐

牧云@^-^@ 提交于 2020-01-13 13:33:16
这个题太无聊了 /** * @Classname Solution1 * @Description TODO * @Date 2020/1/13 0:36 * @Author SonnSei */ public class Solution1 { public static List < String > fullJustify ( String [ ] words , int maxWidth ) { Stack < String > stack = new Stack < > ( ) ; for ( int i = words . length - 1 ; i >= 0 ; i -- ) { stack . push ( words [ i ] ) ; } List < String > ret = new ArrayList < > ( ) ; while ( ! stack . isEmpty ( ) ) { ArrayList < String > list = new ArrayList < > ( ) ; int cnt = 0 ; while ( ! stack . isEmpty ( ) && cnt + stack . peek ( ) . length ( ) + list . size ( ) <= maxWidth ) { cnt += stack .

LC13. 罗马数字转整数

六月ゝ 毕业季﹏ 提交于 2020-01-12 19:38:27
1. 解法一 /** * @Classname Solution1 * @Description TODO * @Date 2020/1/11 6:50 * @Author SonnSei */ public class Solution1 { /* 优先匹配两个字符的 */ public int romanToInt ( String s ) { int [ ] value = { 1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1 } ; String [ ] strs = { "M" , "CM" , "D" , "CD" , "C" , "XC" , "L" , "XL" , "X" , "IX" , "V" , "IV" , "I" } ; Map < String , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < strs . length ; i ++ ) { map . put ( strs [ i ] , value [ i ] ) ; } int ret = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i )

Leetcode(7)之整数反转

孤街浪徒 提交于 2020-01-07 01:40:22
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 题目描述: 解题思路: 维护一个结果数,每次将x除以10取余弹出一位,弹出的数加上结果数乘以10。由于 int.MaxValue = 2147483647, in.MinValue = -2147483648,所以结果数乘以10之前要小于 2147483647/10 并大于 -2147483648/10,如果结果数乘以10之前等于 2147483647/10 或等于 -2147483648/10,那么对应的弹出数就要小于7或者大于-8。 代码: public int Reverse(int x) { int ret = 0; while (x != 0) { int pop = x % 10; x /= 10; if (ret > int.MaxValue / 10 || (ret == int.MaxValue / 10 && pop > 7)) return 0; if (ret < int.MinValue / 10 || (ret == int.MinValue / 10 && pop < -8)) return 0; ret = 10 * ret + pop; } return ret; } 来源: oschina 链接: https://my.oschina.net/u/3860932/blog

LeetCode:合并两个有序链表(c#)

五迷三道 提交于 2020-01-01 19:59:32
题目内容 代码实现 /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public ListNode MergeTwoLists ( ListNode l1 , ListNode l2 ) { //读取最小的节点作为根节点 var head = ReadMinNode ( ref l1 , ref l2 ) ; //最后一个节点 var node = head ; while ( node != null ) { //循环读取最小节点赋值到最后一个节点的next节点 node . next = ReadMinNode ( ref l1 , ref l2 ) ; //赋值next节点为最后一个节点 node = node . next ; } return head ; } /// <summary> /// 读取最小的节点 /// </summary> private ListNode ReadMinNode ( ref ListNode l1 , ref ListNode l2

C#调用存储过程返回值

☆樱花仙子☆ 提交于 2019-12-31 01:59:38
因为一般都用T-SQL来执行数据库操作,偶尔现在转到用存储过程,竟然写不来调用过程的返回值了,查了很多资料,都不详细,或者没到点上,也许是我比较笨,呵呵。 C# 源代码 1 /// <summary> 2 /// 校验用户 3 /// </summary> 4 /// <param name="user"> 用户信息 </param> 5 /// <returns></returns> 6 public static int VerifyUser(clsUser user) 7 { 8 int iRet; 9 string sql = String.Format( " EXECUTE VerifyUser @myVerifyReader OUTPUT,'{0}','{1}' " ,user.username,user.password); // 执行的T-SQL串 10 SqlCommand scmd = new SqlCommand(sql, conn); 11 scmd.Parameters.Add( new SqlParameter( " @myVerifyReader " ,SqlDbType.Int)); 12 scmd.Parameters[ " @myVerifyReader " ].Direction = ParameterDirection.Output; 13

逗号运算符

倾然丶 夕夏残阳落幕 提交于 2019-12-30 02:33:57
C语言自学之 逗号运算符 1 #include <stdio.h> 2 3 int main() 4 { 5 int ret; 6 7 // 8 3 15 8 ret = (3+5,1+2,3*5);//运行结果为15. 9 //ret = 3+5,1+2,3*5; 10 // (ret=3+5),1+2,3*5; 11 //运行结果为8.(赋值运算优先级高于逗号运算) 12 printf("%d",ret); 13 14 return 0; 15 } 逗号运算符   优先级最低。   从左至右运算,最右边的值为逗号运算符的值。 1 #include <stdio.h> 2 int main() 3 { 4 int x = 5; 5 int y = 8; 6 //在这里合适的位置加上括号使z的值为4 7 //int z = x*8/y+2;//运行结果为7:5*8/8+2=7; 8 int z = x*8/(y+2);//运行结果为4:8+2=10,5*8=40,40/10=4; 9 printf("z=%d\n", z); 10 return 0; 11 } C语言中运算符中最高等级的为() 来源: https://www.cnblogs.com/wuyuenini/p/8976493.html

C--动态申请二维数组

。_饼干妹妹 提交于 2019-12-26 09:55:15
# include <stdio.h> # include <malloc.h> int * * malloc2d ( int row , int col ) { int * * ret = NULL ; if ( ( row > 0 ) && ( col > 0 ) ) { int * p = NULL ; ret = ( int * * ) malloc ( row * sizeof ( int * ) ) ; p = ( int * ) malloc ( row * col * sizeof ( int ) ) ; if ( ( ret != NULL ) && ( p != NULL ) ) { int i = 0 ; for ( i = 0 ; i < row ; i ++ ) { ret [ i ] = p + i * col ; } } else { free ( ret ) ; free ( p ) ; ret = NULL ; } } return ret ; } void free2d ( int * * p ) { if ( * p != NULL ) { free ( * p ) ; } free ( p ) ; } int main ( ) { int * * a = malloc2d ( 3 , 3 ) ; //malloc申请的内存需要先清零

信号灯的典型应用

不羁的心 提交于 2019-12-17 14:41:56
二值信号灯:值为0或1.与互斥锁类似,资源可用时值为1,不可用时值为0。 程序如下: 读端 #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <errno.h> #include <sys/sem.h> #include <string.h> #include <strings.h> int wait(void) { int semid = -1; int ret = -1; key_t key = ftok("/home",2); if ( -1 == key ) { perror("ftok"); return -1; } semid = semget(key,2,0666|IPC_CREAT|IPC_EXCL);//获得信号灯ID。包含2个信号灯 if ( -1 == semid ) { if( EEXIST == errno ) { semid = semget(key,2,0666); if (-1 == semid ) { perror("semget"); return -1; } }else { perror("semget"); return -1; } } struct sembuf