【100Days of 100 line Code】4 day

本文分享了作者在LeetCode上解决多个编程题目的过程,包括两数相除、二进制求和、数组加一等题目。对于部分题目如直线上最多的点,作者遇到了一些困难,计划后续继续研究。

列表类型不支持索引?好吧 调试了大半天,也没有找到办法解决。就这样吧。反正本地能跑()

加班加点的完成任务,今天第四天

日常刷题。

LeetCode #29 两数相除

没有AC,过了十个点,仅仅十个点,暂时的 没有思路,先挖个坑,以后有想法了 再回来补。。

判断是否为0或者 除数比被除数大,即return 0

不满足上面条件,每次减被除数,这里要注意正数和负数的区别。

class Solution:
    def divide(self, dividend, divisor):
        """
        :type dividend: int
        :type divisor: int
        :rtype: int
        """
        count,i = True,0
        CopyDiviend = dividend
        if dividend == 0 or abs(dividend) < abs(divisor) :
            return 0
        else :
            while  count :
                CopyDiviend = abs(CopyDiviend) - abs(divisor)
                i += 1
                if abs(CopyDiviend) <= abs(divisor) :
                    count = False
            if dividend > 0 and divisor > 0 or dividend < 0 and divisor < 0 :
                return i
            else :
                return -i

LeetCode #67 二进制求和 

 很水的题,进制转化 + 二进制的处理即可

class Solution:
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        a = int(a,2)
        b = int(b,2)

        return str(bin(a+b)).replace('0b','')

LeetCode #66 加一

数组转化为字符串,字符串转化为数字,处理数字,数字转化为字符串,然后再转化为数组,即可。

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        new_num = ''
        for i in range(len(digits)):
            new_num = new_num + str(digits[i]) 
        new_num = int(new_num) + 1
        str_digits = str(new_num)
        re_digits = []
        for i in range(len(str_digits)):
            re_digits.append(int(str_digits[i]))
        return re_digits
        

LeetCode #149 直线上最多的点

分别储存x,y,x-y,x+y,并计算其中出现最多的元素的次数,即再同一直线上面的点,return 四个数字中间最大的点

这里没有AC,报错: 'Point' object does not support indexing,但是points 是list,怎么可能不支持索引???

明天白天问问大佬怎么解决。

class Solution:
    def maxPoints(self, Points):
        """
        :type points: List[Point]
        :rtype: int
        """
        Points_x = []
        Points_y = []
        Points_x_add_y =[]
        Points_x_minus_y = []
        for i in range(len(Points)):
            Points_x.append(Points[i][0]) # x轴坐标
            Points_y.append(Points[i][1]) # y轴坐标
            Points_x_add_y.append(Points[i][0] + Points[i][1])
            Points_x_minus_y.append(Points[i][0] - Points[i][1])
        return max(Solution.handlePoints(Points_x),Solution.handlePoints(Points_y),Solution.handlePoints(Points_x_add_y),Solution.handlePoints(Points_x_minus_y))
        

    def handlePoints(getlist):
        new_getlist = set(getlist)
        pointsTime = 0
        for i in new_getlist:
            if getlist.count(i) > pointsTime:
                pointsTime = getlist.count(i)
        return pointsTime

 

转载于:https://www.cnblogs.com/mygzhh/p/9308141.html

/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention »ùÓÚSTM32µÄÍòÄêÀú Ч¹ûÑÝÊ&frac34;ÊÓÆµÁ´½Ó£ºhttps://www.bilibili.com/video/BV11w4m1a74J/ ÓÎϷЧ¹û¿ÉÒÔ ¹Ø×¢ ΢ÐŹ«Öںš¢BÕ&frac34;¡¢¶¶ÒôºÍ¿ìÊÖµÈÆ½Ì¨£¨Õ˺ÅÃû³Æ£ºJLµ¥Æ¬»ú£©£¬ÓÐÂ&frac14;ÖÆµÄÊÓÆµµÄЧ¹û¡£ Ò²·ÖÏíÁËÆäËûºÃÍæÓÐȤµÄ´úÂ룬¶&frac14;¿ÉÒÔȥ΢ÐŹ«ÖÚºÅÏÂÔØ¡£¸ÐÐËȤµÄ¿ÉÒÔ¿´Ï¡£ ²ÄÁÏ£º STM32F103C8T6×îСϵͳ°å SSD1306 OLED128*64ÏÔÊ&frac34;ÆÁ 6¸ö°´&frac14;ü IO½ÓÏß˵Ã÷£º Ïòϰ´&frac14;ü£ºPA4 ÏòÓÒ°´&frac14;ü£ºPA5 ÏòÉϰ´&frac14;ü£ºPB11 Ïò×ó°´&frac14;ü£ºPB10 F1°´&frac14;ü£ºPA6 OLED SCK£ºPA11 OLED SDA£ºPA12 * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "OLED.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ #define LEFT_BUTTON 1 #define RIGHT_BUTTON 2 #define UP_BUTTON 3 #define DOWN_BUTTON 4 #define A_BUTTON 5 #define B_BUTTON 6 /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ uint32_t yr = 2024; uint8_t mo = 7; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ unsigned char isLeapYear(uint32_t yr) { if (yr % 100) return (yr % 400) == 0; else return (yr % 4) == 0; } uint8_t getDaysOfMonth(uint32_t yr, uint8_t mo) { return (mo == 2) ? (28 + isLeapYear(yr)) : 31 - (mo - 1) % 7 % 2; } uint8_t getDayOfWeek(uint32_t yr, uint8_t mo, uint8_t d) { return (d += mo < 3 ? yr-- : yr - 2, 23 * mo / 9 + d + 4 + yr / 4 - yr / 100 + yr / 400) % 7; } int get_key_input() { if(HAL_GPIO_ReadPin(left_key_GPIO_Port, left_key_Pin)==0){ HAL_Delay(100); if(HAL_GPIO_ReadPin(left_key_GPIO_Port, left_key_Pin)==0) return LEFT_BUTTON; } if(HAL_GPIO_ReadPin(right_key_GPIO_Port, right_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(right_key_GPIO_Port, right_key_Pin)==0) return RIGHT_BUTTON; } if(HAL_GPIO_ReadPin(up_key_GPIO_Port, up_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(up_key_GPIO_Port, up_key_Pin)==0) return UP_BUTTON; } if(HAL_GPIO_ReadPin(down_key_GPIO_Port, down_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(down_key_GPIO_Port, down_key_Pin)==0) return DOWN_BUTTON; } if(HAL_GPIO_ReadPin(f1_key_GPIO_Port, f1_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(f1_key_GPIO_Port, f1_key_Pin)==0) return A_BUTTON; } if(HAL_GPIO_ReadPin(f2_key_GPIO_Port, f2_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(f2_key_GPIO_Port, f2_key_Pin)==0) return B_BUTTON; } return 0; } void update() { switch (get_key_input()) { case LEFT_BUTTON: if (--yr < 1970) yr = 1970; break; case RIGHT_BUTTON: if (++yr > 2199) yr = 2199; break; case UP_BUTTON: if (++mo > 12) { if (++yr > 2199) { yr = 2199; mo = 12; } else { mo = 1; } } break; case DOWN_BUTTON: if (--mo < 1) { if (--yr < 1970) { yr = 1970; mo = 1; } else { mo = 12; } } break; case A_BUTTON: yr = 2024; mo = 7; break; case B_BUTTON: break; } } void draw() { uint8_t days = getDaysOfMonth(yr, mo); uint8_t day = getDayOfWeek(yr, mo, 1); OLED_ShowNum(40, 0, yr, 4, OLED_6X8); OLED_ShowNum(70, 0, mo, 2, OLED_6X8); OLED_ShowString(4, 8, "SU MO TU WE TH FR SA", OLED_6X8); uint8_t x = day; uint8_t y = 0; for (uint8_t i = 1; i <= days; ++i) { if (i < 10) { OLED_ShowNum(x * 18 + 4 + 6, y * 8 + 16, i, 1, OLED_6X8); } else { OLED_ShowNum(x * 18 + 4, y * 8 + 16, i, 2, OLED_6X8); } if (++x == 7) { x = 0; ++y; } } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ OLED_Init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while(get_key_input()==0); while (1) { OLED_Clear(); update(); draw(); OLED_Update(); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, scl_Pin|sda_Pin, GPIO_PIN_SET); /*Configure GPIO pins : down_key_Pin right_key_Pin f1_key_Pin f2_key_Pin */ GPIO_InitStruct.Pin = down_key_Pin|right_key_Pin|f1_key_Pin|f2_key_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : left_key_Pin up_key_Pin */ GPIO_InitStruct.Pin = left_key_Pin|up_key_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pins : scl_Pin sda_Pin */ GPIO_InitStruct.Pin = scl_Pin|sda_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ 学习代码
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值